Skip to main content
Connect to the WhiteBIT WebSocket API for real-time streaming data. The guide targets developers building trading bots, live price feeds, or account monitoring dashboards.

Prerequisites

  • A terminal with a WebSocket client (Python websocket-client library, or wscat for 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
WhiteBIT has no public testnet or sandbox. Private channel subscriptions expose live account data. Store API credentials securely — never commit secrets to version control or expose credentials in shared environments. For risk-free balance-change testing, use Demo Tokens (DBTC/DUSDT) — activate from the WhiteBIT Codes page.

Connection parameters

1

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.
2

Subscribe to last price updates

Subscribe to real-time price updates for BTC_USDT.
Subscribe confirmation:
Update message:
The params array contains: market name (index 0) and last traded price (index 1). The server pushes updates at 1-second intervals while the subscription is active.
3

Subscribe to orderbook depth

Subscribe to orderbook depth updates for BTC_USDT with 5 price levels.
Parameters: market name, depth limit (a fixed set of values from 1 to 100), price aggregation interval ("0" = no aggregation), and the multiple-subscription flag. Full parameter semantics, including flag and unsubscribe behavior: Depth channel.Update message:
The first element in params is a boolean: true = full snapshot, false = incremental update. To maintain a local orderbook, apply incremental updates to the initial snapshot.
4

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.
Private channels (balance updates, order updates) require authentication. First obtain a WebSocket token via the REST API, then send an authorize message.
Token endpoint: POST /api/v4/profile/websocket_token (authenticated REST call).Authorization response:
5

Subscribe to spot balance updates

After authentication, subscribe to real-time spot balance changes.
Update message — sent within 1 second of any balance change (trade or transfer):
Key fields: available (funds ready for trading), freeze (funds locked in open orders).
6

Send a keepalive ping

The server closes the connection after 60 seconds of inactivity. Send a ping message to verify the connection is alive.
Expected response:
In production, send a ping every 50 seconds or less to prevent disconnection.

Error responses

Failed requests return a JSON-RPC error object in the error field:
Check the error field on every response before treating a request as confirmed. Typical failures: an expired or invalid token in authorize — obtain a fresh one via POST /api/v4/profile/websocket_token — and a market name that does not exist in a subscribe call. Error codes and connection limits: WebSocket Rate Limits & Error Codes. The six steps above cover a single connection. Production integrations need automatic reconnection and state recovery, covered next.

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

The recovery flow after a dropped connection: reconnect with growing delays, re-authenticate with a fresh token, resubscribe every channel, then resync local state. The critical path is the reconnect loop — wait with backoff and jitter, reconnect, resubscribe, and reset the delay only after a successful connection:
Key points:
  • Keepalive ping — send ping every 50 seconds from a background task, staying inside the 60-second inactivity timeout even when no updates flow
  • 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
For an implementation with fill monitoring, grid strategy logic, and error recovery, see Building a Trading Bot — Auto-resubscription.

State recovery after reconnect

Different channels use different update models. The recovery strategy depends on the channel type: For incremental-delta and event-stream channels, use a query-then-subscribe pattern:

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 — market and account streams with schema references.