Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tiagosiebler/bitget-api/llms.txt
Use this file to discover all available pages before exploring further.
WebsocketClientV2 provides real-time WebSocket streams for Bitget’s classic (non-UTA) spot and futures accounts. It handles two separate connections — a public one for market data and a private one for account events — and takes care of authentication, heartbeat management, and automatic reconnection for you. If your account has not been upgraded to Unified Account mode, this is the correct client for live market data and order tracking.
If your Bitget account has been upgraded to Unified Account (UTA) mode, use
WebsocketClientV3 instead, which supports
the V3 topic set and account-level aggregation.Installation
Instantiation
import { WebsocketClientV2 } from 'bitget-api';
const wsClient = new WebsocketClientV2({
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
apiPass: process.env.API_PASS,
});
Event Listeners
Register listeners before subscribing to any topics to ensure no messages are dropped:Event Reference
| Event | When it fires |
|---|---|
update | A subscribed topic pushed new data (ticker, candle, order book, account, etc.) |
open | A WebSocket connection was opened or re-opened |
response | Non-data server frame (subscription confirmation, login acknowledgement) |
reconnect | The client detected a dropped connection and is about to reconnect |
reconnected | The connection was successfully re-established |
authenticated | The private connection completed the login handshake |
exception | A server-side error event was received |
subscribeTopic() Method
WebsocketClientV2 exposes a convenient helper that automatically routes subscriptions to the correct connection (public or private) based on the topic name:
coin parameter is the instrument ID for market data topics (e.g. 'BTCUSDT'). For account-level topics such as account, it defaults to 'default' and can be omitted.
Public Topics (WsPublicTopicV2)
Public topics stream market data and require no authentication. Valid instType values are 'SPOT', 'USDT-FUTURES', 'COIN-FUTURES', 'USDC-FUTURES', 'SUSDT-FUTURES', 'SCOIN-FUTURES', and 'SUSDC-FUTURES' (the S-prefixed variants are for simulated/demo trading).
| Topic | Description |
|---|---|
ticker | Best bid/ask and last-trade snapshot |
trade | Individual trade executions |
books | Full order book depth updates |
books1 | Best bid/ask order book (level 1) |
books5 | Top 5 bids and asks |
books15 | Top 15 bids and asks |
candle1m | 1-minute OHLCV candlestick |
candle5m | 5-minute OHLCV candlestick |
candle15m | 15-minute OHLCV candlestick |
candle30m | 30-minute OHLCV candlestick |
candle1H | 1-hour OHLCV candlestick |
candle4H | 4-hour OHLCV candlestick |
candle6H | 6-hour OHLCV candlestick |
candle12H | 12-hour OHLCV candlestick |
candle1D | 1-day OHLCV candlestick |
candle3D | 3-day OHLCV candlestick |
candle1W | 1-week OHLCV candlestick |
candle1M | 1-month OHLCV candlestick |
candle6Hutc | 6-hour OHLCV candlestick (UTC aligned) |
candle12Hutc | 12-hour OHLCV candlestick (UTC aligned) |
candle1Dutc | 1-day OHLCV candlestick (UTC aligned) |
candle3Dutc | 3-day OHLCV candlestick (UTC aligned) |
candle1Wutc | 1-week OHLCV candlestick (UTC aligned) |
candle1Mutc | 1-month OHLCV candlestick (UTC aligned) |
index-price | Index price (margin trading only) |
Public Subscribe Example
Private Topics (WsPrivateTopicV2)
Private topics stream account-level events and require API credentials. The client will authenticate the private connection automatically on the first private-topic subscription.
| Topic | Description |
|---|---|
account | Spot/futures balance and asset changes |
orders | Spot/futures order state changes |
positions | Futures position updates |
orders-algo | Algo/plan order lifecycle events |
positions-history | Historical position close records |
orders-crossed | Margin crossed-mode order updates |
orders-isolated | Margin isolated-mode order updates |
account-crossed | Margin crossed-mode account balance updates |
account-isolated | Margin isolated-mode account balance updates |
Private Subscribe Example
connectAll() Method
Eagerly open both the public and private connections before the first subscription:
unsubscribeTopic() Method
Remove an active subscription from the connection and the internal store:
Low-Level subscribe() / unsubscribe()
For advanced use cases you can bypass subscribeTopic() and call the lower-level methods directly, providing the WsKey explicitly:
Auto-Reconnect Behaviour
The client sends periodic ping frames and expects pong responses withinpongTimeout milliseconds. If the pong does not arrive the connection is torn down and a fresh one is opened after reconnectTimeout milliseconds. All subscriptions in the internal store are automatically replayed on reconnect — no manual intervention is needed. Listen to the reconnect and reconnected events to log or react to these lifecycle changes.