Markyp
Markyp

Reputation: 1

Question: 401 Unauthorized Error for Coinbase Advanced Trade API /api/v3/brokerage/products/<product_id>/ticker

Question: 401 Unauthorized Error for Coinbase Advanced Trade API /api/v3/brokerage/products/<product_id>/ticker

I’m building a trading bot using the Coinbase Advanced Trade API, but I’m encountering a 401 Unauthorized error when trying to fetch live market data from the /api/v3/brokerage/products/<product_id>/ticker endpoint.

Here are the details:

Setup:
    Using the coinbase-advancedtrade-python SDK (EnhancedRESTClient).
    API key format: organizations/{org_id}/apiKeys/{key_id}.
    The key is loaded correctly, and the bot successfully connects to the Coinbase API (e.g., fetching account balances and tradable pairs works).

Authentication:
    Authorization header:

Authorization: Bearer organizations/{org_id}/apiKeys/{key_id}

IP address is whitelisted.
The API key has permissions for View and Trade.

Issue:

Fetching live ticker data returns:

    HTTPError: 401 Client Error: Unauthorized for url: https://api.coinbase.com/api/v3/brokerage/products/BTC-GBP/ticker
    Response Content: Unauthorized

    This happens for all product pairs (e.g., BTC-GBP, ETH-GBP).

Troubleshooting Steps:
    Verified the API key format and permissions.
    Manually tested the API endpoint using curl, with the same 401 Unauthorized error.
    Checked that the IP address is correctly whitelisted in the API key settings.
    Tried generating signed requests using the private key, but the error persists.

Question:
    Has anyone successfully used the /api/v3/brokerage/products/<product_id>/ticker endpoint for live market data?
    Are there additional permissions or authentication steps required for this endpoint?
    Does the Coinbase Advanced Trade API require specific headers or signing for this endpoint?

Any insights or examples of working implementations would be greatly appreciated!

tried everything had to resort to v2, but i need live ticker feeds.

Upvotes: -1

Views: 50

Answers (1)

Chwebe
Chwebe

Reputation: 1

Don't know if that can help you but here is the code that i'm using to get data from the coinbase api. The documentation says : "CDP API keys are used to generate a JSON Web Token (JWT) for an API. Once you've generated a JWT, set it as a Authorization Bearer header to make an authenticated request." : https://docs.cdp.coinbase.com/coinbase-app/docs/api-key-authentication

 from coinbase import jwt_generator
import requests

# Initialize the client
api_key = "organizations/{ID/apiKeys/{ID}
api_secret = "-----BEGIN EC PRIVATE KEY-----\n{ID}\n-----END EC PRIVATE KEY-----\n"

request_method = "GET"
request_path = "/v2/accounts"

def make_coinbase_request(jwt_token, endpoint):
    """
    Make an authenticated request to Coinbase API using JWT token.
    """
    base_url = "https://api.coinbase.com"
    headers = {
        "Authorization": f"Bearer {jwt_token}",
        "Content-Type": "application/json"
    }
    
    response = requests.get(f"{base_url}{endpoint}", headers=headers)
    return response.json()

def main():
    jwt_uri = jwt_generator.format_jwt_uri(request_method, request_path)
    jwt_token = jwt_generator.build_rest_jwt(jwt_uri, api_key, api_secret)
    accounts = make_coinbase_request(jwt_token, "/v2/accounts")
    print("Accounts:", accounts)


if __name__ == "__main__":
    main()

Upvotes: 0

Related Questions