Skip to main content
๐Ÿ“– Tronsell Wiki

Binance API Guide: Complete Guide to Binance API Integration

Complete guide to integrating with Binance's REST and WebSocket APIs for spot, futures, and margin trading. Learn authentication, key endpoints, rate limits, and best practices for building production-ready applications.

๐Ÿ”ต Binance API at a Glance
API Version v3 (REST) ยท WebSocket Streams
Rate Limit 1200 weight/min
Products Spot, Futures (USD-M/COIN-M), Margin
Testnet testnet.binance.vision (spot) ยท testnet.binancefuture.com (futures)
Auth Method API Key + HMAC SHA256 Signature
Best For Trading bots, data analysis

๐Ÿ”ต Binance API Overview

The Binance API is one of the most comprehensive and widely used cryptocurrency APIs in the world. It provides programmatic access to Binance's spot, futures, and margin trading platforms. The API supports both REST (for one-off operations) and WebSocket (for real-time streaming).

Binance offers separate APIs for different products:

  • Spot API: Trading on the main exchange (base URL: api.binance.com).
  • Futures API (USD-M): USD-margined futures (base URL: fapi.binance.com).
  • Futures API (COIN-M): Coin-margined futures (base URL: dapi.binance.com).
  • Margin API: Margin trading (uses spot endpoints with margin parameters).
  • Wallet API: Deposit and withdrawal operations.
๐Ÿ’ก Why Binance API is Popular
  • Comprehensive: Covers spot, futures, margin, and wallet operations.
  • Reliable: High uptime and low latency.
  • Well-documented: Detailed documentation with examples.
  • Large ecosystem: Many libraries and tools built on top of it.

๐Ÿš€ Getting Started with Binance API

Before you can use the Binance API, you need to set up your credentials and understand the authentication process.

1. Get Your API Keys

  • 1
    Log in to your Binance account

    Go to the Binance website and log in.

  • 2
    Navigate to API Management

    Go to Account Settings โ†’ API Management.

  • 3
    Create a new API key

    Click "Create API Key," enter a label, and set permissions (Read, Trade, Withdrawal). For most trading bots, enable Read and Trade.

  • 4
    Enable IP whitelisting (recommended)

    Add the IP address of your server to the whitelist for enhanced security.

  • 5
    Save your API key and secret

    Copy both the API key and secret. The secret is shown only once โ€” store it securely.

2. Authentication

Binance API uses HMAC SHA256 signature authentication for all private endpoints. Here's how it works:

signature = HMAC_SHA256(queryString, apiSecret)
The query string includes all parameters and the timestamp

3. Use the Testnet

Always test on the Binance testnet before deploying with real funds.

  • Spot Testnet: testnet.binance.vision
  • Futures Testnet: testnet.binancefuture.com
๐Ÿ’ก Pro Tip

Use the testnet to test your API integration without risking real money. You can get free test funds from the testnet faucet.

๐Ÿ“‹ Key Binance API Endpoints

Here are the most important endpoints for spot trading on Binance.

Category Endpoint Description Method
Market Data /api/v3/exchangeInfo Get exchange trading rules and symbol info GET
Market Data /api/v3/ticker/price Get current price GET
Market Data /api/v3/klines Get candlestick data GET
Market Data /api/v3/depth Get order book depth GET
Account /api/v3/account Get account balances GET
Orders /api/v3/order Place a new order POST
Orders /api/v3/order Cancel an order DELETE
Orders /api/v3/openOrders Get all open orders GET
Orders /api/v3/allOrders Get order history GET
Orders /api/v3/myTrades Get trade history GET
๐Ÿ’ก Futures vs Spot Endpoints

Futures endpoints use /fapi/v1/ (USD-M) or /dapi/v1/ (COIN-M) instead of /api/v3/. Always use the correct endpoint for your trading product.

๐Ÿ”„ Binance WebSocket Streams

Binance WebSocket streams provide real-time data for spot and futures markets.

Public WebSocket Streams

  • Price Ticker: wss://stream.binance.com:9443/ws/btcusdt@ticker
  • Order Book: wss://stream.binance.com:9443/ws/btcusdt@depth
  • Trade Stream: wss://stream.binance.com:9443/ws/btcusdt@trade
  • Candlestick: wss://stream.binance.com:9443/ws/btcusdt@kline_1m

User Data Streams (Authenticated)

  • Account updates: Balance changes
  • Order updates: Order status changes
  • Trade updates: Trade execution
๐Ÿ“Œ WebSocket Best Practices
  • Use combined streams for multiple symbols in one connection.
  • Implement auto-reconnection with exponential backoff.
  • Send ping/pong keepalive messages.
  • Subscribe only to streams you need to minimize bandwidth.

๐Ÿšฆ Binance API Rate Limits

Binance uses a weight-based rate limiting system. Each endpoint has a weight, and you're limited to 1200 weight per minute.

Endpoint Weight Description
/api/v3/exchangeInfo 10 Exchange info
/api/v3/ticker/price 1 Price ticker
/api/v3/klines 1 Candlestick data
/api/v3/depth 1-5 Order book depth (depends on limit)
/api/v3/account 10 Account balance
/api/v3/order (POST) 1 Place order
/api/v3/order (DELETE) 1 Cancel order
/api/v3/openOrders 3 Get open orders
/api/v3/allOrders 10 Get order history
๐Ÿ’ก Rate Limit Headers

Monitor these headers in responses: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, and X-RateLimit-Used. Always check remaining weight before sending requests.

โš ๏ธ Common Binance API Errors

Here are the most common Binance API errors and how to fix them.

Error Code Message Cause Solution
-1021 Timestamp out of sync System time is off Synchronize system time via NTP
-1022 Signature invalid Incorrect signature Check signature algorithm and order
-1121 Invalid symbol Symbol doesn't exist Verify symbol is correct
-1128 Invalid quantity Quantity below min or not step-sized Check exchange filters for the symbol
-1130 Invalid price Price below min or not tick-sized Check exchange filters for the symbol
-2010 Order failed Insufficient balance, order rejected Check balance, parameters, market conditions
-2013 Order does not exist Order ID not found Verify order ID before querying
-2015 Invalid API key API key doesn't exist or is disabled Verify API key is active and correct

๐Ÿ† Binance API Best Practices

Follow these best practices for reliable Binance API integration.

  • Always use the testnet first: Test all code on testnet before deploying with real funds.
  • Enable rate limiting: Use a library like CCXT that handles rate limiting automatically.
  • Secure your API keys: Use environment variables, IP whitelisting, and least privilege permissions.
  • Monitor rate limit headers: Track your weight usage to avoid hitting limits.
  • Use WebSocket for real-time data: Avoid polling REST endpoints for price updates.
  • Implement error handling: Handle common errors like -1021 (timestamp) and HTTP 429 (rate limit).
  • Use client order IDs: Assign unique IDs to orders for better tracking.
  • Stay updated: Binance API changes frequently โ€” keep your libraries updated.
๐Ÿ’ก Pro Tip

Use the CCXT library for a unified interface to Binance and other exchanges. It handles authentication, rate limiting, and error handling automatically.

โ“ Frequently Asked Questions About Binance API

How do I get a Binance API key?

To get a Binance API key: 1) Log in to your Binance account. 2) Go to Account Settings โ†’ API Management. 3) Click 'Create API Key.' 4) Enter a label and set permissions (Read, Trade, Withdrawal). 5) Complete 2FA verification. 6) Copy and save your API key and secret immediately. The secret is shown only once.

What is the difference between Binance Spot API and Futures API?

Binance Spot API is for spot trading on the main exchange. Binance Futures API is for futures and perpetual contracts. The endpoints differ: spot uses /api/v3/, futures uses /fapi/v1/ (USD-M) or /dapi/v1/ (COIN-M).

What are Binance API rate limits?

Binance uses weight-based rate limits. The limit is 1200 weight per minute. Different endpoints have different weights โ€” simple requests cost 1-5 weight, while complex order placement costs more. Always check the response headers (X-RateLimit-*) to monitor usage.

How do I fix Binance API error -1021?

Error -1021 means 'Timestamp for this request is outside of the recvWindow.' This occurs when your system time is out of sync with Binance's server time. Fix by synchronizing your system time using NTP and/or increasing the recvWindow parameter (default is 5000ms).

Does Binance have a testnet for API testing?

Yes, Binance offers testnet environments for both spot and futures. The spot testnet is available at testnet.binance.vision, and the futures testnet is available at testnet.binancefuture.com. Both provide free test funds for safe API testing.

What permissions should I set for my Binance API key?

For most trading bots, enable Read (to view balances and market data) and Trade (to place orders). Never enable Withdrawal unless absolutely necessary. Use IP whitelisting for additional security.

Can I use WebSocket for Binance API?

Yes, Binance provides WebSocket streams for real-time data. Public streams include price tickers, order books, and trade streams. User data streams (authenticated) provide account and order updates. WebSocket is recommended for real-time applications.

What are the best libraries for Binance API?

The most popular libraries are CCXT (unified API for multiple exchanges), python-binance (Python), and binance-api-node (Node.js). CCXT is recommended for multi-exchange support, while python-binance is a dedicated Python library.

๐Ÿ”ต Start Building with Binance API

Integrate with Binance's powerful API for spot, futures, and margin trading. Start with the testnet, secure your keys, and build reliable trading applications.