Registry / ecommerce / okx-api

okx-api

JSON →
library3.1.4jsnpmunverified

Complete Node.js SDK for OKX (formerly OKEx) REST APIs and WebSockets, with full TypeScript support and over 100 end-to-end tests. Current stable version is 3.1.4, actively maintained with frequent releases. Key differentiators: supports all OKX regions (Global, EEA, US), both REST and WebSocket APIs, automatic WebSocket reconnection and resubscription, and a unique WebSocket API client that provides a REST-like interface over WebSockets. Ships with type declarations for most request/response payloads and works in browsers via webpack. Compared to alternatives, it offers broader regional support and more extensive testing.

npm install okx-api
INSTALL
IMPORT
SIG · OKX-API
O
okx-api
ecommercejavascriptv3.1.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RestClient
import { RestClient } from 'okx-api'
const RestClient = require('okx-api').RestClient
ESM-only since v3; named export.
WebsocketClient
import { WebsocketClient } from 'okx-api'
import WebsocketClient from 'okx-api'
Named export, not default.
WebsocketAPIClient
import { WebsocketAPIClient } from 'okx-api'
Available since v3.1.0 for REST-like WebSocket usage.
OKXClient
import { OKXClient } from 'okx-api'
import { Client } from 'okx-api'
Deprecated alias for RestClient, avoid using.

Illustrates creating a REST client with API credentials and fetching a ticker and account balance.

import { RestClient } from 'okx-api'; const client = new RestClient({ apiKey: process.env.OKX_API_KEY ?? '', apiSecret: process.env.OKX_API_SECRET ?? '', apiPassphrase: process.env.OKX_API_PASSPHRASE ?? '', }); async function main() { try { const ticker = await client.getTicker({ instId: 'BTC-USDT' }); console.log('BTC-USDT ticker:', ticker); const accountInfo = await client.getAccountBalance(); console.log('Account balance:', accountInfo); } catch (error) { console.error('Error:', error); } } main();
Debug
Known issues
breakingESM-only starting from v3.x. CommonJS require() no longer works.
fix
Use import statements or upgrade to Node.js with ESM support.
affects: >=3.0.0
deprecatedOKXClient export is deprecated and may be removed in future versions. Use RestClient instead.
fix
Replace OKXClient with RestClient in imports and instantiation.
affects: >=3.0.0
gotchaWebSocket client must be configured with the correct market region (e.g., 'EEA' or 'US') or defaults to Global. Using wrong region leads to connection errors.
fix
Set the 'market' property in WebsocketClient options to match your account region.
affects: >=2.0.0
gotchaAPI keys require passphrase (not just apiKey and apiSecret). Leaving passphrase empty results in authentication failures.
fix
Provide a non-empty apiPassphrase when constructing RestClient or WebsocketClient.
affects: >=1.0.0
deprecatedDirect WebSocket subscription methods like subscribe() may be deprecated in favor of WebsocketAPIClient.
fix
Use WebsocketAPIClient for WebSocket API requests instead of manual subscription.
affects: >=3.1.0
breakingNode.js version <14 are no longer supported starting from v3.
fix
Upgrade Node.js to version 14 or later.
affects: >=3.0.0
Errors
Common errors & fixes
RestClient is not a constructor
Using CommonJS require() with an ESM-only package (v3+).
fix
Switch to ES module imports: 'import { RestClient } from \"okx-api\"'
Cannot read properties of undefined (reading 'instId')
Omitting required parameters like 'instId' in ticker request.
fix
Provide an object with the mandatory fields as per API spec, e.g., { instId: 'BTC-USDT' }
Bad Request: api-key header is required
Missing apiKey in client configuration.
fix
Set apiKey property when constructing the client.
WebSocket connection closed with code 1006
Incorrect market region or network issue.
fix
Ensure WebsocketClient options include correct 'market' (e.g., 'US' for OKX US) and check network connectivity.
TypeError: this._ws is undefined
Calling WebSocket methods before connection is established or after connection is closed.
fix
Wait for the 'open' event before subscribing, or handle reconnect logic using the library's auto-reconnect.
Upgrade
Version history
3.1.4latest on npm
Audit
Dependencies
axiosrequiredHTTP client for REST API requests
wsrequiredWebSocket client for real-time data
crypto-jsrequiredHMAC-SHA256 signing for authenticated requests
Agent activity
40 hits · last 30 days
node
36
OpenAI (training)
1
Resources
okx-api — npm install okx-api · libregistry