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.
JavaScript
x
2
1
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'
2
I started with this:
JavaScript
1
14
14
1
import requests
2
import json
3
4
key='api-key'
5
6
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
7
s = requests.Session()
8
s.auth = (key)
9
10
headers = {
11
*not sure how to do this*
12
}
13
r = requests.get(url, headers=headers)
14
The docs say this needs to be in the header:
JavaScript
1
3
1
Accept: application/json
2
Accept-Encoding: gzip:
3
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:
JavaScript
1
6
1
headers = {
2
"X-Api-Key": key,
3
"Accept": "application/json",
4
"Accept-Encoding": "gzip"
5
}
6
(took the others ones from your current curl request)
You can get the data by using r.text
, like this:
JavaScript
1
2
1
print(r.text)
2
Your code should look like this:
JavaScript
1
16
16
1
import requests
2
import json
3
4
key='api-key'
5
6
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
7
8
headers = {
9
"X-Api-Key": key,
10
"Accept": "application/json",
11
"Accept-Encoding": "gzip"
12
}
13
14
r = requests.get(url, headers=headers)
15
print(r.text)
16
If you want to get a json object instead, you can use r.json()