The WebSocket endpoint is wss://<host>/api/v1/stream. HTTP/HTTPS URLs are converted to WS/WSS automatically.
Constructor Parameters
Parameter
Type
Required
Description
host
str
Yes
API host URL (HTTP or WebSocket scheme)
reconnect
bool
No
Auto-reconnect on disconnect (default: True)
reconnect_delay
float
No
Initial reconnect delay in seconds (default: 1.0)
max_reconnect_delay
float
No
Max reconnect delay in seconds (default: 60.0)
Subscriptions
Subscribe to a market to receive all updates (orderbook, trades, cancellations) for that market. Subscriptions are market-level — you subscribe to a market ID, not to individual channels.
You can subscribe to multiple markets simultaneously:
Convenience Aliases
These are aliases for subscribe() and exist for backwards compatibility:
Receiving Messages
Async Iteration
The primary way to receive messages. Iterates indefinitely until the connection closes:
Single Receive
Receive one frame of messages (the server may batch multiple messages per frame):
Returns: list[WSMessage] — one or more parsed messages from a single WebSocket frame.
Message Types
All messages are instances of WSMessage or its subclasses. The type field determines the message kind.
Type
Class
Property
Description
"orderbook"
OrderBookUpdate
.orderbook → OrderBookSnapshot
Full orderbook snapshot
"trade"
TradeUpdate
.trade → Trade
Trade execution
"quick_market"
QuickMarketUpdate
.quick_market → QuickMarket
Quick market state change
"order_cancelled"
WSMessage
.data → dict
Order cancellation
Orderbook Updates
Sent whenever the orderbook changes for a subscribed market. Contains a full snapshot (not a diff).
Trade Updates
Sent when a trade is executed in a subscribed market.
Quick Market Updates
Sent when a quick market's state changes (new market, resolution, etc.).
Connection with Retry
For long-running bots, use connect_with_retry() to handle disconnections with exponential backoff:
If the connection fails, it retries with exponential backoff (1s, 2s, 4s, ..., up to 60s). The reconnect=False setting disables retries and raises WebSocketError immediately on failure.
Closing
Close the stream or client explicitly when done:
The context manager (async with ws.connect()) handles cleanup automatically.
Complete Example
A bot that monitors a BTC quick market and logs orderbook + trade activity:
Wire Format
The WebSocket server sends newline-delimited JSON. Each frame may contain one or more JSON objects separated by \n. The client parses these automatically into individual WSMessage objects.
market_ids = ["0xMarket1...", "0xMarket2...", "0xMarket3..."]
for market_id in market_ids:
await stream.subscribe(market_id)
await stream.subscribe_orderbook(market_id) # same as subscribe()
await stream.subscribe_trades(market_id) # same as subscribe()
recv_iter.py
async with ws.connect() as stream:
await stream.subscribe("0x...")
async for msg in stream:
if msg.type == "orderbook":
ob = msg.orderbook
if ob and ob.bids:
print(f"Best bid: {ob.bids[0].price} (${ob.bids[0].price / 1e6:.4f})")
elif msg.type == "trade":
trade = msg.trade
if trade:
print(f"Trade: {trade.size / 1e6:.2f} @ {trade.price} (${trade.price / 1e6:.4f})")
elif msg.type == "order_cancelled":
print(f"Order cancelled in market {msg.market_id}")
elif msg.type == "quick_market":
qm = msg.quick_market
if qm:
print(f"Quick market update: {qm.market_id}")
recv_single.py
messages = await stream.recv()
for msg in messages:
print(f"{msg.type}: {msg.data}")
orderbook_example.py
if msg.type == "orderbook":
ob = msg.orderbook # OrderBookSnapshot
# Bids sorted best (highest) first
for level in ob.bids[:3]:
print(f" BID {level.price} (${level.price / 1e6:.4f}) x {level.size / 1e6:.2f}")
# Asks sorted best (lowest) first
for level in ob.asks[:3]:
print(f" ASK {level.price} (${level.price / 1e6:.4f}) x {level.size / 1e6:.2f}")
# Mid price
if ob.bids and ob.asks:
mid = (ob.bids[0].price + ob.asks[0].price) / 2
print(f" Mid: {mid:.0f} (${mid / 1e6:.4f})")