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
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
APICredentials[]
One or more API credential objects. Required for all order operations.
Each entry has
apiKey, apiSecret, and apiPass.'prod' | 'EEA' | 'US'
default:"'prod'"
The OKX region to connect to:
'prod'— global (www.okx.com)'EEA'— European Economic Area (my.okx.com)'US'— United States (app.okx.com)
boolean
default:"false"
Set to
true to use OKX’s demo trading environment.boolean
default:"true"
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.number
How often (in milliseconds) to send a ping to keep the connection alive.
number
Delay in milliseconds before attempting to respawn a dropped connection.
Basic setup
1
Install the package
2
Create the client
3
Place your first order
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

