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}Instruments, sizes and orders
- Instrument ids use dashes:
BTC-USDT(spot),BTC-USDT-SWAP(perpetual),BTC-USD-250926(future). - Read
lotSz,minSzandtickSzfrom/api/v5/public/instrumentsbefore sizing orders. - Spot market buys are sized in quote currency by default; set
tgtCcy=base_ccyto size in coins. - Daily candles default to Hong Kong time; use
bar=1Dutcfor 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"
}
}backtest.py and live.py connect to when you run the exported code.| Key | What it is |
|---|---|
OKX_API_KEY | OKX API key (demo keys are created in Demo Trading mode). |
OKX_API_SECRET | OKX secret key; shown once when the key is created. |
OKX_PASSPHRASE | The passphrase you chose when creating the OKX API key. |
.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 onceLIVE_TRADING_ACKNOWLEDGED=yesis 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
- Binance API Built into AlgoGPThe largest crypto exchange by volume, with a deep public market-data API and a free spot testnet.
- Bybit API Built into AlgoGPGlobal crypto exchange with a unified V5 API for spot, derivatives and options, and a Demo Trading environment.
- Kraken API Built into AlgoGPLong-running, US-available crypto exchange with granular API key permissions and public OHLC data.
- Deribit API Built into AlgoGP (data)The leading crypto options exchange, with JSON-RPC over HTTP and WebSocket, FIX and a test environment.
Independent guide. AlgoGP is not affiliated with OKX. Limits, prices and availability change; confirm them in the official documentation. Nothing here is investment advice.