pythoncoinbase-api

CoinBase API not returning product ticker data


I am trying to get the product ticker from CoinBase API https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproductticker using two different functions. The first one is the following:

def connect_to_coinbase_ticker(product_id: str):

    url = f"https://api.exchange.coinbase.com/products/{product_id}/ticker"

    headers = {"accept": "application/json"}

    response = requests.get(url, headers=headers).json()

    print(json.dumps(response, indent=4))

But I am getting this response message : {"message": "NotFound"}

By looking at the above link I also tried to use the function it is provided there to get these data:

def connect_to_coinbase_ticker(product_id: str):

    conn = http.client.HTTPSConnection("api.exchange.coinbase.com")
    payload = ''
    headers = {'Content-Type': 'application/json'}
    conn.request("GET", f"/products/{product_id}/ticker", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

But I keep getting an error message: {"message":"User-Agent header is required."}

So my question is how can I modify one of the above functions so I do not get these messages and get the product ticker data I request.

Thank you!


Solution

  • Make sure first that your pruduct_id is correct. I've used "btc-usd" and it works fine (don't forget the dash).
    As for the second function that requires the user-agent, you can add it like this:

    headers = {'Content-Type': 'application/json',  
               'User-Agent': 'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36'}  
    

    or whatever user-agent string that you want. You can google "whats my user agent string" to find out what is your actual user agent and use it.