Documentation Index
Fetch the complete documentation index at: https://docs.agg.market/llms.txt
Use this file to discover all available pages before exploring further.
WebSocket Protocol
Connect to the AGG WebSocket API to receive live aggregated orderbook updates, public trades, JSON heartbeat events, and authenticated user events across connected venues.Getting connected
Open a connection
Pass your If the user is already signed in, you can also include
appId as a query parameter.&token=eyJ... for immediate
user-level authentication.Wait for the connected message
The server acknowledges the connection with app and optional user context:
userId is present only when a valid JWT is supplied on connect.A single connection supports up to 100 total subscriptions across orderbook and trade
channels. Requests beyond that limit return an
error message.1. Orderbook stream
Subscribe to an outcome’s aggregated orderbook to receive a full snapshot followed by incremental deltas. Each outcome has its own orderbook — there is no complement derivation. Aggregated levels include per-venue attribution. Per-venue books are also included.Subscribe
Initial snapshot
Incremental deltas
Sequencing and resync
Every orderbook message includes a monotonicseq value for that outcome. Your client is
responsible for maintaining local book state and detecting integrity issues. The gateway is a
stateless fan-out — it forwards deltas from the aggregation engine without server-side seq tracking.
Apply deltas when delta.seq === book.seq + 1. After applying, recompute the checksum
(XOR of per-level CRC32 values) and compare it to checksum in the message. If they match, the
book is consistent.
Drop stale deltas where delta.seq <= book.seq — these can arrive after a subscribe when
Kafka deltas predate the initial snapshot. Silently discard them.
Request a resnapshot if you detect a forward gap (delta.seq > book.seq + 1) or a checksum
mismatch:
Raw WebSocket example
This keeps the protocol logic visible without pulling in SDK helpers:2. Trade stream
Subscribe to the public trade feed for an outcome. No user auth is required.Subscribe
Trade event
Unsubscribe
3. Authenticated user events
Receive user-specific events, such as order confirmations and balance updates, after the connection has user-level auth.Authenticate
- On connect
- Mid-session
Include the JWT in the connection URL:
Order submitted
Balance update
Order event
Venue-agnostic stream of DAG lifecycle and terminal order status events. Emitted for every venue (Polymarket, Kalshi, dFlow, …) automatically — no per-venue handling on the client. Discriminate on theevent field.
Step lifecycle (progress UI)
event values: step_started | step_completed | step_waiting | step_failed | step_retrying.
sequence is the 1-based declaration index of the step within the DAG, and totalSteps is the total step count. Pair them to render progress like “Step 3 of 7”. dag_started also carries totalSteps so the UI can show an empty progress bar before the first step runs. For branching DAGs, sequence reflects declaration order, not strict execution order — parallel branches may emit non-monotonic sequence values.
Terminal fill (success toast + position refresh)
event values: filled | partial_fill. remainingAmountRaw is present only on partial_fill.
Failure (error toast)
event values: failed (order-level) | dag_failed (DAG-level).
DAG lifecycle (optional)
event values: dag_started | dag_completed | dag_failed | dag_cancelled.
All
order_event messages are broadcast verbatim — the gateway does not filter by event type. Switch on event and ignore types your UI doesn’t care about.Connection management
Heartbeat
The service emits a JSON heartbeat event that clients can observe directly:Use the JSON
heartbeat event if you want an app-level liveness timer in a raw client.Reconnection
If the connection closes, reconnect with exponential backoff and re-subscribe to your channels:Error handling
Protocol or validation errors arrive as JSON messages:| Code | Meaning |
|---|---|
4001 | Missing appId query parameter |
4003 | Invalid app or token scope not accepted |
1001 | Connection closed; reconnect with backoff |
Related
Setup Guide
Provider wiring, auth setup, and SDK client initialization.
Real-Time Orderbook
SDK, hooks, and UI orderbook implementations.
Real-Time Charts
Build live candles from REST history plus WebSocket events.
User Notifications
Authenticated order and balance events with refresh-aware reconnect logic.