BestBook APIv1
Get a keybestbook.app

WebSocket quickstart

Connect

WSSwss://api.bestbook.app/v1/stream

Authenticate with the same header as REST, or ?key=<key> from a browser. The client must offer permessage-deflate; browsers, Node's ws and Python's websockets do by default.

Subscribe

json

{ "op": "subscribe", "id": "nba", "league": "nba" }

You get a snapshot of every line, then an update for each change. See Subscribe.

Your client api.bestbook.app {"op":"subscribe","id":"nba","league":"nba"} snapshot every line, in parts, last has done: true update each change, as it happens update … heartbeat after 15 s with nothing to send

Keep a live copy

javascript

const ws = new WebSocket("wss://api.bestbook.app/v1/stream?key=" + KEY);
const lines = new Map();

ws.onopen = () => ws.send(JSON.stringify({ op: "subscribe", id: "nba", league: "nba" }));

ws.onmessage = (e) => {
  const m = JSON.parse(e.data);
  if (m.type === "resync") lines.clear();
  for (const id of m.removed ?? []) lines.delete(id);
  for (const l of m.lines ?? []) lines.set(l.id, l);
  for (const p of m.prices ?? []) {
    const l = lines.get(p.line);
    if (l) l.prices = l.prices.filter((x) => x.book !== p.book || x.side !== p.side).concat(p);
  }
  for (const p of m.removed_prices ?? []) {
    const l = lines.get(p.line);
    if (l) l.prices = l.prices.filter((x) => x.book !== p.book || x.side !== p.side);
  }
};

python

# pip install websockets   (version 14 or later)
import asyncio, json, os, websockets

URL = "wss://api.bestbook.app/v1/stream"
HEADERS = {"Authorization": f"Bearer {os.environ['BESTBOOK_KEY']}"}

def same(a, b):
    return a["book"] == b["book"] and a["side"] == b["side"]

async def main():
    async for ws in websockets.connect(URL, additional_headers=HEADERS, max_size=None):
        lines = {}
        try:
            await ws.send(json.dumps({"op": "subscribe", "id": "nba", "league": "nba"}))
            async for raw in ws:
                m = json.loads(raw)
                if m["type"] == "resync":
                    lines.clear()
                for i in m.get("removed", []):
                    lines.pop(i, None)
                for l in m.get("lines", []):
                    lines[l["id"]] = l
                for p in m.get("prices", []):
                    if l := lines.get(p["line"]):
                        l["prices"] = [x for x in l["prices"] if not same(x, p)] + [p]
                for p in m.get("removed_prices", []):
                    if l := lines.get(p["line"]):
                        l["prices"] = [x for x in l["prices"] if not same(x, p)]
        except websockets.ConnectionClosed:
            continue

asyncio.run(main())

If the connection drops, reconnect and subscribe again.