Connect
Offerkalshi.predictions.sbe.v1 in the Sec-WebSocket-Protocol header when opening the connection. Sign the request with your API key using the path for the endpoint.
Before subscribing, verify that the HTTP 101 response returns
Sec-WebSocket-Protocol: kalshi.predictions.sbe.v1. For a JSON connection, omit the subprotocol. See API environments and endpoints and the WebSocket authentication example.
Supported channels
Private updates follow the same account permissions and subaccount restrictions as JSON. The server checks a subscription’s channels in request order. If a channel is unsupported (code 8) or a market-data-only key requests a private channel (code 9), the first error applies to the whole request. Use a JSON connection for other channels.
Message types
Compression is negotiated independently using
permessage-deflate. When enabled, the WebSocket library decompresses and reassembles the message before SBE decoding. For heartbeat timing, see connection keep-alive.
Decode a message
Each message starts with an eight-byte, little-endian header containingblockLength, templateId, schemaId, and version, in that order. Each field is an unsigned 16-bit integer. Schema ID and version are both 1. Select the decoder by template ID and pass it the received block length and version. Repeating groups and variable-length fields follow the fixed block in schema order.
The delivery fields occupy the first 32 bytes after the header in every v1 template.
Published v1 layouts are stable. Additional channels can introduce new template IDs. An incompatible layout requires a new schema and subprotocol.
Field conventions
Python example
This example subscribes to public trades and reads the contract count and YES price from template 4. Pass the signed headers from the WebSocket quick start and the market ticker you want to follow.Sequence gaps and reconnects
Trackseq by sid, including sequenced JSON responses on the same connection. If a book update is missing or cannot be decoded, request a fresh snapshot for every market on the affected subscription before applying subsequent deltas.
If overlapping snapshots leave the server unable to establish which queued orders belong to the book, it closes the affected subscription with code 10.
After a terminal channel error (code 10 or 25), wait for the matching unsubscribed message and subscribe again. After a disconnect, reconnect with backoff, verify the negotiated subprotocol, and rebuild subscriptions using the new SIDs. Fills and user-order updates are unsequenced. Reconcile them against your authenticated REST fills and orders after reconnecting. See order book updates and WebSocket connection errors.