Coinbase Advanced Trade Python SDK
Official Coinbase Advanced Trade API Python SDK. Current version: 1.8.2 (Mar 2026). TWO deprecated packages to avoid: 'coinbase' (old Coinbase Commerce SDK, EOL) and 'cbpro' (Coinbase Pro, discontinued). The correct package is 'coinbase-advanced-py'. Uses CDP (Coinbase Developer Platform) API keys with EC private key format — NOT the old HMAC API key format. Import is 'from coinbase.rest import RESTClient'.
Common errors
-
ModuleNotFoundError: No module named 'coinbase_advanced'
cause This error occurs when attempting to import a module named `coinbase_advanced`, which is not the correct top-level package for the official `coinbase-advanced-py` SDK.fixEnsure you have installed the correct package (`pip install coinbase-advanced-py`) and use the correct import statements, such as `from coinbase.rest import RESTClient` for REST operations or `from coinbase.websocket import WSClient` for WebSocket operations. -
401 Unauthorized
cause Authentication failed, most commonly due to an incorrect CDP API key, an improperly formatted EC private key (API secret), an expired JWT token, or missing/incorrect authentication headers.fixVerify that your API key ID (`organizations/{org_id}/apiKeys/{key_id}`) and API secret (the full multi-line EC private key string including `-----BEGIN EC PRIVATE KEY-----` and `-----END EC PRIVATE KEY-----`) are correct and properly configured. Ensure your JWT token is valid and not expired. -
403 Forbidden / Permission denied
cause The API key being used lacks the necessary permissions to perform the requested action on the Coinbase Advanced Trade platform.fixNavigate to the Coinbase Developer Platform, review the permissions associated with your API key, and ensure that they cover the specific actions you are attempting to execute (e.g., 'trade', 'wallet:accounts:read', 'wallet:deposits:create'). -
Invalid request (HTTP 400)
cause The API request is malformed, contains invalid data, or is missing required parameters for the specific endpoint being called.fixCarefully review the Coinbase Advanced Trade API documentation for the endpoint you are using to ensure all required fields and parameters are present, correctly formatted, and contain valid values, paying attention to request body schema, query parameters, path parameters, and headers.
Warnings
- breaking Package name is 'coinbase-advanced-py' but import is 'from coinbase.rest import RESTClient'. The 'coinbase' PyPI package is a completely different old SDK (Coinbase Commerce). Do not confuse them.
- breaking Coinbase Pro (cbpro) discontinued. All LLM and tutorial code using cbpro or the old Coinbase Pro API is broken. Migrate to coinbase-advanced-py.
- breaking CDP API keys use EC private key format (PEM). Old HMAC-based API keys (key + secret string) are not compatible with coinbase-advanced-py. Must generate new CDP keys.
- gotcha client_order_id must be unique per order. Setting to empty string '' auto-generates a UUID. Reusing the same client_order_id for different orders causes idempotent rejection.
- gotcha quote_size (in USD) vs base_size (in BTC) for market orders. market_order_buy uses quote_size to buy X dollars of crypto. market_order_sell uses base_size to sell X units of crypto.
Install
-
pip install coinbase-advanced-py
Imports
- RESTClient
# Wrong: old coinbase package from coinbase.wallet.client import Client client = Client(api_key, api_secret) # old Commerce SDK # Wrong: cbpro (Coinbase Pro — discontinued) import cbpro auth_client = cbpro.AuthenticatedClient(key, secret, passphrase)
from coinbase.rest import RESTClient import os # CDP API key format — EC private key client = RESTClient( api_key='organizations/{org_id}/apiKeys/{key_id}', api_secret='-----BEGIN EC PRIVATE KEY-----\nYOUR KEY\n-----END EC PRIVATE KEY-----\n' ) # Or from key file downloaded from Coinbase CDP dashboard client = RESTClient(key_file='path/to/cdp_api_key.json') # List accounts accounts = client.get_accounts() print(accounts.to_dict()) # Place market buy order order = client.market_order_buy( client_order_id='', # empty string = auto-generate product_id='BTC-USD', quote_size='10' # $10 worth of BTC )
Quickstart
# pip install coinbase-advanced-py
from coinbase.rest import RESTClient
# From CDP dashboard: Advanced Trade API key
client = RESTClient(key_file='cdp_api_key.json')
# Get accounts
accounts = client.get_accounts()
for acct in accounts['accounts']:
print(acct['name'], acct['available_balance']['value'])
# Get BTC-USD product info
product = client.get_product(product_id='BTC-USD')
print(product['price'])
# Place market buy ($10 of BTC)
order = client.market_order_buy(
client_order_id='', # auto-generate
product_id='BTC-USD',
quote_size='10'
)
print(order['success'])