Getting 401 error from coinbase advanced trade API

Hi everyone, I am new to the Coinbase API, and was wondering if anyone knows why I am getting 401 error from the following code. Thanks so much!!

from coinbase.rest import RESTClient

client = RESTClient(api_key=api_key, api_secret=api_secret)
market_trades = client.get("/api/v3/brokerage/products/BTC-USD/ticker", params={"limit": 5})
print(market_trades)

Hi @kafka

The 401 error you are encountering typically indicates an “Unauthorized” status code. This error suggests that the authentication credentials you are using with the Coinbase API are invalid or missing. Here are a few things you can check to resolve the issue:

  1. Verify API Key and Secret: Double-check that the

api_key

and

api_secret

values you are providing are correct. These credentials are essential for authenticating your requests to the Coinbase API. Ensure that you have copied them accurately from your Coinbase account.
2. Ensure API Key has necessary permissions: Make sure that the API key you are using has the required permissions to access the specific endpoints or perform the actions you are attempting. Some API operations may require additional permissions, such as trading or account management.
3. Check for any additional authentication requirements: Depending on the specific Coinbase API endpoint you are using, there may be additional authentication requirements, such as including a timestamp or signature in your request headers. Review the Coinbase API documentation for the specific endpoint you are working with to ensure you are meeting all the necessary authentication requirements

Hello @kafka

Where are you defining your api_key and api_secret variables? Your code as is doesn’t show you defining them… if you are in fact running just what you’re showing then you’re getting the correct results - a 401.

Take a look at this doc for help with setting up auth https://docs.cloud.coinbase.com/advanced-trade-api/docs/sdk-rest-client-trade#setting-up-your-client