What is the WebsocketAPIClient?
WebsocketAPIClient is a REST-like wrapper around OKX’s WebSocket API. It exposes each WS API operation as a typed async method that returns a Promise — so you write the same await-based code you use with a REST client, but requests travel over a persistent WebSocket connection.
Internally, every method calls WebsocketClient.sendWSAPIRequest() and resolves when the matching response arrives. The client automatically:
- Authenticates the connection using your API credentials
- Routes order book operations over the
privatechannel - Routes spread trading operations over the
businesschannel - Reconnects and re-authenticates automatically on connection failure
When to use WebsocketAPIClient vs RestClient
RestClient | WebsocketAPIClient | |
|---|---|---|
| Transport | HTTPS | Persistent WebSocket |
| Round-trip latency | Higher (TCP + TLS handshake per request) | Lower (connection already open) |
| Order placement | Yes | Yes |
| Market data & account streams | No | Use WebsocketClient instead |
| Interface style | async/await | async/await |
How it works internally
WebsocketAPIClient wraps WebsocketClient and delegates every call to sendWSAPIRequest():
sendWSAPIRequest() directly via wsClient.getWSClient() if you need lower-level access.
Constructor
Configuration options
One or more API credential objects. Required for all order operations.
Each entry has
apiKey, apiSecret, and apiPass.The OKX region to connect to:
'prod'— global (www.okx.com)'EEA'— European Economic Area (my.okx.com)'US'— United States (app.okx.com)
Set to
true to use OKX’s demo trading environment.When
true, the client attaches default console.log event listeners for
connection events (open, reconnect, reconnected, authenticated, exception).
Set to false and attach your own listeners via wsClient.getWSClient().on(...) if
you want custom handling.How often (in milliseconds) to send a ping to keep the connection alive.
Delay in milliseconds before attempting to respawn a dropped connection.
Basic setup
The WebSocket connection is established lazily on the first request. To avoid
the cold-start delay on your first order, call
await wsClient.connectWSAPI() at
startup.Custom event listeners
By default the client logs connection events to the console. To use your own handlers, setattachEventListeners: false and listen on the embedded WebsocketClient:
Explore the available operations
Order management
Place, amend, cancel, and mass-cancel orders via WebSocket
Spread orders
Place, amend, and cancel spread trading orders via WebSocket

