Connecting to a crypto exchange API looks easy. You sign a request, you get JSON back, and the first trade goes through in an afternoon. The problems start later: a bot that gets its IP banned during a busy hour, an order book that quietly drifts from reality, an order that was filled while your code thinks it failed. This article covers the pitfalls we see most often in exchange integrations, in the three places they hide: rate limits, WebSocket streams and reconciliation.
Rate limits: more than "requests per second"
Every exchange limits how much you can ask of it, but rarely as a simple number of requests per second. Binance, for example, gives each REST endpoint a weight, counts the total weight per IP per minute, and limits the number of orders per account separately. Other exchanges count per API key, per endpoint group or per sub-account. If you treat all of that as one counter, you will hit a limit you did not know existed.
- Read the usage headers. Many exchanges tell you how much of your limit you have used in a response header. Binance does this with
X-MBX-USED-WEIGHT-1M. Use those numbers to slow down before you hit the limit, not after. - Treat a 429 as a stop sign. A "too many requests" response means back off, now. On Binance, continuing to send requests after a 429 escalates to a temporary IP ban (HTTP 418), which takes your whole system offline, not just the noisy part.
- Budget per process, not per request. If three services share one IP or one API key, they share one limit. Put a single rate limiter in front of the exchange, or give each service its own key where the exchange counts per key.
- Don't poll what you can stream. Fetching the order book or your open orders every second is the fastest way to burn your budget. Use WebSocket streams for market data and account updates, and keep REST for actions and for checks.
More background: what is a rate limit.
Authentication: clocks and nonces
Private endpoints are signed, usually with HMAC-SHA256 or a similar scheme. The signing itself rarely breaks. What breaks is the context around it:
- Clock drift. Exchanges that sign with a timestamp reject requests that arrive outside a time window. A server whose clock drifts a second or two starts failing at random. Run NTP on every machine that signs requests, and log the server time the exchange reports.
- Nonces under concurrency. Exchanges such as Kraken require a nonce that increases with every request per API key. Two workers using the same key will send nonces out of order and get rejected. Use one key per process, or route all signed requests through one sender.
- Key permissions. Give each key only what it needs: read-only for reporting, trading without withdrawals for bots, and an IP whitelist where the exchange supports one.
WebSocket streams: a connection is not a guarantee
WebSocket streams are the right way to follow prices, order books and your own orders in real time. But a stream that is connected is not the same as a stream that is complete.
- Connections end on purpose. Exchanges close connections after a maximum lifetime (Binance after 24 hours), during maintenance and when you don't answer pings. Reconnecting is normal operation, not an exception. Build it in with backoff and resubscribe to every channel afterwards.
- Silence is not "nothing happened". A connection can stay open while no data arrives. Track the time since the last message per channel, and treat a quiet channel as stale after a threshold that fits that market.
- Gaps after a reconnect. Everything that happened while you were disconnected is gone from the stream. For order books that means a resync; for your own orders it means asking REST what changed.
- Back-pressure. If your consumer is slower than the stream, messages queue up in memory and your "real-time" view falls behind. Measure the lag between the exchange timestamp and your processing time, and alert on it.
Order books: snapshot plus updates, in the right order
Most exchanges stream order book changes (diffs), not the full book. To build a correct local book you combine a REST snapshot with the stream, using the update IDs the exchange sends along. The usual procedure:
- Open the stream and buffer the updates.
- Fetch a snapshot over REST. It carries a last update ID.
- Drop buffered updates that are older than the snapshot, and check that the first remaining update connects to it.
- Apply updates in order. If an update ID is missing, the book is wrong: throw it away and start again from step 1.
Skipping the sequence check is the classic mistake. The book looks fine, prices move, and nobody notices that a level which was removed on the exchange is still in your copy. More on feeds: market data feed integrations.
Orders: the "unknown" outcome
The most expensive bugs in exchange integrations are about orders whose status you don't know. A request times out, the connection drops, or the exchange answers that the result is unknown. Binance even has an error for this (-1007: the order may or may not have been executed). If your code treats that as "failed" and retries, you can end up with two positions instead of one.
- Set your own order ID. Almost every exchange accepts a client order ID. Generate it before you send the order and store it. After a timeout, ask the exchange for that ID before you do anything else.
- Retry only what is safe to retry. Reading is safe. Placing an order is only safe to retry with the same client order ID, and only if the exchange rejects duplicates.
- Follow fills, not just orders. An order can fill in many parts, at different prices, with fees per part. Your position and average price come from the fills (trades), not from the order status.
Precision and the small print in symbol rules
Every trading pair has rules: a minimum price step (tick size), a minimum quantity step (lot or step size) and a minimum order value. On Binance these are the filters in exchangeInfo; other exchanges publish them per instrument. Orders that break them are rejected.
- Never use floating point for money. Most exchanges send prices and quantities as strings for a reason. Parse them into a decimal type and round to the symbol's step size, down for quantities so you never try to sell more than you have.
- Refresh the rules. Exchanges change tick sizes and minimums, and delist pairs. Load the rules at startup and refresh them on a schedule, rather than hard-coding them.
Reconciliation: the exchange is the source of truth
Whatever your system believes about balances, orders and positions, the exchange decides. Reconciliation means regularly comparing the two and explaining every difference.
- Fees change your balances. A fee can be charged in the asset you bought, the asset you paid with, or a separate fee token. If your bookkeeping assumes one of those, balances drift by small amounts that add up.
- Not everything is a trade. Deposits, withdrawals, transfers between sub-accounts, funding payments on perpetual futures, rebates and airdrops all change balances. Fetch each of those histories, or the balance will never match.
- Reconcile on a schedule, and after every reconnect. Compare open orders and balances with the exchange periodically, and always after a stream was down. Log every difference with its cause. A difference you cannot explain is a bug or a security problem, and both need attention.
- Keep your own ledger. Store every fill, fee and transfer with the exchange's own IDs. That makes reporting, tax and audits possible later, and it lets you rebuild your state when something goes wrong. See also crypto tax software and PnL calculators.
One integration, many exchanges
If you connect to more than one exchange, put a thin layer of your own between your logic and each exchange: one internal model for orders, fills and balances, and one adapter per exchange. Libraries such as CCXT help with the REST basics, but the parts in this article (streams, sequence checks, unknown outcomes, reconciliation) remain your responsibility. Each exchange has its own quirks in exactly those places.
Checklist before you go live
- One rate limiter per IP or API key, driven by the exchange's usage headers, with backoff on 429.
- NTP on every signing machine; one API key per process where the exchange uses nonces.
- Keys with minimal permissions and an IP whitelist; no withdrawal rights for bots.
- WebSocket reconnect with backoff, resubscribe, stale-channel detection and lag monitoring.
- Order books built from snapshot plus updates, with sequence checks and automatic resync.
- A client order ID on every order; after a timeout, query by that ID before any retry.
- Decimal types for all amounts; symbol rules loaded from the exchange and refreshed.
- Scheduled reconciliation of balances and open orders, also after every reconnect, with every difference logged.
Building or fixing an exchange integration? We connect trading systems to exchanges such as Binance, Bybit, Kraken and Coinbase. See also our crypto trading tools and WebSocket development, or get in touch.