KuCoin WebSocket API Trading with WebsocketAPIClient
Place, modify, and cancel spot, margin, and futures orders over WebSocket using WebsocketAPIClient for lower latency than REST, with a promise-based API.
Use this file to discover all available pages before exploring further.
The KuCoin WebSocket API lets you send trading commands — place orders, modify them, cancel them — over a persistent WebSocket connection instead of making individual HTTP REST requests. This reduces round-trip latency and is ideal for high-frequency or latency-sensitive strategies. The SDK provides two ways to use it: the high-level WebsocketAPIClient wrapper that feels like a REST client, and the lower-level sendWSAPIRequest() method on WebsocketClient for full control.
WebsocketAPIClient wraps the raw WebSocket API in typed, promise-returning methods. Each method connects and authenticates automatically on first call, then returns a promise that resolves with the exchange’s response.
The WS API uses two separate WsKey connections internally: wsApiSpotV1 for spot and margin operations, and wsApiFuturesV1 for futures operations. WebsocketAPIClient routes each method to the correct connection automatically.
import { WebsocketAPIClient, DefaultLogger } from 'kucoin-api';const wsClient = new WebsocketAPIClient( { apiKey: process.env.API_KEY || 'keyHere', apiSecret: process.env.API_SECRET || 'secretHere', apiPassphrase: process.env.API_PASSPHRASE || 'apiPassPhraseHere', // Set to false to attach your own event handlers (see below) // attachEventListeners: false, }, // Optional: pass a custom logger { ...DefaultLogger },);
The attachEventListeners option (default: true) automatically logs connection lifecycle events (open, reconnect, reconnected, authenticated, exception) to the console. Disable it if you want to register your own handlers via wsClient.getWSClient().on(...).
For advanced use cases or when you need full control over the request, call sendWSAPIRequest() directly on a WebsocketClient instance. This is the exact method that WebsocketAPIClient uses internally.
Both WebsocketAPIClient methods and sendWSAPIRequest() return promises. Await them directly in async functions or chain .then()/.catch(). This pattern is easiest to reason about and integrates naturally with existing async codebases.
inTime / outTime — gateway timestamps in milliseconds
wsKey — which WS connection handled the request
You can also listen to response and exception events on the underlying WebsocketClient to react to WS API replies alongside subscription events. Retrieve the embedded client via wsClient.getWSClient().
const innerWsClient = wsClient.getWSClient();innerWsClient.on('response', (data) => { if (data.isWSAPIResponse) { console.log('WS API response event:', data); }});innerWsClient.on('exception', (data) => { if (data.isWSAPIResponse) { console.error('WS API error event:', data); }});
WS API responses carry an isWSAPIResponse: true flag in the emitted event, making it easy to distinguish them from standard subscription responses.
By default, WebsocketAPIClient attaches console-logging listeners for lifecycle events. To use your own handlers, set attachEventListeners: false and register handlers directly on the embedded WebsocketClient: