For most applications, start with
OrderbookEngine.
Managed local book
SetTOKEN_ID to a Polymarket outcome token ID. You can obtain token IDs from V3 market data or the clobTokenIds / clob_token_ids field on a short-form rotation.
- TypeScript
- Python
- Rust
subscribe() resolves after the server acknowledges the requested identifiers. The local book becomes readable after its initial snapshot arrives. TypeScript and Python emit ready when every acknowledged token has a valid baseline.
Book update rules
Apply raw messages with these rules:
Prices and sizes are decimal strings. Keep them as strings or use a decimal library for exact calculations. Converting them to binary floating point can change level identity and is incompatible with PN1 checksum verification.
Enable PN1 integrity
PN1 lets the SDK detect a missing or out-of-order depth message instead of continuing with a book that merely looks plausible. It validates:- a trusted anchor snapshot for each token
- sequence continuity across depth changes
- the checksum after every verified mutation
- TypeScript
- Python
- Rust
Fail-closed lifecycle
WithallowStaleReads / allow_stale_reads left at its default false, the local book moves through these states:
On a sequence or checksum failure, the engine gates only the affected token and requests a fresh anchor. It does not serve the last book as verified state.
Set stale reads to
true only when your application deliberately prefers availability over verified depth. Surface that choice to downstream consumers; a stale book should not be mistaken for current market state.
Reconnects
Orderbook reconnect is automatic by default. The SDK replays the active token set and waits for fresh snapshots. Sequence state from the old connection is never reused as if it belonged to the replacement connection. Application handlers remain attached across reconnects. You do not need to register them again.Filtered views
Views share the engine’s connection and state but receive updates only for their token set.- TypeScript
- Python
- Rust
Raw stream
Choose the raw client when you need protocol messages or maintain state elsewhere:- TypeScript:
pn.orderbook.subscribe(tokenIds)andpn.orderbook.on(...) - Python:
await pn.orderbook.subscribe(token_ids)andpn.orderbook.on(...) - Rust:
client.orderbook_stream(ObStreamOptions::default()).await?
LocalOrderbook provides the same deterministic update logic without the higher-level engine.
Cleanup
- TypeScript
- Python
- Rust

