Overview
WebsocketAPIClient is a minimal, promise-driven wrapper around WebsocketClient. It lets you submit, cancel, and amend orders over WebSocket using async/await, without managing the underlying connection lifecycle yourself.
Each method sends a request over the appropriate private or business WebSocket connection and returns a promise that resolves when the matching response arrives from OKX.
WebsocketAPIClient uses WebsocketClient internally. You can access the underlying client at any time via wsApiClient.getWSClient() to subscribe to channels, listen to events, or call sendWSAPIRequest() directly.Constructor
Accepts all
WSClientConfigurableOptions fields (credentials, market, demo trading, etc.) plus the additional options below.Optional custom logger.
Additional options (WSAPIClientConfigurableOptions)
When
true, the client automatically attaches default event listeners that log connection lifecycle events (open, reconnect, reconnected, authenticated, exception) to the console.Set to false if you want to handle all events yourself via getWSClient().on(...).Utility methods
getWSClient()
WebsocketClient instance. Use this to subscribe to channels, listen to raw events, or call sendWSAPIRequest() directly.
connectWSAPI()
setTimeOffsetMs(offsetMs)
WebsocketClient.
Order management methods
All order methods require authentication. Credentials must be provided in the constructor options.submitNewOrder(params)
Order parameters. Key fields include:
instId(string, required) — Instrument ID, e.g.'BTC-USDT'.tdMode(string, required) — Trade mode:'cash','cross', or'isolated'.side(string, required) —'buy'or'sell'.ordType(string, required) — Order type:'market','limit','post_only','fok','ioc', etc.sz(string, required) — Order size.px(string) — Price. Required for limit orders.ccy(string) — Margin currency. Required for cross-margin on non-USDT pairs.clOrdId(string) — Client-assigned order ID.tag(string) — Order tag.
submitMultipleOrders(params)
Array of order parameter objects. Each entry follows the same shape as
submitNewOrder().cancelOrder(params)
instId(string, required) — Instrument ID.ordId(string) — OKX order ID. EitherordIdorclOrdIdis required.clOrdId(string) — Client order ID.
cancelMultipleOrders(params)
Array of order ID objects. Each entry follows the same shape as
cancelOrder().amendOrder(params)
instId(string, required) — Instrument ID.ordId(string) — OKX order ID. EitherordIdorclOrdIdis required.clOrdId(string) — Client order ID.newSz(string) — New order size.newPx(string) — New order price.reqId(string) — Client-assigned request ID, echoed back in the response.
amendMultipleOrders(params)
Array of amendment objects. Each entry follows the same shape as
amendOrder().massCancelOrders(params)
instType(string, required) — Instrument type, e.g.'OPTION'.instFamily(string, required) — Instrument family, e.g.'BTC-USD'.
Spread order methods
Spread order methods route through the business WebSocket connection.submitSpreadOrder(params)
sprdId(string, required) — Spread instrument ID.side(string, required) —'buy'or'sell'.ordType(string, required) — Order type:'limit','market','post_only','ioc', etc.sz(string, required) — Order size (number of contracts).px(string) — Price. Required for limit orders.clOrdId(string) — Client-assigned order ID.tag(string) — Order tag.
amendSpreadOrder(params)
ordId(string) — OKX order ID. EitherordIdorclOrdIdis required.clOrdId(string) — Client order ID.newSz(string) — New order size.newPx(string) — New order price.reqId(string) — Client-assigned request ID, echoed back in the response.
cancelSpreadOrder(params)
ordId(string) — OKX order ID. EitherordIdorclOrdIdis required.clOrdId(string) — Client order ID.
massCancelSpreadOrders(params)
sprdId(string, optional) — Spread instrument ID to target. Omit to cancel all open spread orders.
Method summary
| Method | WS connection | OKX operation |
|---|---|---|
submitNewOrder() | private | order |
submitMultipleOrders() | private | batch-orders |
cancelOrder() | private | cancel-order |
cancelMultipleOrders() | private | batch-cancel-orders |
amendOrder() | private | amend-order |
amendMultipleOrders() | private | batch-amend-orders |
massCancelOrders() | private | mass-cancel |
submitSpreadOrder() | business | sprd-order |
amendSpreadOrder() | business | sprd-amend-order |
cancelSpreadOrder() | business | sprd-cancel-order |
massCancelSpreadOrders() | business | sprd-mass-cancel |

