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

# Binary Margin WebSocket data

> Subscribe to Margin market data and private trading updates in Simple Binary Encoding (SBE) format.

The Margin Trade API WebSocket supports Simple Binary Encoding for perpetual futures market data and private trading updates. JSON is the default. You can generate native codecs from the [Margin SBE XML schema](https://docs.kalshi.com/margin-ws/kalshi-margin-sbe-v1.xml).

## Connect

Sign `GET /trade-api/ws/v2/margin` using your API key and offer `kalshi.margin.sbe.v1` in the `Sec-WebSocket-Protocol` request header. Verify the HTTP 101 response echoes the subprotocol before you subscribe. Omit the offer to receive JSON data.

| Production endpoint | Binary channels |
| - | - |
| `wss://external-api-margin-ws.kalshi.com/trade-api/ws/v2/margin` | All six channels below, subject to your permissions |
| `wss://margin-user-ws.kalshi.com/trade-api/ws/v2/margin` | `fill` and `user_orders` |

See [Margin API environments](https://docs.kalshi.com/margin.md) for demo endpoints. Send subscription commands as JSON text. Acknowledgments, errors and unsubscribe notices arrive as JSON text. Ping, Pong and Close use native WebSocket frames. A negotiated `permessage-deflate` extension applies independently of the data format.

## Supported channels

| Subscription channel | Template IDs | Data |
| - | - | - |
| `orderbook_delta` | 1, 2, 3 | Bid and ask snapshot, public delta, delta with your order details |
| `trade` | 4 | Public execution price, quantity, taker side and trade ID |
| `ticker` | 5 | Market statistics, including mark prices, funding and requested initial ticker snapshots |
| `fill` | 6 | Your execution, fee, post-fill position and subaccount |
| `user_orders` | 7 | Your order, remaining and filled quantities, update reason and timestamps |
| `order_group_updates` | 9 | Your order-group lifecycle and contract limits |

Market filters, account permissions and subaccount restrictions apply to both encodings. Use a JSON connection for corporate actions, market lifecycle or exchange status. Requesting an unsupported channel on a binary connection returns error code 8 for the subscription command.

## Decode a message

Each binary WebSocket message contains one SBE message. Its eight-byte little-endian header contains the four unsigned 16-bit fields `blockLength`, `templateId`, `schemaId`, and `version`. Margin uses schema ID **2** and version **1**. Dispatch by schema and template ID. For example, template 4 in the Margin schema carries a signed 64-bit price. Pass the received block length and version to the decoder.

The next 32 bytes carry the subscription ID (`sid`, uint64), subscription sequence (`seq`, uint64), server send time (`sendingTsMs`, int64), and originating command ID (`requestId`, uint64). A zero sequence denotes an unsequenced message. A zero request ID can mean that no command ID was supplied or that the client sent ID zero. Use a nonzero command ID to correlate a requested snapshot. Business fields, repeating groups and length-prefixed UTF-8 strings follow in schema order.

| Field | Interpretation |
| - | - |
| Prices and fill fees | 10,000 units equal one US dollar. Bid and ask levels carry their native Margin prices. |
| Quantities and position changes | 100 units equal one contract. Deltas and post-fill positions can be negative. |
| Ticker notionals | Cumulative volume and open-interest notionals use millionths of a US dollar. Reconstruct the signed 128-bit integer as `high × 2^64 + low`. The rolling 24-hour notional is decimal text. |
| Timestamp fields | Unix epoch milliseconds. The delivery timestamp and event timestamps have separate meanings. |
| UUIDs | 16 bytes in standard UUID byte order. Client order IDs and order-group IDs are length-prefixed UTF-8. |
| Optional integers | An explicit presence byte distinguishes a missing value from a present zero. Use the XML definitions for nullable enums and optional groups. |

## Recover a subscription

Apply book snapshots as replacements. Track `seq` by subscription ID across binary book updates and sequenced JSON responses. If a sequence is missing, request fresh snapshots for every market on that subscription. If the server cannot safely deliver a book update, it sends error 10 and ends the affected subscription. Wait for its `unsubscribed` response and subscribe again. Reconnects create new subscription IDs. Fills and user-order updates are unsequenced. Reconcile them with your authenticated REST fills and orders after reconnecting. See [Margin order book updates](https://docs.kalshi.com/margin-ws/websockets/orderbook-updates.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.