I’m trying to turn this cURL request into a Python code. I want to eventually be able to save this to a CSV file but I need to get connected first.
curl --compressed -H 'Accept: application/json' -H 'X-Api-Key: 123abc' 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/cbse/spot/btc-usd/aggregations/count_ohlcv_vwap?interval=1h'
I started with this:
import requests
import json
key='api-key'
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
s = requests.Session()
s.auth = (key)
headers = {
   *not sure how to do this*
}
r = requests.get(url, headers=headers)
The docs say this needs to be in the header:
Accept: application/json Accept-Encoding: gzip:
How do I include the API key? how do I save the data once its returned?
Advertisement
Answer
X-Api-Key would be a request header, so you can include it in your headers variable, like this:
headers = {
   "X-Api-Key": key,
   "Accept": "application/json",
   "Accept-Encoding": "gzip"
}
(took the others ones from your current curl request)
You can get the data by using r.text, like this:
print(r.text)
Your code should look like this:
import requests
import json
key='api-key'
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
headers = {
   "X-Api-Key": key,
   "Accept": "application/json",
   "Accept-Encoding": "gzip"
}
r = requests.get(url, headers=headers)
print(r.text)
If you want to get a json object instead, you can use r.json()
