1

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?

2
  • What about starting from the documentation? Commented Jan 19, 2021 at 19:22
  • You could just put the curl request in a subprocess.check_output command and go from there. Commented Jan 19, 2021 at 19:23

1 Answer 1

3

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()

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.