Built into AlgoGP Pro
BrokerMarket dataCryptoFuturesOptions API key

OKX API guide

OKX is one of the largest crypto exchanges and its V5 API is unusually complete: one unified account and one API for spot, margin, perpetual swaps, dated futures and options. The REST signing scheme is plain HMAC, so it fits standard-library code well.

Last verified · Also known as OKEx

Quick answer

The OKX V5 API covers spot, perpetual swaps, futures and options through REST and WebSocket. Private calls are signed with HMAC-SHA256 over timestamp + method + path + body and sent with OK-ACCESS-KEY, OK-ACCESS-SIGN, OK-ACCESS-TIMESTAMP and OK-ACCESS-PASSPHRASE headers. Demo Trading uses the same host with an x-simulated-trading: 1 header and keys created in demo mode.

OKX API at a glance

Pricing
Free API; trading fees apply
Availability
Global; restricted in the US and some regions
Paper trading
Demo Trading (same host, x-simulated-trading header)
Integration type
REST + API key
In AlgoGP
Built in: broker and data adapter (Pro)
REST base
https://www.okx.com (/api/v5/…)
Auth
Key + secret + passphrase; HMAC-SHA256, base64
Demo
Same host, header x-simulated-trading: 1
Candles
/api/v5/market/candles (300) and /history-candles (100/req)
Markets
Spot, margin, swaps, futures, options

How OKX requests are signed

import base64, hashlib, hmac
from datetime import datetime, timezone
ts = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
prehash = ts + "GET" + "/api/v5/account/balance"
sign = base64.b64encode(hmac.new(SECRET.encode(), prehash.encode(), hashlib.sha256).digest()).decode()
headers = {"OK-ACCESS-KEY": KEY, "OK-ACCESS-SIGN": sign,
           "OK-ACCESS-TIMESTAMP": ts, "OK-ACCESS-PASSPHRASE": PASSPHRASE}
The prehash string is timestamp + METHOD + requestPath (with query) + body.

Instruments, sizes and orders

  • Instrument ids use dashes: BTC-USDT (spot), BTC-USDT-SWAP (perpetual), BTC-USD-250926 (future).
  • Read lotSz, minSz and tickSz from /api/v5/public/instruments before sizing orders.
  • Spot market buys are sized in quote currency by default; set tgtCcy=base_ccy to size in coins.
  • Daily candles default to Hong Kong time; use bar=1Dutc for UTC-aligned days.

Rate limits

Limits are published per endpoint (for example 20 requests per 2 seconds on many trading endpoints) and enforced by user id or IP. WebSocket connection requests are limited to 3 per second per IP. Some order books update every 50 ms on the public feed.

Use OKX in AlgoGP

Open a project, go to the APIs & keys tab and, under Platforms, pick OKX as the market data source (market_data.py) and the broker (brokers.py). The choice is saved to config.json as a new strategy version:

{
  "platforms": {
    "data": "okx",
    "broker": "okx"
  }
}
In-app backtests always use AlgoGP’s normalized history so results stay comparable; this choice decides what backtest.py and live.py connect to when you run the exported code.
KeyWhat it is
OKX_API_KEYOKX API key (demo keys are created in Demo Trading mode).
OKX_API_SECRETOKX secret key; shown once when the key is created.
OKX_PASSPHRASEThe passphrase you chose when creating the OKX API key.
Names the generated code reads. Add values under Keys & secrets or in a local .env.
  • TRADING_MODE=paper (default): simulated fills on your machine; the broker is never contacted.
  • TRADING_MODE=sandbox: orders go to OKX’s demo trading (same host, x-simulated-trading header).
  • TRADING_MODE=live: real orders, and only once LIVE_TRADING_ACKNOWLEDGED=yes is set.

New to OKX? Follow the step-by-step OKX API key guide.

OKX API: frequently asked questions

Is the OKX API free?

Yes. You pay trading fees, not API fees.

Does OKX have a testnet?

It has Demo Trading: switch to demo mode, create a demo API key, and send x-simulated-trading: 1 to the normal host.

What is the OKX API passphrase?

A password you choose when creating the key; it is sent with every private request and cannot be recovered.

Can US residents use the OKX API?

OKX.com restricts US residents; check the regional service (such as OKX US) available to you.

Why does OKX return error 50113 or 50111?

Signature or key problems: check the prehash string, base64 encoding and that a demo key is used with the demo header.

Build an OKX trading bot in minutes

Describe the strategy in plain English, backtest it on years of data, then export Python that already talks to OKX: paper, sandbox and live modes, keys handled for you.

  • Generated adapter for OKX orders and balances
  • Backtests with costs, slippage and walk-forward checks
  • Paper first; live trading only after you opt in

Free plan, no card required. OKX is included with Pro.

OKX alternatives

Independent guide. AlgoGP is not affiliated with OKX. Limits, prices and availability change; confirm them in the official documentation. Nothing here is investment advice.