Skip to main content

Coinbase Advanced API Python SDK

Project description

Coinbase Advanced API Python SDK

PyPI version License Code Style

Welcome to the official Coinbase Advanced API Python SDK. This python project was created to allow coders to easily plug into the Coinbase Advanced API.

Installation

pip3 install coinbase-advanced-py

Cloud API Keys

This SDK uses the Coinbase Cloud API keys. To use this SDK, you will need to create a Coinbase Cloud API key and secret by following the instructions here. Make sure to save your API key and secret in a safe place. You will not be able to retrieve your secret again.

WARNING: We do not recommend that you save your API secrets directly in your code outside of testing purposes. Best practice is to use a secrets manager and access your secrets that way. You should be careful about exposing your secrets publicly if posting code that leverages this library.

Optional: Set your API key and secret in your environment (make sure to put these in quotation marks). For example:

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

In your code, import the RESTClient class and instantiate it:

from coinbase.rest import RESTClient

client = RESTClient() # Uses environment variables for API key and secret

If you did not set your API key and secret in your environment, you can pass them in as arguments:

from coinbase.rest import RESTClient

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)

After creating your API key, a json file will be downloaded to your computer. It's possible to pass in the path to this file as an argument:

client = RESTClient(key_file="path/to/coinbase_cloud_api_key.json")

We also support passing a file-like object as the key_file argument:

from io import StringIO
client = RESTClient(key_file=StringIO('{"name": "key-name", "privateKey": "private-key"}'))

You can also set a timeout in seconds for your REST requests like so:

client = RESTClient(api_key=api_key, api_secret=api_secret, timeout=5)

Using the Client

You are able to use any of the API hooks to make calls to the Coinbase API. For example:

from json import dumps

accounts = client.get_accounts()
print(dumps(accounts, indent=2))

order = client.market_order_buy(client_order_id="clientOrderId", product_id="BTC-USD", quote_size="1")
print(dumps(order, indent=2))

This code calls the get_accounts and market_order_buy endpoints.

Refer to the Advanced API Reference for detailed information on each exposed endpoint. Look in the coinbase.rest module to see the API hooks that are exposed.

Passing in additional parameters

Use kwargs to pass in any additional parameters. For example:

kwargs = {
    "param1": 10,
    "param2": "mock_param"
}
product = client.get_product(product_id="BTC-USD", **kwargs)

Generic REST Calls

You can make generic REST calls using the get, post, put, and delete methods. For example:

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

portfolio = client.post("/api/v3/brokerage/portfolios", data={"name": "TestPortfolio"})

Here we are calling the GetMarketTrades and CreatePortfolio endpoints through the generic REST functions. Once again, the built-in way to query these through the SDK would be:

market_trades = client.get_market_trades(product_id="BTC-USD", limit=5)

portfolio = client.create_portfolio(name="TestPortfolio")

Authentication

Authentication of Cloud API Keys is handled automatically by the SDK when making a REST request.

However, if you wish to handle this yourself, you must create a JWT token and attach it to your request as detailed in the Cloud API docs here. Use the built in jwt_generator to create your JWT token. For example:

from coinbase import jwt_generator

api_key = "organizations/{org_id}/apiKeys/{key_id}"
api_secret = "-----BEGIN EC PRIVATE KEY-----\nYOUR PRIVATE KEY\n-----END EC PRIVATE KEY-----\n"

uri = "/api/v3/brokerage/orders"

jwt_uri = jwt_generator.format_jwt_uri("POST", uri)
jwt = jwt_generator.build_rest_jwt(jwt_uri, api_key, api_secret)

This will create a JWT token for the POST /api/v3/brokerage/orders endpoint. Pass this JWT token in the Authorization header of your request as: "Authorization": "Bearer " + jwt

You can also generate JWTs to use with the Websocket API. These do not require passing a specific URI. For example:

from coinbase import jwt_generator

api_key = "organizations/{org_id}/apiKeys/{key_id}"
api_secret = "-----BEGIN EC PRIVATE KEY-----\nYOUR PRIVATE KEY\n-----END EC PRIVATE KEY-----\n"

jwt = jwt_generator.build_ws_jwt(api_key, api_secret)

Use this JWT to connect to the Websocket API by setting it in the "jwt" field of your subscription requests. See the docs here for more details.

Changelog

For a detailed list of changes, see the Changelog.

Contributing

If you've found a bug within this project, open an issue on this repo and add the "bug" label to it. If you would like to request a new feature, open an issue on this repo and add the "enhancement" label to it. Direct concerns or questions on the API to the Advanced API Developer Forum.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coinbase-advanced-py-1.0.4.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coinbase_advanced_py-1.0.4-py3-none-any.whl (27.5 kB view details)

Uploaded Python 3

File details

Details for the file coinbase-advanced-py-1.0.4.tar.gz.

File metadata

  • Download URL: coinbase-advanced-py-1.0.4.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for coinbase-advanced-py-1.0.4.tar.gz
Algorithm Hash digest
SHA256 179e9c845d96141513ab6068eec0fc4f6e3f3d6b508c74feca294fd1fd6a9ba4
MD5 cd54f11f399dad9f1bf825e5acf61e40
BLAKE2b-256 daa1b86d4eeb7c879e24e27f073211c00cd11b10782ef0edccc87975d5cdde02

See more details on using hashes here.

File details

Details for the file coinbase_advanced_py-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for coinbase_advanced_py-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d4e91a20e89ace1209bc06a0107b1095de3146b84fa64f0f5a4c34d593e3bcd3
MD5 109fc801516d2053a68ca830af4f04dc
BLAKE2b-256 81482068d569b725ffb0a2f9de6f916e5db519fe3a1f544e069b165051386cf4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page