Skip to main content
One WebSocket carries every channel you subscribe to, for one key, its trading account and its mode. For reconnecting and rebuilding state, see the Streaming guide.

Connect

Frames you send must not carry fields other than the ones shown on this page. Channels always follow the ticket’s account and mode; no frame can name another.

Get a ticket

Your API key never travels over the socket. Exchange it for a ticket with Create a stream ticket:
A ticket opens one connection and is good for 60 seconds, or until the key expires if that is sooner. Request a new one for each connection, including every reconnect.

Authenticate

Within 10 seconds of opening the socket, send the ticket as the first frame:
Any other frame before auth closes the socket; see Before authentication. A second auth frame on the same connection is answered with a VALIDATION_ERROR error frame and the connection stays open.

Subscribe and unsubscribe

Name one or more channels:
The reply lists the channels you named, each once:
To stop channels, send the same shape with unsubscribe. The reply is an unsubscribed frame listing the channels you named, each once:
Subscribing to a channel you already hold does not add it twice. These are answered with a VALIDATION_ERROR error frame and the connection stays open:

Channels

orders

Sends each state of the account’s orders that happens after subscribed. Each order’s states only move forward. Call GET /v1/orders once after subscribed to cover anything earlier.

positions

Each frame is the whole state and replaces the one before it. The newest state arrives first, then each change. Frames follow every order change on the account and are refreshed about every ten seconds otherwise.

balances

Sent and refreshed the same way as positions.

book

Takes a market_id: a market ID or a venue symbol. With a market ID, each frame holds the market’s book across every venue that lists it; with a venue symbol, only that symbol’s listing. combined merges the venues’ levels by price, at a depth of 20 levels per side. The channel opens with a book frame holding the whole book, then sends book_delta frames holding only what changed since the book Lean last published. A busy book can get several deltas within a few milliseconds or one delta covering many changes. A book with no change gets a delta with no levels about once a second. Every book and book_delta frame has a sequence. A delta also has prev_sequence: apply it only when that equals the sequence of the book you hold, then hold its sequence. When a delta cannot follow the frame before it, because your session read too slowly or the market’s venues changed, Lean sends a whole book frame instead, which replaces the book you hold. If you still see a delta whose prev_sequence is not yours, drop the book, send unsubscribe for the channel, then subscribe again: the new subscription opens with a whole book frame. The official SDKs do all of this and hand you the whole book after each frame. The frame’s top-level market_id is the one you subscribed with, so a session following one market under two names can tell their frames apart. book.market_id is always the market ID. book.as_of is when Lean built the book. The first frame after you subscribe has snapshot set to true: it is the newest book Lean already holds, so on a quiet book its as_of can be up to about a second before you subscribed. Every later book frame has snapshot set to false, and book_delta frames carry no snapshot field; both are built as they are sent. To measure delay, skip the snapshot frame. Subscribing again to a channel you already have changes nothing, so it sends no new snapshot; unsubscribe first to get one.
A book_delta frame lists only the levels that changed:
  • Each level replaces the level at its price on its side. A quantity of "0" removes the level.
  • A level that falls out of the 20 shown is removed, and one that comes into them is added, so the book you build always matches the next whole book.
  • A combined level carries its whole venues split, not only the venue that changed.
  • A venue book is matched by venue, ticker and side. It lists only its changed levels, with its new quantity and notional totals.
  • The four book totals and as_of are always the new book’s.
  • Keep bids highest price first and asks lowest price first.
Quantities keep the venue’s precision, as GET /v1/markets/:id/book returns them, so a level holding a fraction of a share is listed and can be the best price. Every listed level holds more than 0, so "0" only ever means a removal.
When a book channel will send no more books, it sends one book_ended frame and the session stops following it. reason is unknown_market when the market_id names no market, or is a venue symbol that names more than one market, such as the ticker of a listing that pays both outcomes. The REST endpoints refuse that symbol with 400; the channel ends instead. reason is market_closed once every venue listing of the market is closed or resolved. unknown_market arrives within a few seconds. market_closed arrives after Lean records the close, which can be a few minutes after the venue closes the market. A market whose listings are unopened or resolving still streams its book.
A market listed after you subscribed, such as an upcoming crypto window, can end with unknown_market. Subscribe again once List markets returns it.

trades

Name a venue listing by its leg ticker, as a market’s legs[].ticker shows it:
Each frame holds the trades the venue reported since the last frame, oldest first. Only trades that happen after subscribed are sent; read List recent market trades for earlier ones. Live and test keys get the same trades.

market_lifecycle

Name a venue listing the same way:
A status_change frame arrives when the listing’s status changes. status is unopened, open, closed, resolving or resolved, and result names the outcome once the venue reports one, else null:
A tick_size_change frame arrives when the listing’s price step changes. tick_rule is cent, half_cent, tenth, hundredth or tapered, and tick_size is the exact step in dollars when the venue gives one, else null:
The first frame after subscribed can repeat the listing’s current state. New event values can appear without a new version; skip one you do not recognize. Only a ticker’s shape is checked: one no venue lists is accepted and sends nothing. Unlike a book channel’s market_id, a Polymarket condition ID, token ID or Hyperliquid coin is not resolved to a ticker. A session that reads positions, balances or books too slowly skips ahead to the newest state. A session that reads trades or lifecycle events too slowly is closed with 1013 and RATE_LIMITED, with the message Stream fell behind; reconnect and refetch state.

Keep the connection alive

Send {"op":"ping"} every 10 seconds; the reply is {"type":"pong"}. If no frame of any kind, pong included, arrives for 25 seconds, close the socket and reconnect with a new ticket.

Errors

Every error is an error frame with a code and a message. Whether the connection stays open depends on whether it has authenticated.

Before authentication

Until authenticated arrives, Lean accepts only an auth frame. Anything else gets one error frame, then the socket closes: An auth frame whose ticket or key is refused also closes with 1008 and UNAUTHORIZED: the ticket is invalid, expired or already used, or the key is expired or revoked. The closes that can follow an auth frame for other reasons are in Close codes.

After authentication

A bad frame is answered with an error frame and the connection stays open:
Unrecognized frame answers text that is not JSON or not a frame shown on this page. A second auth frame gets This connection is already authenticated, and a bad subscribe gets one of the subscribe errors. A binary frame still closes the socket with 1003.

Close codes

When the connection must end, Lean sends an error frame and then closes the socket: Lean sends a WebSocket ping every 15 seconds, and any frame from the client counts, a pong included. Most WebSocket libraries answer pings on their own; sending {"op":"ping"} every 10 seconds also keeps the connection open. After a 1001, 1012 or 1013 close, reconnect with a new ticket, subscribe again and call GET /v1/orders. After a 1008 close, the ticket or key was refused: try once more with a new ticket and stop after three 1008 closes in a row. The Streaming guide covers the full reconnect plan.