Binance API SDK in production
Keep account state up to date
Use REST to submit orders and private WebSocket subscriptions to follow what happens next. Order, fill, balance and position events update your application's account state as changes occur.
- Connect the private streams and watch for subscription acknowledgements or errors before submitting orders. Load any starting state the streams do not provide, and account for events received while that data loads.
- Submit the order through REST. Save the returned IDs and inspect the response for errors or immediate results. An acknowledgement alone does not establish the order's later state.
- Apply matching WebSocket events to local state. Use order and fill events to track execution, and balance or position updates for the account changes your application needs. These events can arrive separately.
- Use REST to fill a specific gap: startup data, updates missed during a disconnect, conflicting events, or an outcome that neither the response nor the stream has resolved. Once account state is current, continue applying WebSocket updates.
Do not query REST after every submission or poll it while the required streams are healthy and complete. A quiet stream can mean nothing has changed. Check connection and subscription health separately from account activity.
Trigger follow-up work from position changes
Where the exchange and market provide position updates, they are useful triggers for follow-up logic. For example, when a position opens or increases, use its updated size to place or adjust stop-loss and take-profit (SL/TP) orders. Compare the previous and current position state; receiving another position message does not necessarily mean the position changed.
For signed position sizes, compare absolute exposure within the same direction and handle a direction change separately. Also resize or cancel obsolete protection when exposure shrinks or the position closes.
Debounce bursts of partial-fill updates to reduce API calls, and set a maximum wait so continuous updates cannot postpone adjustments indefinitely. Choose the debounce delay and maximum wait to fit the exchange's rate limits and the delay your strategy can tolerate.
When applying an adjustment:
- Check stream health and use the latest reconciled position and order state.
- Process changes for the same position sequentially and skip adjustments that are already satisfied.
- Pause dependent adjustments while recovering account state.
- Include the account, market and position side in state keys where applicable.
Recover after a connection gap
Use the SDK's reconnect lifecycle events to mark affected state as stale when connection recovery starts and to trigger REST reconciliation when the socket reopens. The event names differ by SDK; see the recovery section below. A reopened socket does not prove that authentication, subscriptions or account-state recovery are complete.
Confirm the required streams are healthy, load any missed order, fill, balance or position state, and account for updates received during those reads. Then compare current positions and open orders with the intended state and create, amend or cancel orders only where needed, including SL/TP orders. Resolve uncertain requests before submitting replacements so recovery does not create duplicates. Resume dependent actions once their required state is current.
See Account State for the full workflow. The private stream guide shows how to receive updates; the examples print events rather than maintain a complete account-state store.
Set timeouts and bound concurrency
Set an HTTP timeout for each operation and limit concurrent requests across workers. See Configuration for client options.
Retry reads deliberately
Retry transient read failures with a maximum attempt count or deadline, exponential backoff and jitter. Respect server retry guidance. Correct invalid parameters, permissions and signatures before retrying.
Stay within rate limits
Binance limits REST traffic by request weight per IP address and order placement per account. Read the current limits from getExchangeInfo(), and read your recent usage from getRateLimitStates(). Slow down before you hit the limit rather than waiting for an HTTP 429.
The Binance API rate limits guide covers the Spot and USD-M figures, how to refresh the usage cache with getServerTime(), the backfill example, HTTP 429 and HTTP 418, and when to prefer WebSocket streams.
Resolve uncertain order outcomes
A timeout does not prove that Binance rejected an order. First use matching private order and trade events to determine what happened. If the outcome is still unclear, query getOrder() with the symbol and returned orderId. If no ID is available, use open orders, order history and trade history for the same account and symbol to resolve the request before submitting again. For derivatives, use the matching futures client's methods.
Save the returned order ID with the account, network and symbol. Track acknowledgement, acceptance and fills separately.
Recover streaming state
The client reconnects and restores subscriptions automatically. reconnecting signals that connection recovery is starting; mark the affected account state as stale. reconnected means the socket has reopened and is a useful trigger for REST reconciliation of missed orders, fills, balances and, for futures, positions. Listen for exception to diagnose errors.
Authentication and subscription recovery may still be completing when reconnected fires. The SDK handles authentication and resubscription; do not add a manual resubscription loop. Account for updates received during REST recovery, and reconcile open orders with the current position before making follow-up changes. Do not blindly recreate orders after reconnecting.
For a local order book, follow Binance's documented snapshot and depth-update sequence rules. Reconnecting a socket alone does not rebuild an accurate book.
Track feed freshness and finish recovery before using affected data for new orders.
Logging
Record operation names, response codes, duration, retries and reconnects. Select fields using the SDK's error format; whole request and error objects can contain credentials.
Shut down cleanly
Stop new work, settle or reconcile outstanding requests, call WebsocketClient.closeAll() and clear application timers. The stream example includes shutdown handlers.
Upgrading
Review release notes and run integration checks before upgrading. Commit the lockfile to reproduce deployed versions.