This guide walks you from a blank project to a working Bybit integration. You will install the SDK, create API credentials, load them safely via environment variables, fetch public market data, authenticate a private REST call, and subscribe to a live WebSocket stream.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.
Prerequisites
Before you begin, make sure you have the following installed:- Node.js 16 or later — check with
node --version - npm, yarn, or pnpm — any one of them works
Public market data calls do not require API keys. For any private endpoint — account info, order placement, position reads — you will need credentials.
Use testnet keys while developing. Testnet is a safe sandbox where you can verify endpoint connectivity and permissions without any risk to real funds. When you are ready to go live, swap in your live credentials.
Always grant the minimum permissions your integration actually needs. A market-data monitor does not need trading permissions. A trading bot does not need withdrawal permissions. Use IP whitelisting whenever your deployment environment allows it.
For local development you can use a
.env file and a package like dotenv:Public market data endpoints do not require API credentials. Instantiate
RestClientV5 with no arguments to make public calls:import { RestClientV5 } from 'bybit-api';
// No credentials needed for public market data
const client = new RestClientV5();
async function main() {
// Fetch the current BTCUSDT order book (linear perpetual)
const orderBook = await client.getOrderbook({
category: 'linear',
symbol: 'BTCUSDT',
limit: 50,
});
console.log('Order book:', orderBook.result);
// Fetch the latest ticker for BTCUSDT
const ticker = await client.getTickers({
category: 'linear',
symbol: 'BTCUSDT',
});
console.log('Ticker:', ticker.result.list[0]);
// Fetch recent 5-minute candles
const candles = await client.getKline({
category: 'linear',
symbol: 'BTCUSDT',
interval: '5',
limit: 5,
});
console.log('Candles:', candles.result.list);
}
main().catch(console.error);
Provide your API key and secret in the constructor. The SDK automatically attaches timestamps and HMAC signatures to every private request — you never need to sign requests manually.
import { RestClientV5 } from 'bybit-api';
const client = new RestClientV5({
key: process.env.BYBIT_API_KEY,
secret: process.env.BYBIT_API_SECRET,
// Uncomment to use the testnet environment:
// testnet: true,
});
async function main() {
// Fetch account information (private endpoint — requires credentials)
const accountInfo = await client.getAccountInfo();
console.log('Account info:', accountInfo.result);
// Fetch UNIFIED wallet balance
const wallet = await client.getWalletBalance({
accountType: 'UNIFIED',
});
console.log('Wallet balance:', wallet.result.list[0]);
// Fetch open positions for BTCUSDT linear perpetual
const positions = await client.getPositionInfo({
category: 'linear',
symbol: 'BTCUSDT',
});
console.log('Positions:', positions.result.list);
}
main().catch(console.error);
The
WebsocketClient manages all WebSocket connections for you — it handles connecting, authenticating, heartbeats, reconnections, and resubscriptions automatically. Attach event listeners, then call subscribeV5 with the topics you want.import { WebsocketClient } from 'bybit-api';
// Public WebSocket — no credentials required for public topics
const ws = new WebsocketClient();
// Listen for streaming data
ws.on('update', (data) => {
console.log('Stream update:', JSON.stringify(data, null, 2));
});
// Optional: connection lifecycle events
ws.on('open', ({ wsKey }) => {
console.log('Connection opened:', wsKey);
});
ws.on('response', (response) => {
console.log('Subscribe response:', response);
});
ws.on('reconnect', ({ wsKey }) => {
console.log('Reconnecting:', wsKey);
});
ws.on('reconnected', ({ wsKey }) => {
console.log('Reconnected:', wsKey);
});
// Log errors
ws.on('exception', (err) => {
console.error('WebSocket error:', err);
});
// Subscribe to public topics on the linear (USDT perpetual) market
ws.subscribeV5(
['orderbook.50.BTCUSDT', 'publicTrade.BTCUSDT', 'tickers.BTCUSDT'],
'linear',
);
import { WebsocketClient } from 'bybit-api';
const ws = new WebsocketClient({
key: process.env.BYBIT_API_KEY,
secret: process.env.BYBIT_API_SECRET,
});
ws.on('authenticated', ({ wsKey }) => {
console.log('Authenticated:', wsKey);
});
ws.on('update', (data) => {
console.log('Account event:', JSON.stringify(data, null, 2));
});
ws.on('exception', console.error);
// Subscribe to private account topics
ws.subscribeV5(['order', 'execution', 'position', 'wallet'], 'linear');
Next steps
Authentication
Learn how HMAC and RSA key types work, configure recvWindow, and set up environment variables securely.
REST API
Browse all RestClientV5 methods — market data, order management, positions, wallet, and more.
WebSockets
Explore all public and private stream topics and learn how to use the WebSocket API for order commands.
Configuration
Tune recv_window, enable rate-limit parsing, configure regional domains, and set up keep-alive connections.