TheDocumentation 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 in bybit-api is a full-featured WebSocket manager that handles every aspect of connectivity for you. Connections open on first subscribe, authentication happens automatically when credentials are provided, heartbeats (ping/pong) keep connections alive, and dropped connections are silently respawned with all topics resubscribed. You write event handlers — the SDK handles the rest.
What WebsocketClient Provides
- Automatic connection management — connections open lazily when you first subscribe to a topic, so there is no explicit
connect()call required. - Authentication on connect — supply
keyandsecretonce in the constructor; the SDK signs and sends auth frames before any private topic subscription. - Heartbeats — a configurable ping/pong cycle keeps connections alive and detects silent disconnects.
- Reconnect + resubscribe — when a connection drops, the SDK reopens it and replays all tracked subscriptions without any code on your part.
- Multi-category routing — a single client instance correctly routes spot, linear, inverse, option, and private topics to their respective Bybit WebSocket endpoints.
WebSocket Connection Model
Bybit exposes separate WebSocket endpoints for each product category. The SDK tracks these as named WsKeys, defined inWS_KEY_MAP:
| WsKey | Bybit Endpoint | Purpose |
|---|---|---|
v5SpotPublic | wss://stream.bybit.com/v5/public/spot | Spot public market data |
v5LinearPublic | wss://stream.bybit.com/v5/public/linear | Linear perp/futures public data |
v5InversePublic | wss://stream.bybit.com/v5/public/inverse | Inverse perp/futures public data |
v5OptionPublic | wss://stream.bybit.com/v5/public/option | Options public data |
v5Private | wss://stream.bybit.com/v5/private | All private account streams |
v5PrivateTrade | wss://stream.bybit.com/v5/trade | WebSocket API (order submission) |
subscribeV5(topic, category), the SDK automatically determines which WsKey to use based on the topic name and category, then opens (or reuses) the appropriate connection.
Testnet uses
stream-testnet.bybit.com and demo trading uses stream-demo.bybit.com/v5/private. Switch between environments via the testnet or demoTrading constructor options — no URL changes needed.Constructor and Configuration
Configuration Reference
Your Bybit API key. Required to subscribe to private topics or use the WebSocket API.
Your Bybit API secret. Required alongside
key for authenticated connections.Connect to Bybit’s testnet environment. Set to
false when using demo trading.Connect to Bybit’s V5 demo trading environment. Note: demo trading does not support the WebSocket API as of January 2025.
The API market group. Only
'v5' is supported.Milliseconds between outgoing ping frames. Keeps connections alive.
Milliseconds to wait for a pong reply. If no pong arrives within this window the connection is considered dead and a reconnect is triggered.
Milliseconds to wait before spawning a new connection after a drop.
Size of the recv window (ms) included in the WebSocket auth signature.
Override the WebSocket URL entirely (e.g. for a proxy or alternative domain).
Provide your own HMAC signing function. Useful for using Node.js’s faster
crypto.createHmac instead of the built-in Web Crypto API.Event System
All data and lifecycle events are emitted on theWebsocketClient instance. Subscribe with .on(event, handler).
| Event | When it fires | Payload |
|---|---|---|
update | A topic update arrives (market data or account event) | Parsed JSON object with a topic and data field, plus an injected wsKey |
open | A WebSocket connection is established | { wsKey: string, event: object } |
response | A reply to a subscribe/unsubscribe/ping command | Parsed reply object |
authenticated | The SDK successfully authenticated a private connection | { wsKey: string } |
close | A connection closes | — |
exception | An error or rejected command is received | Error/event details |
reconnect | Automatic reconnection is starting | { wsKey: string } |
reconnected | Reconnection succeeded | { wsKey: string } |
update is the primary data event. All subscription topic messages — orderbooks, klines, position changes, wallet updates — arrive on update. Filter by data.topic to distinguish between streams.Basic Setup Example
Pre-connecting
By default, connections open lazily when you first subscribe. If you want to eliminate the cold-start delay on the first subscription, you can request connections explicitly:Load Balancing Across Multiple Clients
EachWebsocketClient instance maintains its own set of connections. To distribute a large number of topics across separate connections and reduce per-connection throughput, create multiple instances:
Next Steps
Public Streams
Subscribe to orderbook, kline, ticker, trade, and liquidation streams without authentication.
Private Streams
Receive live position, order, execution, wallet, and greeks updates for your account.
WebSocket API
Submit, amend, and cancel orders over WebSocket with a promise-based interface.