Skip to main content

REST error handling

Response unwrapping

The SDK automatically inspects every REST response before returning it to your code:
  • Success — when the HTTP status is 200 and the response body contains "code": "0", only the data array is returned. The outer code, msg, and data wrapper 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 the code and msg fields 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:
On success you receive 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

The full error code reference is in the OKX API docs.

WebSocket error handling

The WebsocketClient 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 the WebsocketAPIClient (order placement via WebSocket), errors are returned as rejected Promises so you can use standard try/catch: