TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tiagosiebler/kucoin-api/llms.txt
Use this file to discover all available pages before exploring further.
WebsocketClient is the primary interface for receiving real-time data from KuCoin over WebSocket. It manages connections, authentication, heartbeats, and resubscription automatically — so your application only needs to subscribe to topics and handle incoming events. Two generations of KuCoin WebSocket endpoints are supported: the original V1 APIs and the newer V2 / Pro endpoints, each with distinct WS keys that route connections to the correct server.
WebSocket Key Map
Every subscription call requires awsKey that identifies which connection to use. The full key map is defined in WS_KEY_MAP:
| WsKey | Description |
|---|---|
spotPublicV1 | Public spot market data (V1) |
spotPrivateV1 | Private spot & margin account events (V1) |
futuresPublicV1 | Public futures market data (V1) |
futuresPrivateV1 | Private futures account & position events (V1) |
spotPublicProV2 | Public spot market data — KuCoin Pro (V2) |
futuresPublicProV2 | Public futures market data — KuCoin Pro (V2) |
privateProV2 | Unified private channel for spot + futures (V2) |
wsApiSpotV1 | WebSocket API connection for spot & margin order operations |
wsApiFuturesV1 | WebSocket API connection for futures order operations |
The
WS_KEY_MAP object is exported from kucoin-api and can be used directly in your code instead of hardcoding strings: import { WS_KEY_MAP } from 'kucoin-api'.V1 vs V2 (Pro) Endpoints
KuCoin maintains two generations of WebSocket APIs. Both are fully supported by this SDK.- V1 (Original)
- V2 / Pro
The V1 endpoints use dynamic connection URLs fetched via a REST API call before connecting. They are the current stable generation and cover all spot, margin, and futures topics. Each market (spot vs futures) and visibility (public vs private) has its own dedicated connection, identified by one of the four V1
WsKey values.- Public spot:
spotPublicV1 - Private spot:
spotPrivateV1 - Public futures:
futuresPublicV1 - Private futures:
futuresPrivateV1
Events Emitted
TheWebsocketClient is an EventEmitter. Register handlers with .on(event, handler) before subscribing to topics.
| Event | When it fires |
|---|---|
open | A WebSocket connection is established |
update | A data message arrives (market data or account event) |
reconnect | An unexpected disconnection was detected; reconnect starting |
reconnected | Connection has been successfully re-established |
close | Connection was closed |
response | A reply to a subscribe/unsubscribe/authenticate request |
authenticated | A private connection has successfully authenticated |
exception | An error was received or message parsing failed |
Configuration Options
Pass aWSClientConfigurableOptions object as the first argument to new WebsocketClient(options).
| Option | Type | Description |
|---|---|---|
apiKey | string | Your KuCoin API key (required for private streams) |
apiSecret | string | Your KuCoin API secret (required for private streams) |
apiPassphrase | string | The passphrase you set when creating the API key |
apiRegion | 'global' | 'EU' | 'AU' | Target API region (default: global) |
recvWindow | number | Recv window in milliseconds used when signing private WebSocket connections (e.g. 5000 = 5 seconds) |
pingInterval | number | Milliseconds between heartbeat pings |
pongTimeout | number | Milliseconds to wait for a pong before declaring the connection dead |
reconnectTimeout | number | Milliseconds to wait before attempting reconnection |
reauthWSAPIOnReconnect | boolean | Re-authenticate the WS API connection after reconnect |
wsUrl | string | Override the connection URL (advanced use) |
customSignMessageFn | function | Custom HMAC signing function, e.g. using Node’s crypto.createHmac |
Auto-Reconnect and Resubscribe
TheWebsocketClient handles network interruptions transparently:
Disconnection detected
When a connection drops unexpectedly, the client emits a
reconnect event with the affected wsKey.Automatic reconnection
After
reconnectTimeout milliseconds the client re-establishes the WebSocket connection, fetching a fresh connection token from the REST API if required (V1).Re-authentication
For private connections, authentication is automatically repeated as part of the connection handshake.
Topic resubscription
All previously subscribed topics are re-sent automatically. Your application does not need to re-register subscriptions after a reconnect.
Explore Specific Topics
Public Streams
Subscribe to live ticker, order book, trade, kline, and snapshot data without credentials.
Private Streams
Receive real-time order updates, balance changes, and position events for your account.
WebSocket API
Submit and cancel orders over WebSocket for the lowest possible round-trip latency.