TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tiagosiebler/bitmart-api/llms.txt
Use this file to discover all available pages before exploring further.
WebsocketClient is the single entry point for all real-time data on BitMart. It manages six persistent WebSocket connections — covering spot and futures markets, public and private channels, across both API v1 and v2 — and handles connection lifecycle, heartbeats, authentication, and topic resubscription transparently so your application code only needs to react to events.
WebSocket Connections
BitMart exposes distinct WebSocket endpoints for each combination of market (spot / futures), channel visibility (public / private), and API generation (v1 / v2). TheWebsocketClient maps these to six internal connection keys defined in WS_KEY_MAP.
| WsKey | URL | Notes |
|---|---|---|
spotPublicV1 | wss://ws-manager-compress.bitmart.com/api?protocol=1.1 | Spot public channels |
spotPrivateV1 | wss://ws-manager-compress.bitmart.com/user?protocol=1.1 | Spot private channels |
futuresPublicV1 | wss://openapi-ws.bitmart.com/api?protocol=1.1 | Futures public (legacy v1) |
futuresPrivateV1 | wss://openapi-ws.bitmart.com/user?protocol=1.1 | Futures private (legacy v1) |
futuresPublicV2 | wss://openapi-ws-v2.bitmart.com/api?protocol=1.1 | Futures public v2 (recommended) |
futuresPrivateV2 | wss://openapi-ws-v2.bitmart.com/user?protocol=1.1 | Futures private v2 (recommended) |
New integrations should use the v2 futures endpoints (
futuresPublicV2 / futuresPrivateV2). The v1 futures endpoints are maintained for backwards compatibility. Demo trading is only available on the v2 futures connections.spot/ or futures/) and whether the topic is private.
Events
TheWebsocketClient extends Node.js EventEmitter. Bind handlers with .on(event, handler) before subscribing to any topics.
| Event | When it fires |
|---|---|
open | A WebSocket connection is established |
update | A market data or account data message is received |
reconnect | The library detected a dropped connection and is retrying |
reconnected | A dropped connection has been restored |
authenticated | The server accepted the login / access request on a private channel |
response | The server replied to a subscribe or unsubscribe request |
exception | An error occurred (auth failure, bad message, etc.) |
close | A connection closed (may precede a reconnect cycle) |
Core Methods
subscribe(topics, market)
The primary subscription method. Accepts a single topic string or an array of topic strings, plus the market name ('spot' or 'futures'). Automatically routes each topic to the correct underlying WebSocket connection and handles authentication when needed.
unsubscribe(topics, market)
Removes topics from the active subscription list and sends an unsubscribe request to the server. Topics removed this way will not be resubscribed after a reconnect.
subscribeTopics(topics)
Lower-level method that accepts an array of topic strings and automatically determines the correct connection and market from the topic name prefix. Useful when managing mixed spot + futures topics in one call.
unsubscribeTopics(topics)
Lower-level counterpart to subscribeTopics. Sends unsubscribe requests and removes topics from the reconnect cache.
connectAll()
Pre-establishes all four primary connections (spot public, spot private, futures public v2, futures private v2) immediately, rather than waiting for the first subscription to trigger lazy connection. Returns an array of promises that resolve when each connection is ready.
Topic Format
BitMart WebSocket topics follow the pattern{market}/{channel}:{symbol} for symbol-specific channels, or {market}/{channel} for market-wide channels.
Spot symbols use underscores (
BTC_USDT). Futures symbols omit the underscore (BTCUSDT). Use the correct format for each market to avoid subscription errors.spot vs futures) and inspects sub-components like user, asset, order, and position to determine whether the topic is private. You never need to manually specify isPrivate unless you are using a custom or undocumented topic string.
Connection Lifecycle
Connections are created lazily — no network activity occurs until the firstsubscribe() call for a given market/visibility combination. Once a connection is open, the library sends a periodic ping (raw string 'ping' for spot; {"action":"ping"} for futures) and monitors the pong response to detect dead connections. When a disconnect is detected, the library emits reconnect, re-establishes the connection, re-authenticates if needed, and resubscribes every topic that was previously active — then emits reconnected. Your event handlers continue to work without any manual intervention.
Public Streams
Subscribe to market data — tickers, order books, trades, and klines — for spot and futures markets without credentials.
Private Streams
Subscribe to account-level streams — orders, positions, and balances — using your API key, secret, and memo.