Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tiagosiebler/bybit-api/llms.txt
Use this file to discover all available pages before exploring further.
WebsocketClient is the primary real-time data client for the bybit-api SDK. It manages all WebSocket connections to Bybit V5 public and private streams, automatically handles authentication, reconnects on network failures, and batches subscription requests. It also exposes the low-level sendWSAPIRequest() method for sending order-management commands over WebSocket.
Installation & Import
Constructor
WebSocket client configuration. All fields are optional.
Optional custom logger instance. If omitted, the SDK’s built-in console logger is used.
Instantiation examples
Connection Management Methods
subscribeV5(wsTopics, category, isPrivateTopic?)
Subscribe to one or more V5 WebSocket topics. The SDK automatically routes each topic to the correct underlying connection (spot public, linear public, inverse public, option public, or private) based on the category and whether the topic is private.
One or more topic strings to subscribe to, e.g.
'orderbook.50.BTCUSDT'.The instrument category:
'spot' | 'linear' | 'inverse' | 'option'. Ignored for private topics.Force the topic to be treated as private. Usually not needed — the SDK detects private topics automatically.
unsubscribeV5(wsTopics, category, isPrivateTopic?)
Unsubscribe from one or more V5 topics and remove them from the reconnect cache. The SDK will not re-subscribe to these topics if the connection drops.
Topic or topics to unsubscribe from.
The instrument category.
Force private topic detection override.
connectAll()
Eagerly open every dependent WebSocket connection (all public streams + private stream) rather than waiting for the first subscription call. Returns an array of connection promises.
connectPublic()
Open all V5 public WebSocket connections (spot, linear, inverse, option) without connecting the private stream.
connectPrivate()
Open the V5 private WebSocket connection.
connectWSAPI()
Open and authenticate the V5 private trade WebSocket connection used for WS API order management. This connection must be authenticated before any sendWSAPIRequest() calls. Calling this in advance can reduce latency on the first order placement.
subscribe(requests, requestedWsKey?)
Lower-level subscription method that accepts WsTopicRequest objects or plain topic strings with optional explicit WsKey routing. Consider using subscribeV5() for most use-cases.
unsubscribe(requests, wsKey?)
Lower-level unsubscription counterpart to subscribe().
Event Emitter API
WebsocketClient extends an event emitter. Attach listeners with .on(event, handler). The primary data events are listed below.
Always attach
.on('update', handler) before calling any subscribe method to ensure you don’t miss the first messages.update
Fired for every incoming market-data or private stream message. This is the main data event.
Parsed JSON of the incoming WebSocket message. The
topic property identifies which stream produced the event.open
Fired when a WebSocket connection is successfully opened.
Key identifying which connection opened.
Raw WebSocket open event.
response
Fired in response to a subscribe/unsubscribe request or ping/pong. Used to confirm that a subscription was accepted.
authenticated
Fired after the private WebSocket connection successfully authenticates.
close
Fired when a connection closes.
exception
Fired when an error occurs. Inspect data for details.
reconnect
Fired when the SDK begins a reconnect attempt.
reconnected
Fired when the SDK has successfully reconnected and re-subscribed to all tracked topics.
WS API — sendWSAPIRequest()
Send a command over the authenticated WS API connection and receive a typed promise that resolves with the exchange’s reply.
The connection key. Use
WS_KEY_MAP.v5PrivateTrade for all Bybit V5 WS API operations.The WS API command string. One of:
'order.create' | 'order.amend' | 'order.cancel' | 'order.create-batch' | 'order.amend-batch' | 'order.cancel-batch'.Request parameters matching the operation. For
order.create this is OrderParamsV5.{ authIsOptional?: boolean } — skip auth requirement if true.- The WS API response contains a non-zero
retCode. - The connection closes before a reply is received.
WS_KEY_MAP — Connection Keys
TheWS_KEY_MAP constant maps human-readable names to the string keys used internally by the SDK. Import it to reference specific connections.
| Key | Value | Description |
|---|---|---|
WS_KEY_MAP.v5SpotPublic | 'v5SpotPublic' | V5 spot public market data stream. |
WS_KEY_MAP.v5LinearPublic | 'v5LinearPublic' | V5 linear (USDT/USDC perp & futures) public stream. |
WS_KEY_MAP.v5InversePublic | 'v5InversePublic' | V5 inverse (BTC-margined) public stream. |
WS_KEY_MAP.v5OptionPublic | 'v5OptionPublic' | V5 options public stream. |
WS_KEY_MAP.v5Private | 'v5Private' | V5 private stream (orders, positions, wallet). |
WS_KEY_MAP.v5PrivateTrade | 'v5PrivateTrade' | V5 WebSocket API connection for order management. |
Complete Usage Example
WebsocketAPIClient
Higher-level promise wrapper for WS order management.
Types Reference
WsKey, WsTopic, WSAPIOperation, and all configuration types.
Enums & Constants
WS_KEY_MAP values and WS_API_Operations array.
RestClientV5
Full REST API method reference.