# Subscribing & Unsubscribing

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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
