The following methods can be used to authenticate your requests to the Coinbase Developer Platform (CDP). Choose the method that best suits your needs:
Visit the CDP Authentication docs for more details.
The following example shows how to generate a JWT token, which can then be injected manually into your Authorization header as a Bearer Token to authenticate REST API requests to the CDP APIs using the HTTP request library of your choice.
Step 1: Install the required package:
pip install cdp-sdkStep 2: Generate a JWT:
from cdp.auth.utils.jwt import generate_jwt, JwtOptions
# For REST (HTTP) requests
jwt = generate_jwt(JwtOptions(
api_key_id="YOUR_API_KEY_ID",
api_key_secret="YOUR_API_KEY_SECRET",
request_method="GET",
request_host="api.cdp.coinbase.com",
request_path="/platform/v2/evm/accounts",
expires_in=120 # optional (defaults to 120 seconds)
))
print(jwt)
# For websocket connections
websocket_jwt = generate_jwt(JwtOptions(
api_key_id="YOUR_API_KEY_ID",
api_key_secret="YOUR_API_KEY_SECRET",
request_method=None,
request_host=None,
request_path=None,
expires_in=120 # optional (defaults to 120 seconds)
))
print(websocket_jwt)For information about the above parameters, please refer to the Authentication parameters section.
Step 3: Use your JWT (Bearer token) in the Authorization header of your HTTP request:
curl -L 'https://api.cdp.coinbase.com/platform/v2/evm/accounts' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer $jwt'The following example shows how to generate the required authentication headers for authenticating a request to the CDP REST APIs. These headers can be added to your request manually using the HTTP request library of your choice.
Step 1: Install the required package:
pip install cdp-sdkStep 2: Generate authorization headers:
from cdp.auth.utils.http import get_auth_headers, GetAuthHeadersOptions
# Example POST request with a body
headers = get_auth_headers(
GetAuthHeadersOptions(
api_key_id="YOUR_API_KEY_ID",
api_key_secret="YOUR_API_KEY_SECRET",
wallet_secret="YOUR_WALLET_SECRET",
request_method="POST",
request_host="api.cdp.coinbase.com",
request_path="/platform/v2/evm/accounts",
request_body={
"name": "MyAccount",
},
expires_in=120 # optional (defaults to 120 seconds)
)
)
# Prints the headers required for authentication
print(headers)For information about the above parameters, please refer to the Authentication parameters section.
The following example shows how to use a modified urllib3 request client to authenticate your requests to the CDP REST APIs. This client will automatically add the appropriate authentication headers to each request.
Step 1: Install the required packages:
pip install urllib3 cdp-sdkStep 2: Generate an HTTP request using the urllib3 request library:
from cdp.auth.clients import Urllib3AuthClient, Urllib3AuthClientOptions
# Create an authenticated HTTP client
client = Urllib3AuthClient(
Urllib3AuthClientOptions(
api_key_id="YOUR_API_KEY_ID",
api_key_secret="YOUR_API_KEY_SECRET",
wallet_secret="YOUR_WALLET_SECRET"
),
base_url="https://api.cdp.coinbase.com"
)
# Make authenticated requests (example)
# The appropriate authentication headers will be automatically added to the request
try:
response = client.request(
"POST",
"/platform/v2/evm/accounts",
body={
"name": "MyAccount",
}
)
print(response.data)
except Exception as error:
print("Request failed:", error)The Urllib3AuthClient will automatically:
- Generate a JWT for each request
- Add the JWT to the
Authorizationheader - Set the appropriate
Content-Typeheader - Add wallet authentication when required
For information about the above parameters, please refer to the Authentication parameters section.
The following table provides more context of many of the authentication parameters used in the examples above:
* Either all three request parameters (request_method, request_host, and request_path) must be provided for REST API requests, or all three must be None for JWTs intended for websocket connections.
