Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tiagosiebler/bybit-api/llms.txt
Use this file to discover all available pages before exploring further.
bybit-api handles all authentication automatically. Pass your API key and secret to the client constructor once, and every subsequent private request is signed with the correct algorithm, timestamp, and headers — no manual signing, no raw header management. The SDK supports both HMAC-SHA256 and RSA-SHA256 authentication and detects which algorithm to use based on the shape of the secret value you provide.
How authentication works
For every private REST API request, the SDK:- Takes the current timestamp (in milliseconds).
- Serialises the request parameters into a canonical string.
- Signs the string using HMAC-SHA256 (for standard API secrets) or RSA-SHA256 (for PEM private keys).
- Attaches the required
X-BAPI-API-KEY,X-BAPI-SIGN,X-BAPI-SIGN-TYPE, andX-BAPI-TIMESTAMPheaders automatically.
secret string contains the substring PRIVATE KEY. If it does, RSA-SHA256 with base64 encoding is used. Otherwise, HMAC-SHA256 with hex encoding is used. No extra configuration flag is required.
Creating API keys
Create credentials from the Bybit API Management page for the environment you are targeting:- Live environment: bybit.com — API Management
- Testnet environment: testnet.bybit.com — API Management
- Demo trading notes: Bybit Demo Trading Service
HMAC authentication
HMAC is the standard Bybit API key type. Bybit generates a key string and a secret string for you. Pass both directly to the client constructor:WebsocketClient and WebsocketAPIClient:
RSA authentication
Bybit supports self-generated RSA key pairs. You provide Bybit with your public key when creating the API key; Bybit returns a standard-looking API key string. You then pass that key string askey and your private PEM key as secret.
Generating an RSA key pair
Useopenssl to generate a 4096-bit private key and the matching public key:
rsa-public-key.pem to Bybit when creating the API key using the “Self-generated” option. Bybit will provide you with an API key string. Keep your private key completely secret — never share it, never commit it to source control.
Configuring the RSA client
Pass the Bybit-provided API key askey and the full PEM private key (including the -----BEGIN PRIVATE KEY----- header and -----END PRIVATE KEY----- footer) as secret. The SDK detects the PEM header and switches to RSA-SHA256 signing automatically:
RSA detection is automatic. The SDK looks for the substring
PRIVATE KEY anywhere in the secret value. If found, RSA-SHA256 with base64 encoding is used. For plain HMAC secrets — which never contain those words — HMAC-SHA256 with hex encoding is used.Environment variable best practices
Store credentials in environment variables, not in source files. For local development, use a.env file (added to .gitignore) and a package such as dotenv:
\n characters, then restored at runtime:
.env files.
Constructor options reference
The followingRestClientOptions fields are relevant to authentication and environment configuration. Pass them as the first argument to RestClientV5, WebsocketClient, or WebsocketAPIClient.
Your Bybit API key. Required for all private endpoints. For HMAC authentication this is the key string generated by Bybit. For RSA authentication this is the key string Bybit provides after you upload your public key.
Your API secret. For HMAC authentication, this is the secret string generated by Bybit. For RSA authentication, this is your full PEM private key including the
-----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- lines. The SDK detects RSA keys automatically from the PEM header.Set to
true to connect to Bybit’s testnet environment. Testnet uses separate API domains and separate credentials. Do not combine with demoTrading: true.Set to
true to connect to Bybit’s V5 demo trading environment. Demo trading uses mainnet domains with a simulated account and separate demo API keys. Do not combine with testnet: true. Note: as of January 2025, the WebSocket API is not supported in demo trading.The maximum allowed age (in milliseconds) of a request timestamp, as enforced by Bybit. Requests whose timestamp differs from Bybit’s server clock by more than this value are rejected. The default of
5000 (5 seconds) is sufficient for most environments. Increase this value only if you experience consistent clock-drift rejection errors, and fix your system clock sync first.Required API permissions by use case
Grant the minimum permissions your integration needs. Do not enable permissions that your code does not use.| Use case | Required permissions |
|---|---|
| Public market data only | None — no API key required |
| Account reads (balances, positions, orders) | Read-only |
| Order management (submit, amend, cancel) | Trade |
| Spot margin trading | Trade + Spot Margin Trade |
| Asset transfers between sub-accounts | Transfer |
| Withdrawals | Withdraw — only add this if absolutely necessary |
IP whitelisting
Bybit allows you to restrict an API key so it only accepts requests from specific IP addresses. This is one of the most effective mitigations against credential theft. Enable IP whitelisting for every production key where your deployment environment has a fixed egress IP. Configure IP restrictions in the Bybit API Management dashboard when creating or editing an API key.RecvWindow configuration
Therecv_window option (for REST) and recvWindow option (for WebSockets) control how much clock drift Bybit will tolerate on signed requests. The defaults are almost always sufficient. If you encounter 10002 (recv window exceeded) errors, check your system clock synchronisation before increasing the window.
recvWindow on the WebSocket client:
Debugging requests with BYBITTRACE
In development, you can enable verbose HTTP response logging by setting theBYBITTRACE environment variable to true. When enabled, the SDK logs the full request URL, method, headers, parameters, and Bybit’s response for every API call.
process.env.BYBITTRACE. There is no runtime toggle; restart your process with or without the variable to enable or disable tracing.
Next steps
Quickstart
Step-by-step guide to making your first REST and WebSocket calls.
REST API
Full reference for all RestClientV5 endpoints and methods.
WebSockets
Subscribe to market and account streams with automatic reconnection.
Configuration
Advanced options: regional domains, rate-limit parsing, keep-alive, and custom signing.