
Short answer: To set up a trading bot on OKX, create an API key under Profile icon > API (OKX calls the page “My API”) > Create API key, give it read and trade permission only (never withdraw), bind it to your server IP, and paste the key, secret and a passphrase into your bot. With Python you can connect in a few lines using ccxt (exchange id “okx”).
Checked on 26 October 2026 against each exchange’s official API documentation (linked in Sources). Exchanges rename menus often; if a label differs, look for “API” in your profile menu.
This guide is part of our hub How to Set Up a Crypto Trading Bot. Before you start, read our OKX review and compare it on our exchange comparison.
Before you start: prerequisites
- A verified OKX account with 2FA on.
- A server or computer that will run the bot, ideally with a fixed outbound IP (a small VPS works).
- A separate budget you can afford to lose. Keep long-term holdings elsewhere.
- Python 3.10+ if you use the code below.
How to create a OKX API key for a trading bot (step by step)
- Create and secure your account. Sign up, finish identity verification (KYC) and turn on 2FA. API trading on most exchanges, and futures API permissions on several, require KYC.
- Open the API page. Profile icon > API (OKX calls the page “My API”) > Create API key.
- Name the key. Use a clear label such as “bot-spot-1” so you can revoke it later without guessing.
- Set permissions. OKX has three permissions: Read, Trade and Withdraw. Select Read and Trade only.
- Bind your IP address. Each key can bind up to 20 IP addresses (IPv4, IPv6 or ranges). Keys with Trade or Withdraw permission and no IP binding expire after 14 days of inactivity. Demo trading keys do not expire.
- Confirm and save the credentials. You set a passphrase when you create the key. OKX stores only a salted hash, so a lost passphrase cannot be recovered and you must create a new key. Copy the API key and secret into a password manager. The secret is usually shown only once.
- Connect your bot. Store the values as environment variables and load them in your bot (code below). Run a read call first, then one tiny order.
- Test, then scale slowly. Use demo or testnet if available, then a small live budget. Watch logs for a few days before adding funds.
OKX API settings at a glance
| Passphrase needed | Yes |
|---|---|
| IP whitelist | Each key can bind up to 20 IP addresses (IPv4, IPv6 or ranges). Keys with Trade or Withdraw permission and no IP binding expire after 14 days of inactivity. Demo trading keys do not expire. |
| Demo or testnet | Yes. Log in > Trade > Demo Trading > Personal Center > Demo Trading API > Create Demo Trading API Key. Requests need the header x-simulated-trading: 1; ccxt adds it with set_sandbox_mode(True). |
| Key limits and rate limits | Private REST limits are per User ID, so each sub-account has its own limits. Hitting a limit returns error 50011. Order placement is also capped at 1,000 new or amended orders per 2 seconds per sub-account. |
| Spot vs futures | OKX uses account modes (Spot mode, Futures mode, Multi-currency margin, Portfolio margin). Set the mode once on the web or app before the bot trades derivatives. Perpetual swaps use IDs such as BTC-USDT-SWAP. |
| Python ccxt id | okx |
Which permissions to choose
OKX has three permissions: Read, Trade and Withdraw. Select Read and Trade only.
Rule one: a trading bot never needs withdrawal rights. If a bot service asks for withdrawal permission, do not use it.
Use a sub-account for the bot
OKX recommends sub-accounts both for isolation and for extra rate limit. Create one under Sub-accounts, fund it with the bot budget, and create the key there.
Spot or futures
OKX uses account modes (Spot mode, Futures mode, Multi-currency margin, Portfolio margin). Set the mode once on the web or app before the bot trades derivatives. Perpetual swaps use IDs such as BTC-USDT-SWAP.
Start with spot. Futures bots can be liquidated in minutes if a stop fails.
Connect OKX to a Python bot (ccxt example)
ccxt is an open source library that wraps OKX and over 100 other exchanges. This minimal script loads keys from environment variables, checks the balance and reads a price.
# 1) store keys as environment variables, never in code or git
export EXCHANGE_API_KEY="..."
export EXCHANGE_API_SECRET="..."
export EXCHANGE_PASSPHRASE="..."
# 2) pip install ccxt
import os, ccxt
ex = ccxt.okx({
"apiKey": os.environ["EXCHANGE_API_KEY"],
"secret": os.environ["EXCHANGE_API_SECRET"],
"password": os.environ["EXCHANGE_PASSPHRASE"], # API passphrase
"enableRateLimit": True,
"options": {"defaultType": "spot"}, # or "swap" for perpetual futures
})
# ex.set_sandbox_mode(True) # demo / testnet keys only
print(ex.fetch_balance()["total"]) # read test
print(ex.fetch_ticker("BTC/USDT")["last"]) # market data
# ex.create_order("BTC/USDT", "limit", "buy", 0.0001, 50000) # tiny test order
Build your own AI trading bot
You do not need a paid bot service. AI assistants such as Grok Bot can write and run the code above, add your strategy rules and log every trade. TradeReviewed hosts independent traders who run AI-built bots on real accounts and publish every trade, wins and losses. Treat any bot, AI or not, as experimental until it has months of logged results.
Security checklist
- Read and trade only. Withdraw off.
- IP whitelist on.
- One key per bot, clear labels.
- Keys in environment variables or a secrets manager, never in git, screenshots or chats.
- Sub-account with only the bot budget.
- Stop-loss logic in the bot and a daily loss limit.
- Review and rotate keys every few months; delete unused keys.
- If a key leaks, delete it on the exchange first, then investigate.
Troubleshooting common API errors
| Error | Fix |
|---|---|
| 50102 Timestamp request expired | Your server clock is off by more than 30 seconds. Sync with NTP or use the OKX public time endpoint. Always use UTC. |
| 50011 Rate limit reached | Throttle requests. In ccxt keep enableRateLimit on (default). |
| 50061 Sub-account rate limit exceeded | More than 1,000 order requests in 2 seconds. Slow down or split across sub-accounts. |
| Login or key errors for EU or US users | EU accounts registered on my.okx.com must use the eea.okx.com API domain, and US or AU accounts us.okx.com. See the regional note in the OKX docs. |
| Timestamp or recvWindow errors | Sync your server clock with NTP. |
Fees and EU availability
Bot strategies trade often, so fees matter. See OKX fees. OKX serves EEA users through its MiCA-authorised entity. EU accounts use the eea.okx.com API domain according to the OKX docs.
Frequently asked questions
Can I use a trading bot on OKX?
Yes. OKX offers an API for automated trading. Create a key with read and trade permission, bind your IP and connect your bot.
Does the OKX API need a passphrase?
Yes. You set it when creating the key and it cannot be recovered.
Should I enable withdrawal permission on my OKX API key?
No. A trading bot never needs it. Leave withdraw off.
Does OKX have a demo or testnet API?
Yes. Log in > Trade > Demo Trading > Personal Center > Demo Trading API > Create Demo Trading API Key. Requests need the header x-simulated-trading: 1; ccxt adds it with set_sandbox_mode(True).
Why did my OKX API key stop working?
Each key can bind up to 20 IP addresses (IPv4, IPv6 or ranges). Keys with Trade or Withdraw permission and no IP binding expire after 14 days of inactivity. Demo trading keys do not expire.
Is OKX supported by ccxt?
Yes, as “okx”.
API bot guides for other exchanges
- BingX API trading bot setup
- Bitget API trading bot setup
- MEXC API trading bot setup
- WEEX API trading bot setup
- OrangeX API trading bot setup
- All exchanges: crypto trading bot setup hub
Sources
Not financial advice. Automated trading can lose money fast, especially with leverage. Past results do not guarantee future returns. Never give any bot or third party withdrawal rights, and check the rules in your country before you trade.