REST error handling
Response unwrapping
The SDK automatically inspects every REST response before returning it to your code:- Success — when the HTTP status is
200and the response body contains"code": "0", only thedataarray is returned. The outercode,msg, anddatawrapper is stripped away. - Error — when the HTTP status indicates an error, or when
code !== "0"in the response body, the full response object is thrown as an exception. This includes thecodeandmsgfields so you can inspect the exact OKX error.
The APIResponse<T> interface
This is the raw shape OKX returns on every REST call, defined in src/types/rest/shared.ts:
T directly. On failure the SDK throws the full APIResponse-shaped object (with a non-'0' code and a populated msg).
Catching REST errors
Inspecting the error object
When a request fails, the thrown object matches the OKX error shape:Common OKX error codes
WebSocket error handling
TheWebsocketClient is event-driven. Errors surface on the 'exception' event.
The 'exception' event
WebSocket event reference
The SDK handles reconnection automatically. When a connection drops, it tears down the dead socket, reopens a fresh connection, re-authenticates if needed, and re-subscribes to all previously subscribed channels — no extra logic required.
WebSocket API errors
When using theWebsocketAPIClient (order placement via WebSocket), errors are returned as rejected Promises so you can use standard try/catch:

