NOONOO TRADINGStart in the bot

Exchange API Integration: REST, WebSockets, Authentication and Security

A trading bot reads prices and places orders through exchange APIs. This guide explains the roles of REST and WebSockets, request signing, rate limits and key security.

REST versus WebSockets

REST uses request and response, making it suitable for actions such as placing or canceling an order and querying balances. WebSockets maintain a connection for server-pushed updates, such as price, order-book and private order-status changes.

CategoryRESTWebSocket
DirectionClient request → server responseServer pushes after connection
Latency in the guide's comparisonTens to hundreds of millisecondsA few milliseconds, described as real time
UsesOrders, cancellation, balances and historyPrices, order books and trade streams
ConcernRepeated calls consume request limitsDisconnections require reconnection

The workflow presented here uses WebSockets for market data and REST for order submission. Prices arrive through the stream, signals are evaluated, and a decision triggers a REST order. Fast-response methods such as scalping depend more heavily on streaming data.

Authentication and signing

Market data is often public, but orders and balances require private authentication. The guide describes a common model using an API key and secret key with an HMAC-SHA256 signature.

  1. Include a current timestamp. The original guide uses roughly ±5 seconds from server time as its example tolerance; clock mismatch can cause rejection.
  2. Build the signing string from the path, parameters and timestamp.
  3. Use the secret key to calculate the HMAC signature and attach it to the request headers.
  4. The exchange recomputes the signature and accepts authentication if it matches.
Example A schematic balance request: create timestamp → sign GET /account?timestamp=... → attach X-API-KEY and X-SIGNATURE headers → receive USDT balance and asset quantities. The secret is used locally for signing and is not transmitted.

The basic order and balance workflow

A typical automated-trading cycle follows these steps:

Before live use, validate order handling in an exchange testnet or simulated environment and through sufficient backtesting.

Rate limits

Exchanges impose request limits, commonly by calls per second or minute, or by accumulated request weights. With an illustrative limit of 1,200 weight units per minute, requests costing five units could be made at most 240 times per minute if no other calls consumed the budget.

API-key security: permissions and IP restrictions

An API key is an access credential. Issue it using least privilege, because exposure can endanger the account.

PermissionRecommended setting in the guide
Read: prices and balancesEnable only as needed
Trade: orders and cancellationEnable only for the trading bot's needs
WithdrawDisable

These additional practices are central to API-key management:

Risk note: A working API connection does not guarantee profit. Disconnects, exchange maintenance, slippage, clock errors and code bugs can cause real losses. Validate a trading bot thoroughly with a testnet and small amounts, and operate only within a capital limit you can afford to lose.

NOONOO TRADING invites you to follow live trading in our free chat.

Start in the bot

📈 OKX trading fee discount for new registrations

Register for the OKX Fee Discount →