From creating an API key, choosing permissions and setting an IP whitelist, to what each error code means when an order is rejected and how to handle it.
No OKX account yet? Sign up for OKX
The OKX API lets a program talk to your OKX account directly: check balances, check positions, place and cancel orders, all without opening the website. Auto trading programs place orders by rules through the API.
To use the API, first create a key. The program uses it to prove it's authorized by your account. What the key can do depends on the permissions you enable when you create it.
Give all three to your auto trading program. Miss one and it can't connect.
The OKX API FAQ states that a key with trade or withdrawal permission and no IP bound is deleted after 14 days of inactivity. With an IP bound, the key doesn't expire from inactivity, and anyone who gets the key can't use it from anywhere else. One key can bind up to 20 IPs.
If you bind one, enter the public IP of the machine running the program, not your phone's or the one you browse from. A home connection's IP can change, and when it does, add the new IP to the whitelist.
With Blave Agent, the connect dialog on a cloud machine shows the machine's IP, so just enter that. The desktop app doesn't require binding, but if you don't, remember the 14-day rule.
These are the errors auto trading runs into most. The meanings follow the official OKX API docs (51008 follows how Blave's order code classifies it), and the fixes are general advice.
| Code | Meaning | What to do |
|---|---|---|
| First group: key and permission problems, rejected as soon as you connect or place an order | ||
| 50111 | Invalid API key | Check it was pasted correctly with no extra spaces at either end |
| 50113 | Invalid signature | Usually a wrong secret key. Paste it again |
| 50105 | Wrong passphrase | Enter the one you set when creating the key, not your login password |
| 50110 | IP not on the whitelist | Add the new IP of the machine running the program to the whitelist |
| 50119 | Key doesn't exist | It may have been deleted (for example by the 14-day rule). Create a new one |
| 50100 | API frozen | Contact OKX support |
| Second group: the key is fine, but this order can't go through | ||
| 51010 | Not supported in the current account mode | Spot mode can't trade futures. Switch to a mode that supports futures in the account mode setting |
| 51008 | Insufficient margin | Add funds or lower the strategy amount |
| 51020 | Below the minimum order size | Raise the strategy amount, or pick a coin with a smaller minimum unit |
| 50102 | Request timestamp expired | The clock on the machine running the program is off. Turn on automatic time sync |
| Third group: the result is uncertain, so be most careful | ||
| 50004 | API request timed out | Don't resend. Check the order first |
| 51149 | Order timed out | Don't resend. Check the order first |
| 50001 | Service temporarily unavailable | Don't resend. Check the order first |
| 50026 | System error | Don't resend. Check the order first |
The OKX docs state that 50004 and 51149 "does not mean that the request was successful or failed". The order may already have filled, and resending it would place it twice. Blave Agent never resends the same order on any of these four codes. For a futures market order it checks whether the order filled before deciding what to do next. Any other order stops and reports back, with no automatic retry.
If you write your own code against the OKX API, this is where things go wrong most often.
Blave Agent's order code already handles everything above, including syncing time with OKX on 50102 and never resending after a timeout. Paste the API key, secret key and passphrase into Auto Trading › Connect Account › OKX, and the strategy places orders on OKX by its rules.
Before connecting a real account, paper trade for a while. No key needed, and you start with 100,000 USDT.
Download the desktop appFor the full steps, see Set Up Exchange Trading Keys. For the whole path from idea to going live, see OKX Auto Trading: From Idea to Backtest to Live.
Do API keys expire?
A key with an IP bound doesn't expire from inactivity. A key with trade or withdrawal permission and no IP bound is deleted after 14 days of inactivity.
What if I forget the passphrase?
It can't be recovered. Delete the key, create a new one, and paste the three new values back into the program.
When during the day do orders time out most?
OKX advises avoiding 8:00, 16:00 and 24:00 (Taiwan time, UTC+8) each day. Those are the funding settlements, when server load peaks.
An API key is trading authority over your account, so don't paste it into sites or programs you don't trust. Never enable Withdraw, and delete keys you no longer use. Auto trading doesn't make losses go away. It only follows the rules. Leverage magnifies losses too.