> For the complete documentation index, see [llms.txt](https://docs.sfox.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sfox.com/websocket-api/subscribing-and-unsubscribing.md).

# Subscribing & Unsubscribing

Subscribing or unsubscribing to WebSocket feeds

Once [connected](/websocket-api/connecting.md) to the WebSocket API you can subscribe to various feeds using the `subscribe` or `unsubscribe` commands.&#x20;

These commands should be JSON with the following properties:

<table><thead><tr><th width="116">Property</th><th width="100">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>type</code></td><td>string</td><td>Command you are sending to the WebSocket (<code>subscribe</code> or <code>unsubscribe</code>).</td></tr><tr><td><code>feeds</code></td><td>string[]</td><td>List of the feeds that should be subscribed/unsubscribed to/from, respectively.</td></tr></tbody></table>

### 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.

{% hint style="info" %}
To ensure optimal performance, we suggest maintaining a separate Websocket connection for market data subscriptions ([orderbooks](/websocket-api/market-data/order-book.md), [tickers](/websocket-api/market-data/ticker.md), [trades](/websocket-api/market-data/trades.md)) to ensure high-frequency market data doesn't impact the delivery of your time-sensitive account level updates, such as [balances](/websocket-api/orders-and-account-data/balances.md) or [open-orders](/websocket-api/orders-and-account-data/orders.md) messages.
{% endhint %}

#### Subscribe Message

```json
// 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.

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

#### Example Code

{% tabs %}
{% tab title="NodeJS" %}

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

{% endtab %}

{% tab title="Python" %}

```python
subscribe_msg = {
    "type": "subscribe",
    "feeds": ["ticker.sfox.btcusd"],
}

await ws.send(json.dumps(subscribe_msg))
```

{% endtab %}
{% endtabs %}

### Unsubscribing

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

#### Unsubscribe Message

```json
// 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.

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

#### Example Code

{% tabs %}
{% tab title="NodeJS" %}

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

{% endtab %}

{% tab title="Python" %}

```python
unsubscribe_msg = {
    "type": "unsubscribe",
    "feeds": ["ticker.sfox.btcusd"],
}

await ws.send(json.dumps(unsubscribe_msg))
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sfox.com/websocket-api/subscribing-and-unsubscribing.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
