π£ Bybit API Overview
The Bybit API is a robust and well-documented API that provides programmatic access to Bybit's spot, futures, and options trading platforms. Bybit uses a unified v5 API structure, making it easier for developers to integrate with different product types.
Bybit's API is known for its:
- Unified v5 API: Consistent endpoints across spot, futures, and options.
- High performance: Low latency and high throughput.
- Comprehensive WebSocket: Real-time data with low latency.
- Developer-friendly: Clear documentation and SDKs.
- Unified v5 API: One API for all products.
- WebSocket streams: Real-time data with low latency.
- Testnet support: Safe testing with free test funds.
- Sub-account support: Manage multiple accounts via API.
π Getting Started with Bybit API
Before you can use the Bybit API, you need to set up your credentials and understand the authentication process.
1. Get Your API Keys
-
1
Log in to your Bybit account
Go to the Bybit website and log in.
-
2
Navigate to API Management
Go to Account & Security β API Management.
-
3
Create a new API key
Click "Create New API Key," enter a name, and set permissions (Read, Trade, Withdraw).
-
4
Set IP whitelist (recommended)
Add the IP address of your server for enhanced security.
-
5
Save your API key and secret
Copy the API key and secret. The secret is shown only once β store it securely.
2. Authentication
Bybit API uses HMAC SHA256 signature authentication. Here's how it works:
3. Use the Testnet
Always test on the Bybit testnet before deploying with real funds.
Bybit Testnet: testnet.bybit.com β Create testnet API keys and get free test funds.
π Key Bybit API Endpoints
Bybit's v5 API uses a unified structure with different endpoint categories.
| Category | Endpoint | Description | Method |
|---|---|---|---|
| Market Data | /v5/market/tickers |
Get ticker prices | GET |
| Market Data | /v5/market/kline |
Get candlestick data | GET |
| Market Data | /v5/market/orderbook |
Get order book depth | GET |
| Account | /v5/account/wallet-balance |
Get account balances | GET |
| Orders | /v5/order/create |
Place a new order | POST |
| Orders | /v5/order/cancel |
Cancel an order | POST |
| Orders | /v5/order/realtime |
Get open orders | GET |
| Orders | /v5/order/history |
Get order history | GET |
| Orders | /v5/order/trade-history |
Get trade history | GET |
| Futures | /v5/position/list |
Get futures positions | GET |
Bybit v5 API uses different endpoint categories: /v5/market/* for public data, /v5/account/* for account, /v5/order/* for orders, and /v5/position/* for positions.
π Bybit WebSocket Streams
Bybit WebSocket streams provide real-time data for all product types.
Public WebSocket Streams
- Price Ticker:
wss://stream.bybit.com/v5/public/spot - Order Book:
wss://stream.bybit.com/v5/public/spot - Trade Stream:
wss://stream.bybit.com/v5/public/spot - Candlestick:
wss://stream.bybit.com/v5/public/spot
Private WebSocket Streams
- Account updates: Balance and position changes
- Order updates: Order status changes
- Trade updates: Trade execution
- Use multiple topics in a single connection.
- Implement auto-reconnection with exponential backoff.
- Use ping/pong keepalive messages.
- Authenticate for private streams using the login message.
π¦ Bybit API Rate Limits
Bybit has a rate limit of 50 requests per second per API key for most endpoints. WebSocket connections have separate limits.
| Endpoint | Rate Limit | Notes |
|---|---|---|
/v5/market/* |
50 req/sec | Market data endpoints |
/v5/order/* |
50 req/sec | Order placement and management |
/v5/account/* |
50 req/sec | Account information |
/v5/position/* |
50 req/sec | Position management |
Monitor these headers in responses: x-ratelimit-limit, x-ratelimit-remaining, x-ratelimit-reset. Always check remaining limit before sending requests.
β οΈ Common Bybit API Errors
Here are the most common Bybit API errors and how to fix them.
| Error Code | Message | Cause | Solution |
|---|---|---|---|
| 10001 | Invalid request | Malformed request | Check request format and parameters |
| 10002 | Invalid API key | API key doesn't exist or is disabled | Verify API key is active and correct |
| 10003 | Invalid signature | Incorrect signature generation | Check signature algorithm and order |
| 10004 | Timestamp out of sync | System time is off | Synchronize system time via NTP |
| 10005 | IP not whitelisted | IP not in whitelist | Add IP to whitelist in API settings |
| 11001 | Insufficient balance | Not enough funds | Check balance before placing orders |
| 11002 | Invalid symbol | Symbol doesn't exist | Verify symbol is correct |
| 11003 | Invalid quantity | Quantity below min or not step-sized | Check exchange filters for the symbol |
| 11004 | Invalid price | Price below min or not tick-sized | Check exchange filters for the symbol |
| 11005 | Order not found | Order ID not found | Verify order ID before querying |
π Bybit API Best Practices
Follow these best practices for reliable Bybit API integration.
- Always use the testnet first: Test all code on testnet before deploying with real funds.
- Secure your API keys: Use environment variables, IP whitelisting, and least privilege permissions.
- Monitor rate limit headers: Track your request count to avoid hitting limits.
- Use WebSocket for real-time data: Avoid polling REST endpoints for price updates.
- Implement error handling: Handle common errors like 11001 (insufficient balance) and 10004 (timestamp).
- Use client order IDs: Assign unique IDs to orders for better tracking.
- Stay updated: Bybit API changes frequently β keep your libraries updated.
- Use the correct product type: Specify
categorycorrectly for spot, futures, or options.
Use the CCXT library for a unified interface to Bybit and other exchanges. It handles authentication, rate limiting, and error handling automatically.