> ## Documentation Index
> Fetch the complete documentation index at: https://api.synthesis.trade/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Orderbook

> Stream real-time orderbook snapshots and delta updates for prediction markets

The Orderbook WebSocket streams real-time orderbook updates for prediction markets. Subscribe to multiple markets at once to build live trading interfaces with accurate bid/ask spreads and depth visualization.

**Endpoint:** `wss://synthesis.trade/ws/orderbook`

## Use cases

| Use Case           | Description                                                                      |
| ------------------ | -------------------------------------------------------------------------------- |
| Live price display | Show real-time best bid/ask prices derived from the top of the orderbook         |
| Event pages        | Subscribe to all markets in an event to show live prices for each outcome        |
| Depth charts       | Build orderbook depth visualizations showing liquidity at each price level       |
| Spread monitoring  | Track bid-ask spreads to identify trading opportunities or market inefficiencies |

## Subscribe

Subscribe using token IDs (for Polymarket) or market IDs (for Kalshi). You can subscribe to up to 5,000 markets at once:

```javascript theme={null}
const ws = new WebSocket("wss://synthesis.trade/ws/orderbook");

ws.onopen = () => {
    ws.send(JSON.stringify({
        type: "subscribe",
        venue: "polymarket",
        markets: [
            "21742633143463906290569050155826241533067272736897614950488156847949938836455",
            "48572956138472615938471625938471659384716593847165938471659384716593847165938"
        ]
    }));
};
```

## Initial response

Upon subscribing, you receive the full orderbook for each market:

```json theme={null}
{
    "success": true,
    "response": {
        "orderbooks": [
            {
                "venue": "polymarket",
                "orderbook": {
                    "condition_id": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
                    "token_id": "21742633143463906290569050155826241533067272736897614950488156847949938836455",
                    "bids": {
                        "0.65": "1500",
                        "0.64": "3200",
                        "0.63": "5000"
                    },
                    "asks": {
                        "0.66": "1200",
                        "0.67": "2800",
                        "0.68": "4500"
                    },
                    "best_bid": "0.65",
                    "best_ask": "0.66",
                    "hash": "a1b2c3d4e5f6",
                    "created_at": "2026-01-01T12:00:00"
                }
            }
        ]
    }
}
```

## Real-time updates

After the initial snapshot, you receive delta updates whenever the orderbook changes:

```json theme={null}
{
    "success": true,
    "response": {
        "venue": "polymarket",
        "delta": {
            "condition_id": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
            "token_id": "21742633143463906290569050155826241533067272736897614950488156847949938836455",
            "amount": "800",
            "price": "0.66",
            "side": "BUY",
            "best_bid": "0.66",
            "best_ask": "0.67",
            "hash": "f6e5d4c3b2a1",
            "created_at": "2026-01-01T12:00:05"
        }
    }
}
```

## Complete example: Event page

Build a live event page that shows prices for all markets in an event:

```javascript theme={null}
const eventResponse = await fetch(
    "https://synthesis.trade/api/v1/polymarket/market/event/democratic-presidential-nominee-2028"
);
const { response: event } = await eventResponse.json();

const tokenIds = event.markets.flatMap(market => [
    market.left_token_id,
    market.right_token_id
]);

const ws = new WebSocket("wss://synthesis.trade/ws/orderbook");
const orderbooks = new Map();

ws.onopen = () => {
    ws.send(JSON.stringify({
        type: "subscribe",
        venue: "polymarket",
        markets: tokenIds
    }));
};

ws.onmessage = (event) => {
    const message = JSON.parse(event.data);
    if (!message.success) return;
    
    const data = message.response;
    
    if (data.orderbooks) {
        data.orderbooks.forEach(({ orderbook }) => {
            orderbooks.set(orderbook.token_id, orderbook);
        });
        updateUI();
        return;
    }
    
    if (data.delta) {
        const existing = orderbooks.get(data.delta.token_id);
        if (existing) {
            existing.best_bid = data.delta.best_bid;
            existing.best_ask = data.delta.best_ask;
        }
        updateUI();
    }
};

function updateUI() {
    event.markets.forEach(market => {
        const yesBook = orderbooks.get(market.left_token_id);
        if (yesBook) {
            const yesPrice = yesBook.best_ask || "0";
            document.getElementById(`price-${market.condition_id}`).textContent = yesPrice;
        }
    });
}
```

<Tip>For Polymarket, each market has two token IDs (left/right or YES/NO). Subscribe to both to show complete pricing. The best bid on YES roughly equals 1 minus the best ask on NO.</Tip>
