API reference
WebSocket stream
Subscribe to events or races and receive every price change as it happens. 100 tokens per connection-minute. Resume after a drop without missing a move.
/odds/v1/wsConnect
Connect to wss://api.b337.ai/odds/v1/ws with X-API-Key (or ?api_key=). Your own key only: the demo key is refused. Every frame is one JSON object. The first minute is charged before the server answers; then it sends hello:
server_epoch identifies this server run; keep it for resuming. limits.max_subs_per_connection (100) is how many events or races one connection can hold; max_subs and max_connections are your plan's caps per key.
Tokens
A connection costs 100 tokens per minute, charged at the start of each minute, whatever it carries (up to 100 events or races). It draws on the same allowance and top-up balance as the REST calls. If the connection closes mid-minute, the unused part is refunded, rounded down: a minute you started is never free, so reconnecting every 59 seconds still pays. Each new minute is announced by a tokens frame:
When the allowance and top-up balance cannot pay the next minute, the server closes with code 4402 and reason tokens_exhausted (HTTP 402 for sockets). Top up, or wait for resets_at, then reconnect. If the meter cannot be reached, the stream keeps running, the frames say "metering": "unavailable" and that minute is not counted.
Polling one event every 5 seconds is 12 tokens a minute, so streaming is cheaper from about 9 events on one connection (from 2 on the extended tier), and at the 50-event cap of Starter and Developer it is 2 tokens per event-minute (6 times cheaper than polling); on Enterprise at 100 events it is 1 (12 times cheaper). See Tokens.
Subscribe
You send:
markets, books, tier, format and depth are optional (defaults: all, all, main, decimal, false). depth: true adds exchange ladders (back_ladder, lay_ladder, matched) to snapshots and changes, as depth=true does on /odds. Re-subscribing an event replaces its filters. The event and connection caps are on Limits.
Racing
Subscribe to races with race_ids instead of event_ids. A race_id is the id /racing/meetings lists and /racing/race takes. event_ids and race_ids in the same op is a bad_request; send two ops on one connection to follow both. Races count toward the same 100 per connection and the same per-key caps.
books, format and resume work as for events. You get a snapshot that is exactly the /racing/race body for your books and format (plus type, race_id and seq), a subscribed frame listing race_ids (rejections carry a race_id and a reason), then changes frames about once a second, only for what moved:
fieldis the dotted path of the value in the/racing/racebody, for examplebooks.tab.win,books.bet365.placeor the Betfair back and lay paths. Apply it to your last snapshot atrunnerandfield.runneris the runner number;nullmeans a race-level field such asrace_stateorresult.value: nullmeans the value is gone.- Prices arrive in the format you subscribed with.
seq,resume,snapshot_required(carryingrace_id) and the close codes work as for events; keyfrom_seqbyrace_id.
Stream when you follow about 9 or more races at once, for example every AU race in the pre-jump window on a busy afternoon: one stream, 100 tokens a minute, instead of polling each race every 5 seconds. For a few races, polling every 5 seconds is cheaper.
Server frames
snapshot: one per subscribed event, the full/oddsbody for your filters (incl.open_odds/close_odds,urlandbooks[].stale), plus"type": "snapshot"and itsseq.subscribed:{"type": "subscribed", "event_ids": [...], "tier": "main", "rejected": [...]}, where each rejection carries anevent_idand areason. After a resume,resumedlists the events that were replayed.changes: only what moved, as soon as we see it (we check every second). Each carries aseq.snapshot_required: your resume could not be honoured; a freshsnapshotfollows straight away. See Resume after a disconnect.event_removed: the event is no longer listed (finished or withdrawn); it is unsubscribed for you.tokensevery minute (see Tokens),heartbeatevery 15s,pongfor yourping,unsubscribed, anderror({"type": "error", "code": "...", "message": "..."}).
A changes frame
odds: null: that book no longer prices the selection.- A selection you have not seen before carries a
selectionobject with itsname,label,player,line,direction,periodandmarket_name. - A frame with an empty
changeslist and a newstatusmeans the event went in-play. changesframes do not repeatopen_odds/close_odds; those come in snapshots.- A
booksobject appears only when a book'sstaleflag flipped. See Stale books.
Applying changes
Apply changes to the last snapshot by (market, selection_key, book). If you reconnect, subscribe again: with resume you get only what you missed, without it you get fresh snapshots to replace your state with.
Resume after a disconnect
Every snapshot and changes frame carries seq: a number per event (and tier) that goes up by one each time we see that event move. Keep, per event, the seq of the last frame you applied, and the server_epoch from hello. When you reconnect, send the same subscribe (same tier, markets, books and format) with resume:
For each event in from_seq the server does one of two things.
Replay
It sends the changes frames you missed, in order, each with "replayed": true, and no snapshot. The event is listed in subscribed.resumed. If you missed nothing, no frames at all. Live frames follow.
snapshot_required
It cannot replay, so it sends a snapshot_required frame immediately followed by a fresh snapshot. Replace your state for that event with it.
| reason | Meaning |
|---|---|
epoch_mismatch | The server restarted: server_epoch changed. |
out_of_buffer | You were away longer than we keep history. |
cursor_ahead | A seq we never sent. |
cursor_invalid | A seq that is not an integer. |
What to know
- We keep about 5 minutes of history (
limits.replay_max_age_s) and at mostlimits.replay_max_changeschange rows per event. Under heavy load the oldest history across all events goes first. - A
seqcan skip numbers when your filters drop a change. A gap is not loss: the server tells you withsnapshot_required. - Replay assumes the same filters as before. If you change them, subscribe without
resume. - An event you subscribe without a cursor gets a normal snapshot. A resumed event counts toward your event cap like any new subscription.
A reconnecting client
Stale books
Each book in a snapshot carries stale, the same flag as on /odds. When it flips, the next changes frame for the event carries a books object with the books whose flag changed. A frame can carry only that (changes empty).
A stale book's prices are still served: the flag says it may have stopped updating this event, not that a price is wrong. The threshold is per book; see GET /odds for the rule.
Compression
The socket supports permessage-deflate (RFC 7692), on by default on our side. Most clients only use it when asked. A 1.35 MB snapshot (NFL, 27 books) goes over the wire as 178 KB (13%); change frames shrink to about 17%.
- Python
websockets: on by default (compression="deflate"). - Node
ws:new WebSocket(url, {perMessageDeflate: true}). - Browsers always offer it.
Close codes
| Code | Meaning | Do |
|---|---|---|
| 1008 | Invalid key, the demo key, plan_required (the plan has no stream), too_many_ips, or more than 30 messages in 10 seconds | Do not retry unchanged; fix the key, plan or message rate |
| 1013 | Connection limit, more than 10 connects a minute, or you were not reading fast enough | Retry with backoff |
| 4402 | tokens_exhausted: the allowance and top-up balance cannot pay the next minute | Do not retry in a loop; top up or wait for resets_at, then reconnect and resume |
| Other / none | The connection dropped (e.g. a server restart) | Reconnect and re-subscribe with resume |