I am trying to call coinapi from aws lambda python to get the price of a bitcoin I got new API key from coinapi
from urllib.request import Request, urlopen
import json
from pprint import pprint
def lambda_handler(event, context):
pprint('received request: ' + str(event))
date_input = event['currentIntent']['slots']['Date']
print(date_input)
btc_price = get_bitcoin_price(date_input)
#pprint(dir(urllib.request))
def get_bitcoin_price(date):
print('get_bitcoin_price, date = ' + str(date))
request = Request('https://rest.coinapi.io/v1/ohlcv/BITSTAMP_SPOT_BTC_USD/latest?
period_id=1DAY&limit=1&time_start={}'.format(date))
#pprint(dir(request.add_header))
request.add_header('X-CoinAPI-Key', 'ACBDCAC9-63DC-4AD8-A72C-FBDB0DB67858')
response = json.loads(urlopen(request).read())
return response[0]['price_close']
I am getting the Unauthorized error:
{
"errorMessage": "HTTP Error 401: Unauthorized",
"errorType": "HTTPError",
"requestId": "b4cdc286-4f62-4d57-9bd8-1b2a3c1e47d3",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 9, in lambda_handler\n
btc_price = get_bitcoin_price(date_input)\n",
" File \"/var/task/lambda_function.py\", line 18, in get_bitcoin_price\n
response = json.loads(urlopen(request).read())\n",
" File \"/var/lang/lib/python3.9/urllib/request.py\", line 214, in urlopen\n
return opener.open(url, data, timeout)\n",
" File \"/var/lang/lib/python3.9/urllib/request.py\", line 523, in open\n
response = meth(req, response)\n",
" File \"/var/lang/lib/python3.9/urllib/request.py\", line 632, in
http_response\n response = self.parent.error(\n",
" File \"/var/lang/lib/python3.9/urllib/request.py\", line 561, in error\n
return self._call_chain(*args)\n",
" File \"/var/lang/lib/python3.9/urllib/request.py\", line 494, in _
call_chain\n result = func(*args)\n",
" File \"/var/lang/lib/python3.9/urllib/request.py\", line 641, in
http_error_default\n raise HTTPError(req.full_url, code, msg, hdrs, fp)\n"
]
}