The Coinbase Advanced API Python SDK simplifies interactions with the Coinbase Advanced API, handling authentication, HTTP connections, and providing intuitive methods for seamless integration. Whether you're accessing real-time market data, managing orders, or leveraging the WebSocket API, this SDK streamlines the process for developers.
👉 Explore Coinbase Advanced API
Key Features
- REST API Client: Effortlessly interact with Coinbase’s Advanced Trade API.
- WebSocket Support: Connect to real-time market data streams.
- Authentication Handling: Built-in JWT generation for secure API access.
- Custom Response Objects: Structured data retrieval with dot-notation support.
- Public Endpoints: Access market data without authentication.
Installation
Install the SDK via pip:
pip3 install coinbase-advanced-pyAuthentication
API Keys
To use the SDK, generate Coinbase Developer Platform (CDP) API keys:
- Follow the API key setup guide.
- Securely store your key and secret (the secret cannot be retrieved later).
Warning: Avoid hardcoding secrets in your application. Use environment variables or a secrets manager:
export COINBASE_API_KEY="organizations/{org_id}/apiKeys/{key_id}"
export COINBASE_API_SECRET="-----BEGIN EC PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END EC PRIVATE KEY-----\n"REST API Client
Initialization
from coinbase.rest import RESTClient
# Option 1: Use environment variables
client = RESTClient()
# Option 2: Pass keys directly
api_key = "organizations/{org_id}/apiKeys/{key_id}"
api_secret = "-----BEGIN EC PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END EC PRIVATE KEY-----\n"
client = RESTClient(api_key=api_key, api_secret=api_secret)
# Option 3: Load keys from a JSON file
client = RESTClient(key_file="path/to/cdp_api_key.json")Making API Calls
from json import dumps
# Fetch accounts
accounts = client.get_accounts()
print(dumps(accounts.to_dict(), indent=2))
# Place a market order
order = client.market_order_buy(client_order_id="clientOrderId", product_id="BTC-USD", quote_size="1")
print(dumps(order.to_dict(), indent=2))Custom Response Objects
Access fields using dot-notation:
product = client.get_product("BTC-USD")
print(product.price) # Outputs the current BTC-USD price Rate Limits
Enable rate limit headers in responses:
client = RESTClient(rate_limit_headers=True)WebSocket API Client
Initialization
from coinbase.websocket import WSClient
def on_message(msg):
print(msg)
client = WSClient(api_key=api_key, api_secret=api_secret, on_message=on_message)Subscribing to Channels
client.open()
client.subscribe(product_ids=["BTC-USD"], channels=["ticker", "heartbeats"])
time.sleep(10) # Monitor data for 10 seconds
client.close()Automatic Reconnection
The client retries failed connections (max 5 attempts) with exponential backoff. Disable with:
client = WSClient(retry=False)Public Endpoints
REST Example (Unauthenticated)
client = RESTClient()
public_products = client.get_public_products()
print(public_products.to_dict())WebSocket Example (Unauthenticated)
client = WSClient(on_message=lambda msg: print(msg))
client.ticker(product_ids=["BTC-USD"]) Debugging
Enable verbose logging:
client = RESTClient(verbose=True)
ws_client = WSClient(verbose=True)FAQ
How do I generate a JWT token for manual authentication?
from coinbase import jwt_generator
jwt = jwt_generator.build_rest_jwt("/api/v3/brokerage/orders", api_key, api_secret)What channels are available in the WebSocket API?
Refer to the WebSocket Channels documentation.
Are unauthenticated requests rate-limited?
Yes, public endpoints have stricter rate limits. Authenticate for higher throughput.
Contributing & Support
- Bugs/Issues: Open a GitHub issue with the
buglabel. - Feature Requests: Use the
enhancementlabel. - Community: Join the Advanced API Discord.
For full documentation, visit coinbase-advanced-py docs.
This guide covers the Coinbase Advanced API Python SDK in detail, ensuring you can integrate it effectively into your trading applications. For updates, refer to the Changelog.