Skip to main content

Advanced Trade WebSocket Authentication

This guide explains how to authenticate requests to the Advanced Trade WebSocket API server channels. It assumes that you have already created API keys on the Coinbase Developer Platform.

Sending Messages with API Keys

Making Requests

Use the code samples below to generate/export a JSON Web Token (JWT) and make an authenticated request.

info

The differences between the code snippets for WebSockets (below) and REST calls are:

  • WebSocket JWTs are not built with a request method or request path.

Generating a JWT

Regardless of which code snippet you use, follow these steps:

  1. Replace key name and key secret with your key name and private key. key secret is a multi-line key and newlines must be preserved to properly parse the key. Do this on one line with \n escaped newlines, or with a multi-line string.
  2. Run the generation script that prints the command export JWT=....
  3. Run the generated command to save your JWT.
caution

Your JWT expires after 2 minutes, after which all requests are unauthenticated.

Code samples

The easiest way to generate a JWT is to use the built-in functions in our Python SDK as described below. Otherwise, use the code samples below to generate/export a JWT and make an authenticated request.

  1. Install the SDK.

    pip3 install coinbase-advanced-py
  2. In the console, run: python main.py (or whatever your file name is).

  3. Set the JWT to that output, or export the JWT to the environment with eval $(python main.py).

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"

def main():
jwt_token = jwt_generator.build_ws_jwt(api_key, api_secret)
print(f"export JWT={jwt_token}")

if __name__ == "__main__":
main()

Sending Messages without API Keys

Subscribing

// Request
// Subscribe to ETH-USD and ETH-EUR with the level2 channel
{
"type": "subscribe",
"product_ids": [
"ETH-USD",
"ETH-EUR"
],
"channel": "level2"
}

Unsubscribing

// Request
{
"type": "unsubscribe",
"product_ids": [
"ETH-USD",
"ETH-EUR"
],
"channel": "level2"
}

Sequence Numbers

Most feed messages contain a sequence number. Sequence numbers are increasing integer values for each product, with each new message being exactly one sequence number greater than the one before it.

Sequence numbers that are greater than one integer value from the previous number indicate that a message has been dropped. Sequence numbers that are less than the previous number can be ignored or represent a message that has arrived out of order.

In either situation you may need to perform logic to make sure your system is in the correct state.

caution

Even though a WebSocket connection is over TCP, the WebSocket servers receive market data in a manner that can result in dropped messages. Your feed consumer should be designed to handle sequence gaps and out of order messages, or should use channels that guarantee delivery of messages.

tip

To guarantee that messages are delivered and your order book is in sync, consider using the level2 channel.

See Also:

Was this helpful?