Skip to main content

Overview

The RestClient class maps every OKX REST API endpoint to a typed TypeScript method. Public endpoints work without credentials. Private endpoints require an API key, secret, and passphrase.

Installation

npm install okx-api

Instantiation

import { RestClient } from 'okx-api';

// Public endpoints only (no credentials required)
const publicClient = new RestClient();

// Private + public endpoints
const client = new RestClient({
  apiKey: 'your-api-key',
  apiSecret: 'your-api-secret',
  apiPass: 'your-api-passphrase',
});

Constructor options

OptionTypeRequiredDescription
apiKeystringNoAPI key from OKX. Required for private endpoints.
apiSecretstringNoAPI secret from OKX. Required for private endpoints.
apiPassstringNoAPI passphrase set when creating the key. Required for private endpoints.
market'GLOBAL' | 'EEA' | 'US' | 'prod'NoRegional endpoint to connect to. Defaults to 'GLOBAL' (www.okx.com).
demoTradingbooleanNoSet to true to use the OKX demo trading environment.
baseUrlstringNoOverride the default REST base URL.
keepAlivebooleanNoEnable HTTP KeepAlive for REST requests via axios.
strict_param_validationbooleanNoThrow errors when true and any parameter is undefined.
parse_exceptionsbooleanNoWhether to post-process request exceptions. Default: true.
customSignMessageFnfunctionNoProvide a custom HMAC signing function for higher performance (e.g. Node.js createHmac).

Request and response pattern

All methods return a Promise that resolves to an array of typed response objects. The SDK automatically unwraps the OKX { code, msg, data } envelope — when code === '0' the promise resolves with data; any non-zero code causes the promise to reject with an error.
// The promise resolves with AccountBalance[] directly
const balances = await client.getBalance();
console.log(balances[0].totalEq); // e.g. '1234.56'

Error handling

When OKX returns a non-zero error code, or a network error occurs, the SDK throws. Wrap calls in try/catch:
import { RestClient } from 'okx-api';

const client = new RestClient({
  apiKey: process.env.API_KEY!,
  apiSecret: process.env.API_SECRET!,
  apiPass: process.env.API_PASS!,
});

try {
  const positions = await client.getPositions();
  console.log('Open positions:', positions.length);
} catch (err) {
  // err.body contains the raw OKX error response
  console.error('OKX error:', err);
}
The SDK never silently swallows errors. If code !== '0' the returned data is thrown as the error, giving you the full OKX error object.

Latency utility

The client exposes a helper to measure round-trip latency and clock skew between your machine and OKX servers:
const result = await client.fetchLatencySummary();
console.log(`Round trip: ${result.roundTripTime}ms`);
console.log(`Clock skew: ${result.timeDifference}ms`);
If timeDifference exceeds 500 ms your requests may be rejected by OKX due to timestamp validation. Synchronize your system clock.

Available sections

Account

Balances, positions, leverage, fee rates, and account configuration.

Trading

Place, cancel, and amend orders. Query order history and fills.

Funding

Deposits, withdrawals, transfers, and currency conversion.

Market Data

Public tickers, candles, order books, and funding rates.

Sub-accounts

Manage sub-accounts, balances, and inter-account transfers.

Earn & Finance

Staking, savings, ETH staking, and fixed/flexible lending.

Copy Trading

Lead-trader and copy-trader management, positions, and profit sharing.

Grid Trading

Create and manage spot, futures, and contract grid bots.

Signal Bots

Create signal sources, deploy bots, and manage sub-orders.

Block Trading

RFQ creation, quote management, and block trade execution.

Spread Trading

Submit and manage spread orders and query spread market data.

Recurring Buy

Set up, amend, and track recurring DCA purchase orders.

Build docs developers (and LLMs) love