Skip to main content
๐Ÿ“– Tronsell Wiki

OKX API Guide: Complete Guide to OKX API Integration

Complete guide to integrating with OKX's REST and WebSocket APIs for spot, futures, and options trading. Learn authentication, key endpoints, rate limits, and best practices for building production-ready applications.

๐Ÿ”ด OKX API at a Glance
API Version v5 (REST) ยท WebSocket Streams
Rate Limit 50 req/sec per API key
Products Spot, Futures, Options, Margin
Testnet www.okx.com/testnet
Auth Method API Key + HMAC SHA256 + Passphrase
Best For Trading bots, data analysis

๐Ÿ”ด OKX API Overview

The OKX API is a comprehensive and well-documented API that provides programmatic access to OKX's spot, futures, options, and margin trading platforms. OKX uses a unified API structure (version 5) for all products, with a consistent authentication method across all endpoints.

OKX's API is known for its:

  • Unified structure: Same endpoints for spot, futures, and options.
  • Advanced features: Support for options trading, block trading, and more.
  • Reliable WebSocket: Low-latency real-time data streaming.
  • Comprehensive documentation: Detailed API reference with examples.
๐Ÿ’ก OKX API Key Features
  • Unified API: Single API for all products (spot, futures, options).
  • Passphrase authentication: Additional security layer.
  • WebSocket streams: Real-time data with low latency.
  • Testnet support: Safe testing with free test funds.

๐Ÿš€ Getting Started with OKX API

Before you can use the OKX API, you need to set up your credentials and understand the authentication process.

1. Get Your API Keys

  • 1
    Log in to your OKX account

    Go to the OKX website and log in.

  • 2
    Navigate to API Management

    Go to Account Settings โ†’ API.

  • 3
    Create a new API key

    Click "Create API Key," enter a label, and set permissions (Read, Trade, Withdraw).

  • 4
    Set IP whitelist (recommended)

    Add the IP address of your server for enhanced security.

  • 5
    Save your API key, secret, and passphrase

    Copy the API key, secret, and passphrase. The secret is shown only once โ€” store all securely.

2. Authentication

OKX API uses HMAC SHA256 signature authentication with an additional passphrase. Here's how it works:

signature = HMAC_SHA256(timestamp + method + requestPath + body, apiSecret)
Headers: OK-ACCESS-KEY, OK-ACCESS-SIGN, OK-ACCESS-TIMESTAMP, OK-ACCESS-PASSPHRASE

3. Use the Testnet

Always test on the OKX testnet before deploying with real funds.

๐Ÿ’ก Testnet URL

OKX Testnet: www.okx.com/testnet โ€” Create testnet API keys and get free test funds.

๐Ÿ“‹ Key OKX API Endpoints

OKX uses a unified API structure with consistent endpoints across all product types.

Category Endpoint Description Method
Market Data /api/v5/market/tickers Get ticker prices GET
Market Data /api/v5/market/candles Get candlestick data GET
Market Data /api/v5/market/books Get order book depth GET
Account /api/v5/asset/balances Get account balances GET
Orders /api/v5/trade/order Place a new order POST
Orders /api/v5/trade/cancel-order Cancel an order POST
Orders /api/v5/trade/orders-pending Get open orders GET
Orders /api/v5/trade/orders-history Get order history GET
Orders /api/v5/trade/fills Get trade history GET
Futures /api/v5/account/positions Get futures positions GET
๐Ÿ’ก Unified API

OKX uses the same endpoints for spot, futures, and options. The instType parameter specifies the instrument type: SPOT, FUTURES, OPTIONS, or MARGIN.

๐Ÿ”„ OKX WebSocket Streams

OKX WebSocket streams provide real-time data for all product types.

Public WebSocket Streams

  • Price Ticker: wss://ws.okx.com:8443/ws/v5/public
  • Order Book: wss://ws.okx.com:8443/ws/v5/public
  • Trade Stream: wss://ws.okx.com:8443/ws/v5/public
  • Candlestick: wss://ws.okx.com:8443/ws/v5/public

Private WebSocket Streams

  • Account updates: Balance and position changes
  • Order updates: Order status changes
  • Trade updates: Trade execution
๐Ÿ“Œ WebSocket Best Practices
  • Use multiple channels in a single connection.
  • Implement auto-reconnection with exponential backoff.
  • Use ping/pong keepalive messages.
  • Authenticate for private streams using the login message.

๐Ÿšฆ OKX API Rate Limits

OKX has a rate limit of 50 requests per second per API key for most endpoints. WebSocket connections have separate limits.

Endpoint Rate Limit Notes
/api/v5/market/* 50 req/sec Market data endpoints
/api/v5/trade/* 50 req/sec Order placement and management
/api/v5/account/* 50 req/sec Account information
/api/v5/asset/* 50 req/sec Asset management
๐Ÿ’ก Rate Limit Headers

Monitor these headers in responses: x-ratelimit-limit, x-ratelimit-remaining, x-ratelimit-reset. Always check remaining limit before sending requests.

โš ๏ธ Common OKX API Errors

Here are the most common OKX API errors and how to fix them.

Error Code Message Cause Solution
30001 Invalid request Malformed request Check request format and parameters
30008 Insufficient balance Not enough funds Check balance before placing orders
30009 Invalid symbol Symbol doesn't exist Verify symbol is correct
30010 Invalid order size Quantity below min or not step-sized Check exchange filters for the symbol
30011 Invalid order price Price below min or not tick-sized Check exchange filters for the symbol
30012 Order not found Order ID not found Verify order ID before querying
30013 Order already canceled Order already canceled Check order status before canceling
30014 Order already filled Order already filled Check order status before canceling
30015 API key not found API key doesn't exist Verify API key is active and correct
400004 Timestamp out of sync System time is off Synchronize system time via NTP

๐Ÿ† OKX API Best Practices

Follow these best practices for reliable OKX API integration.

  • Always use the testnet first: Test all code on testnet before deploying with real funds.
  • Secure your API keys: Use environment variables, IP whitelisting, and least privilege permissions.
  • Monitor rate limit headers: Track your request count to avoid hitting limits.
  • Use WebSocket for real-time data: Avoid polling REST endpoints for price updates.
  • Implement error handling: Handle common errors like 30008 (insufficient balance) and 400004 (timestamp).
  • Use client order IDs: Assign unique IDs to orders for better tracking.
  • Stay updated: OKX API changes frequently โ€” keep your libraries updated.
  • Use the correct instrument type: Specify instType correctly for spot, futures, or options.
๐Ÿ’ก Pro Tip

Use the CCXT library for a unified interface to OKX and other exchanges. It handles authentication, rate limiting, and error handling automatically.

โ“ Frequently Asked Questions About OKX API

How do I get an OKX API key?

To get an OKX API key: 1) Log in to your OKX account. 2) Go to Account Settings โ†’ API. 3) Click 'Create API Key.' 4) Enter a label and set permissions (Read, Trade, Withdraw). 5) Set IP whitelist (optional but recommended). 6) Complete 2FA verification. 7) Copy and save your API key, secret, and passphrase. The secret is shown only once.

What is the difference between OKX Spot API and Futures API?

OKX uses a unified API structure for all products. The same endpoints work for spot, futures, and options by specifying the instrument type. The base URL is api.okx.com for all products, and the instrument type is specified in the request parameters.

What are OKX API rate limits?

OKX has a rate limit of 50 requests per second per API key. Different endpoints may have additional limits. WebSocket connections have separate limits. Always check the response headers for rate limit information.

How do I fix OKX API error 30008?

Error 30008 means 'Insufficient balance.' You don't have enough funds in your account to place the order. Check your available balance before retrying.

Does OKX have a testnet for API testing?

Yes, OKX offers a testnet environment at www.okx.com/testnet. It provides free test funds for safe API testing of spot, futures, and options trading. You can create testnet API keys and test your integration without risking real money.

What permissions should I set for my OKX API key?

For most trading bots, enable Read (to view balances and market data) and Trade (to place orders). Never enable Withdraw unless absolutely necessary. Use IP whitelisting for additional security.

Can I use WebSocket for OKX API?

Yes, OKX provides WebSocket streams for real-time data. Public streams include price tickers, order books, and trade streams. Private streams (authenticated) provide account and order updates. WebSocket is recommended for real-time applications.

What are the best libraries for OKX API?

The most popular libraries are CCXT (unified API for multiple exchanges) and okx-api (Node.js). CCXT is recommended for multi-exchange support, while okx-api is a dedicated library for OKX.

๐Ÿ”ด Start Building with OKX API

Integrate with OKX's powerful API for spot, futures, and options trading. Start with the testnet, secure your keys, and build reliable trading applications.