> ## Documentation Index
> Fetch the complete documentation index at: https://pond.dflow.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Orderbook Channel

> Subscribe to real-time orderbook updates for prediction markets

The `orderbook` channel streams real-time orderbook updates for prediction markets. Use this channel to build orderbook visualizations, depth charts, and market depth analysis tools.

## Connection

See the [WebSocket overview](/resources/metadata-api/websockets/overview#connection) for connection setup and endpoint details.

## Subscribe

### Subscribe to All Orderbooks

```json theme={null}
{
  "type": "subscribe",
  "channel": "orderbook",
  "all": true
}
```

### Subscribe to Specific Tickers

```json theme={null}
{
  "type": "subscribe",
  "channel": "orderbook",
  "tickers": ["BTCD-25DEC0313-T92749.99", "SPX-25DEC0313-T5000"]
}
```

## Unsubscribe

### Unsubscribe from All Orderbooks

```json theme={null}
{
  "type": "unsubscribe",
  "channel": "orderbook",
  "all": true
}
```

### Unsubscribe from Specific Tickers

```json theme={null}
{
  "type": "unsubscribe",
  "channel": "orderbook",
  "tickers": ["BTCD-25DEC0313-T92749.99"]
}
```

## Response Format

When subscribed to the `orderbook` channel, you'll receive messages with the following structure:

```json theme={null}
{
  "channel": "orderbook",
  "type": "orderbook",
  "market_ticker": "BTCD-25DEC0313-T92749.99",
  "yes_bids": {
    "0.45": 1000,
    "0.44": 500,
    "0.43": 200
  },
  "no_bids": {
    "0.55": 1500,
    "0.56": 800,
    "0.57": 300
  }
}
```

### Response Fields

| Field           | Type   | Description                                                     |
| --------------- | ------ | --------------------------------------------------------------- |
| `channel`       | string | Always `"orderbook"`                                            |
| `type`          | string | Message type                                                    |
| `market_ticker` | string | Market identifier                                               |
| `yes_bids`      | object | Map of price (string) to quantity (number) for YES outcome bids |
| `no_bids`       | object | Map of price (string) to quantity (number) for NO outcome bids  |

<Note>
  The `yes_bids` and `no_bids` fields are objects where keys are price strings
  and values are quantities. The order of entries in these objects represents
  the orderbook depth.
</Note>

## Code Examples

<AccordionGroup>
  <Accordion title="Subscribe to All Orderbooks">
    ```typescript theme={null}
    // Dev endpoint — no API key required, but rate-limited.
    // For production, use your production WS URL and add:
    //   { headers: { "x-api-key": "YOUR_API_KEY" } }
    // as the second argument to new WebSocket().
    const WS_URL = "wss://dev-prediction-markets-api.dflow.net/api/v1/ws";

    const ws = new WebSocket(WS_URL);

    ws.onopen = () => {
      console.log("Connected to WebSocket");

      // Subscribe to all orderbook updates
      ws.send(
        JSON.stringify({
          type: "subscribe",
          channel: "orderbook",
          all: true,
        })
      );
    };

    ws.onmessage = (event) => {
      const message = JSON.parse(event.data);

      if (message.channel === "orderbook") {
        console.log("Orderbook update:", {
          ticker: message.market_ticker,
          yesBids: message.yes_bids,
          noBids: message.no_bids,
        });
      }
    };

    ws.onerror = (error) => {
      console.error("WebSocket error:", error);
    };

    ws.onclose = () => {
      console.log("WebSocket connection closed");
    };
    ```
  </Accordion>

  <Accordion title="Subscribe to Specific Markets">
    ```typescript theme={null}
    import "dotenv/config";

    const API_KEY = process.env.DFLOW_API_KEY;
    const WS_URL = process.env.DFLOW_PREDICTION_MARKETS_WS_URL;

    // Market tickers to subscribe to
    const marketTickers = ["BTCD-25DEC0313-T92749.99", "SPX-25DEC0313-T5000"];

    if (!WS_URL || !API_KEY) {
      throw new Error(
        "Missing websocket credentials. Set DFLOW_PREDICTION_MARKETS_WS_URL and DFLOW_API_KEY."
      );
    }

    const ws = new WebSocket(WS_URL, {
      headers: {
        "x-api-key": API_KEY,
      },
    });

    ws.onopen = () => {
      console.log("Connected to WebSocket");

      // Subscribe to specific tickers for orderbooks
      ws.send(
        JSON.stringify({
          type: "subscribe",
          channel: "orderbook",
          tickers: marketTickers,
        })
      );
    };

    ws.onmessage = (event) => {
      const message = JSON.parse(event.data);
      console.log(`Orderbook update for ${message.market_ticker}:`, message);
    };
    ```
  </Accordion>

  <Accordion title="TypeScript Interface">
    ```typescript theme={null}
    interface OrderbookUpdate {
      channel: "orderbook";
      type: string;
      market_ticker: string;
      yes_bids: Record<string, number>;
      no_bids: Record<string, number>;
    }
    ```
  </Accordion>
</AccordionGroup>

## Related

<CardGroup cols={2}>
  <Card title="Prices Channel" href="prices" icon="chart-line">
    Subscribe to real-time bid/ask price updates
  </Card>

  <Card title="Trades Channel" href="trades" icon="receipt">
    Subscribe to real-time trade execution updates
  </Card>

  <Card title="Orderbook API" href="/resources/metadata-api/orderbook/orderbook-by-ticker" icon="list">
    Get orderbook snapshots via REST API
  </Card>
</CardGroup>
