Introduction
Consider going through the REST SDK quickstart first as it is referenced in this tutorial.
Prerequisites
Creating API Keys
To you use the SDK, you must first create your own API key on the Coinbase Developer Platform (CDP).Installing the SDK
To install the Coinbase Advanced API Python SDK, run the following command in a terminal:Setting up your Client
Create a Python project with the following code we have set up for you. Replace theapi_key
and api_secret
with your own CDP API Key and Secret.
You must specify an on_message
function that is called when a message is received from the WebSocket API. This function takes in a single argument, which is the raw message received from the WebSocket API.
In the following example, we simply print the message to the console.
WebSocket User API Client
We offer a WebSocket User API client that allows you to connect to the Coinbase Advanced Trade WebSocket heartbeats channel, user channel and futures_balance_summary channel. In your code, import the WSUserClient class instead of WSClient.Subscribing to your First Channels
Let’s start by opening a connection to the WebSocket API. Add the following call toopen
to your code, using the same client you just created.
- The Heartbeats channel receives heartbeats messages for specific products every second, which is used to keep the connection alive.
- The Ticker channel provides real-time price updates every time a match happens for a given product.
heartbeats
and ticker
channels for the BTC-USD
product. The received message is printed to the console.
Running the Client for 10 Seconds
The above code only runs once and then exits. Use thesleep_with_exception_check
method to run the client for a specified number of seconds. This method checks for exceptions every second, and exits if an exception is raised.
Run the following code to keep the client open for 10 seconds. It prints all of the messages it receives, then closes the connection:
Running the Client Indefinitely
To keep the client open indefinitely, use therun_forever_with_exception_check
method. This method runs the client forever, and exits if an exception is raised.
Listening for Order Updates
Now let’s get a bit more… advanced! In this final section, we integrate both the REST and WebSocket APIs to:- Place a limit-buy order 5% below said price (in the REST SDK tutorial).
- Subscribe to the user channel for updates on a Limit order.
- Print when the order is filled.
Placing an Order 5% below Price
This section assumes that you used the REST SDK Client to place a limit-buy order 5% below the current price of a product.Subscribing to the User Channel
Now let’s integrate the WebSocket SDK! Let’s call the User channel to receive updates on the order. This channel sends updates on all of a user’s open orders. First, let’s define theon_message
function. This function:
- Checks for all messages from the
user
channel’ - Checks if the message is an update on our order.
- Sets the
order_filled
variable toTrue
if the order is filled.
user
and heartbeats
channels and run the client in a while
loop to wait for the order to be filled before closing the connection.
Don’t forget to add your own custom
client_order_id
. For learning purposes, we’ve pre-filled it to an arbitrary string.