Error Handling
Error handling
Every OKX v5 REST response — success or failure — uses the same envelope, so you can handle results uniformly. Understanding this shape is the key to robust error handling.
The response envelope
All responses are JSON objects with three top-level fields:
Note that code is a string, not a number — compare against "0", not 0.
A successful response
A top-level error
When the whole request fails (bad authentication, malformed parameters, rate limit), code is non-zero and msg explains why:
Per-item errors on batch requests
Batch endpoints (such as POST /api/v5/trade/batch-orders) can partially succeed: some items go through while others fail. In that case the top-level code may still be non-zero to signal that not everything succeeded, and each element of data carries its own per-item status via sCode and sMsg:
sCode— the status code for that individual item ("0"= that item succeeded).sMsg— the message for that individual item.
Always inspect both levels: check the top-level code, and for batch operations, check each item’s sCode to learn which orders actually went through.
A minimal handling pattern
Representative error codes
Codes fall into ranges by category — general errors around 500xx, trading errors around 510xx. A few you’ll encounter often:
This is a small sample — each endpoint documents the specific codes it can return. Treat msg / sMsg as the authoritative human-readable explanation.
Troubleshooting checklist
- Compare
codeas a string ("0"), not an integer. - Authentication failures (
501xx) almost always trace back to signing: wrong secret, mismatched timestamp, unsigned query string, or a re-serialized body. See REST authentication. 50102(expired timestamp) means clock drift — run NTP.50011/ HTTP 429 means you’re hitting rate limits — throttle and back off.- Partial batch failures won’t show up if you only check the top-level
code; always read per-itemsCode. - Empty
datawith a non-zerocodeis normal for a fully failed request — the reason is inmsg.

