Subscribing & Unsubscribing

Once connected to the WebSocket API you can subscribe to various feeds using the subscribe or unsubscribe commands.

These commands should be JSON with the following properties:

Property
Type
Description

type

string

Command you are sending to the WebSocket (subscribe or unsubscribe).

feeds

string[]

List of the feeds that should be subscribed/unsubscribed to/from, respectively.

Subscribing

To begin receiving feed messages, you must send a subscribe message to the server indicating which feeds to receive. This message is mandatory—you may be disconnected if no subscribe has been received.

To ensure optimal performance, we suggest maintaining a separate Websocket connection for market data subscriptions (orderbooks, tickers, trades) to ensure high-frequency market data doesn't impact the delivery of your time-sensitive account level updates, such as balances or open-orders messages.

Subscribe Message

// Request -> Subscribe to feed(s)
{
    "type": "subscribe", 
    "feeds": ["<feed 1>", "<feed 2>", ...]
}

Once a subscribe message is received the server responds with a subscribe message that lists all feeds you are subscribed to. Subsequent subscribe messages add to the list of subscriptions.

// Response -> Subscribe request
{
    "type": "success",
    "sequence": 2,
    "timestamp": 1727392002151355776,
    "payload": {
        "action": "subscribe",
        "feeds": [
            "<feed 1>",
            "<feed 2>",
            ...
        ]
    },
    "action": "subscribe"
}

Example Code

const subscribeMsg = {
  type: 'subscribe',
  feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(subscribeMsg));

Unsubscribing

To unsubscribe from a feed or feeds, send an unsubscribe message. The structure is equivalent to subscribe messages.

Unsubscribe Message

// Request -> Unsubscribe to feed(s)
{
    "type": "unsubscribe", 
    "feeds": ["<feed 1>", "<feed 2>", ...]
}

Once a unsubscribe message is received the server responds with an unsubscribe message that lists all feeds you are unsubscribed from.

// Response -> Subscribe request
{
    "type": "success",
    "sequence": 4,
    "timestamp": 1727394717752870042,
    "payload": {
        "action": "unsubscribe",
        "feeds": [
            "<feed 1>",
            "<feed 2>",
            ...
        ]
    },
    "action": "unsubscribe"
}

Example Code

const unsubscribeMsg = {
  type: 'unsubscribe',
  feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(unsubscribeMsg))

Last updated