๐ต 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.
- 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:
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
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 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
- 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 |
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.
Use the CCXT library for a unified interface to Binance and other exchanges. It handles authentication, rate limiting, and error handling automatically.