# Order Book

Use this stream for live order book updates by event.

### Endpoint

```
wss://order-book.outpoll.com/event/ws
```

### Authentication

No authentication required.

### Message envelope

Every message uses the same top-level fields:

```json
{
  "t": "<TYPE>",
  "e": "<EVENT_ID>",
  "d": [...],
  "r": "<ERROR_MESSAGE>"
}
```

| Field | Type   | Description                    |
| ----- | ------ | ------------------------------ |
| `t`   | string | Message type                   |
| `e`   | string | Event ID                       |
| `d`   | array  | Outcome price payload          |
| `r`   | string | Error message for failed calls |

### Message types

| Type                | Direction | Description              |
| ------------------- | --------- | ------------------------ |
| `SUBSCRIBE`         | Client    | Subscribe to one event   |
| `SUBSCRIBED`        | Server    | Subscription confirmed   |
| `UNSUBSCRIBE`       | Client    | Remove one subscription  |
| `UNSUBSCRIBED`      | Server    | Unsubscribe confirmed    |
| `REQUEST_DATA`      | Client    | Request a full snapshot  |
| `PRICES_DATA`       | Server    | Full price snapshot      |
| `PRICES_DATA_DELTA` | Server    | Incremental price update |
| `PING`              | Server    | Heartbeat                |
| `ERROR`             | Server    | Request error            |

### Subscribe

```json
{
  "t": "SUBSCRIBE",
  "e": "54ccea1a-16fd-469c-8018-84b375243e8a"
}
```

Server confirmation:

```json
{
  "t": "SUBSCRIBED",
  "e": "54ccea1a-16fd-469c-8018-84b375243e8a"
}
```

### Price payload

`PRICES_DATA` sends the current state.

`PRICES_DATA_DELTA` sends only changed outcomes.

```json
{
  "t": "PRICES_DATA",
  "e": "54ccea1a-16fd-469c-8018-84b375243e8a",
  "d": [
    {
      "o": "b21f6fd8-b9d1-4b9f-bb79-ef141e3dcb76",
      "c": 0.55,
      "b": 0.54,
      "a": 0.56
    }
  ]
}
```

| Field | Type   | Description |
| ----- | ------ | ----------- |
| `o`   | string | Outcome ID  |
| `c`   | number | Last chance |
| `b`   | number | Best bid    |
| `a`   | number | Best ask    |

### Python example

{% code title="order\_book.py" %}

```python
import asyncio, json, websockets

async def order_book():
    uri = "wss://order-book.outpoll.com/event/ws"
    async with websockets.connect(uri) as ws:
        await ws.send(json.dumps({
            "t": "SUBSCRIBE",
            "e": "54ccea1a-16fd-469c-8018-84b375243e8a",
        }))

        async for msg in ws:
            data = json.loads(msg)
            if data["t"] in ("PRICES_DATA", "PRICES_DATA_DELTA"):
                for price in data["d"]:
                    print(price["o"], price["c"], price["b"], price["a"])
```

{% endcode %}
