Prerequisites
- A terminal with a WebSocket client (Python
websocket-clientlibrary, orwscatfor interactive testing) - For private channels: a WhiteBIT API key with Trading permission and familiarity with Authentication
- No special setup for public channels — same zero-authentication principle as the Market Data API
Connection parameters
| Detail | Value |
|---|---|
| Endpoint | wss://api.whitebit.com/ws |
| Protocol | JSON-RPC 2.0 over WebSocket |
| Inactivity timeout | 60 seconds — send ping every 50 seconds or less |
| Rate limit | 200 requests per minute per connection |
Connect to the WebSocket endpoint
Establish a WebSocket connection.A successful connection produces no immediate response. The server is ready to receive subscription messages.For Go and PHP examples, see SDKs.
- Python
- wscat
Subscribe to last price updates
Subscribe to real-time price updates for BTC_USDT.Subscribe confirmation:Update message:The
- Python
- wscat
params array contains: market name (index 0) and last traded price (index 1). Updates arrive whenever a trade executes on the pair.Subscribe to orderbook depth
Subscribe to orderbook depth updates for BTC_USDT with 5 price levels.Parameters: market name, depth limit (The first element in
- Python
- wscat
1/5/10/20/30/50/100), price aggregation interval ("0" = no aggregation), and multiple subscription flag. Set the flag to true to add a subscription alongside existing depth subscriptions. Setting the flag to false unsubscribes from all active depth subscriptions. Full parameter details: Depth channel.Update message:params is a boolean: true = full snapshot, false = incremental update. For local orderbook maintenance, apply incremental updates to the initial snapshot.Authenticate for private channels
Steps 1–3 cover public channels (no authentication required). Steps 4–5
require an API key — see Authentication
for key creation and HMAC-SHA512 signing details.
authorize message.- Python
- wscat
POST /api/v4/profile/websocket_token (authenticated REST call).Authorization response:Subscribe to spot balance updates
After authentication, subscribe to real-time spot balance changes.Update message (triggered by a trade or transfer):Key fields:
- Python
- wscat
available (funds ready for trading), freeze (funds locked in open orders).Reconnection and state recovery
WebSocket connections can drop due to network issues or server maintenance. Production integrations require automatic reconnection, re-authentication, and state recovery logic.Reconnection with exponential backoff
- Exponential backoff — delays double on each failure (1s → 2s → 4s → … → 30s max) with random jitter to prevent thundering herd
- Fresh token — obtain a new WebSocket token on every reconnect; tokens may expire during long disconnections
- Full resubscription — re-subscribe to all channels after reconnecting; the server does not remember previous subscriptions
State recovery after reconnect
Different channels use different update models. The recovery strategy depends on the channel type:| Update model | Channels | Recovery strategy |
|---|---|---|
| Snapshot on subscribe | Depth | First message after subscribe is a full snapshot — local state auto-recovers. Apply incremental updates from subsequent messages. Detect gaps via update_id sequencing. |
| Periodic full snapshot | Positions, Borrows, Market, Market Today | Full state arrives within 1–1.5s of subscribing — no gap to fill. |
| Incremental delta | Balance Spot, Balance Margin | Query first, then subscribe. Send a one-time query (balanceSpot_request / balanceMargin_request) to get full state, then subscribe for incremental changes. |
| Event stream | Orders Pending, Orders Executed, Deals | Query first, then subscribe. Events during disconnection are lost. Use the one-time query operation or the REST equivalent to backfill, then subscribe. |
| Event-only (no snapshot) | Margin Positions Events, Borrows Events | Pair with the snapshot channel (Positions / Borrows) for complete state awareness. |
| Simple replacement | Last Price, Book Ticker, Kline | Each message replaces previous value — no gap handling needed. |
What’s next
Market Data Overview
REST API counterpart — polling-based market data for simpler integrations.
Building a Trading Bot
Combine WebSocket data with REST order placement for automated trading.
WebSocket Channels
Full channel catalog — 10 market streams and 11 account streams with schema references.