Install and set up: code
npm install binance
import { MainClient, USDMClient } from 'binance';
const client = new MainClient({
api_key: process.env.BINANCE_API_KEY,
api_secret: process.env.BINANCE_API_SECRET,
});
const futures = new USDMClient({
api_key: process.env.BINANCE_API_KEY,
api_secret: process.env.BINANCE_API_SECRET,
});
|
API keys
- Public market data needs no key: new MainClient() is enough.
- Keep keys in environment variables, never in code.
- Give a bot only the permissions it uses (Reading, Spot and Margin Trading, Futures). Leave Withdrawals off.
- Add an IP restriction for production servers.
- HMAC, RSA and Ed25519 keys all work with the SDK.
- Practise first: pass testnet: true (Spot testnet) or demoTrading: true (demo trading) to the client options. |
Spot market data
await client.getServerTime();
await client.getExchangeInfo({ symbol: 'BTCUSDT' });
await client.getSymbolPriceTicker({ symbol: 'BTCUSDT' });
await client.get24hrChangeStatistics({ symbol: 'BTCUSDT' });
await client.getKlines({ symbol: 'BTCUSDT', interval: '1h', limit: 100 });
await client.getOrderBook({ symbol: 'BTCUSDT', limit: 20 });
|
Spot orders
await client.getAccountInformation();
// validate first, nothing is placed
await client.testNewOrder({
symbol: 'BTCUSDT', side: 'BUY', type: 'LIMIT',
quantity: 0.001, price: 30000, timeInForce: 'GTC',
});
const order = await client.submitNewOrder({
symbol: 'BTCUSDT', side: 'BUY', type: 'LIMIT',
quantity: 0.001, price: 30000, timeInForce: 'GTC',
});
await client.getOpenOrders({ symbol: 'BTCUSDT' });
await client.cancelOrder({ symbol: 'BTCUSDT', orderId: order.orderId });
|
|
|
USD-M futures
await futures.getMarkPrice({ symbol: 'BTCUSDT' });
await futures.getFundingRateHistory({ symbol: 'BTCUSDT', limit: 10 });
await futures.setLeverage({ symbol: 'BTCUSDT', leverage: 5 });
await futures.submitNewOrder({
symbol: 'BTCUSDT', side: 'BUY', type: 'MARKET', quantity: 0.002,
});
await futures.getPositionsV3({ symbol: 'BTCUSDT' });
await futures.getBalanceV3();
|
WebSocket market streams
import { WebsocketClient } from 'binance';
const ws = new WebsocketClient({ beautify: true });
ws.on('formattedMessage', (data) => console.log(data));
ws.on('reconnected', ({ wsKey }) => console.log('reconnected', wsKey));
ws.on('exception', console.error);
// Spot
ws.subscribe(['btcusdt@trade', 'btcusdt@kline_1m'], 'main');
// USD-M futures
ws.subscribe(['btcusdt@markPrice@1s', '!forceOrder@arr'], 'usdm');
|
Private user data
import { WebsocketAPIClient, WebsocketClient, WS_KEY_MAP } from 'binance';
// Spot: subscription on the WebSocket API
const wsApi = new WebsocketAPIClient({
api_key: process.env.BINANCE_API_KEY,
api_secret: process.env.BINANCE_API_SECRET,
});
wsApi.getWSClient().on('formattedUserDataMessage', console.log);
await wsApi.subscribeUserDataStream(WS_KEY_MAP.mainWSAPI);
// USD-M futures
const wsFutures = new WebsocketClient({
api_key: process.env.BINANCE_API_KEY,
api_secret: process.env.BINANCE_API_SECRET,
beautify: true,
});
wsFutures.on('formattedUserDataMessage', console.log);
await wsFutures.subscribeUsdFuturesUserDataStream();
|
Good to know
- Spot REST requests share a request weight budget of 6,000 per minute per IP (live exchangeInfo, 2 Dec 2026); USD-M has 2,400.
- Spot account events now come from the WebSocket API subscription (Block 7). The old Spot listenKey request (POST /api/v3/userDataStream) has returned HTTP 410 since 20 Feb 2026.
- USD-M streams are split into /public, /market and /private routes. Regular market streams such as markPrice, kline, aggTrade and forceOrder only arrive on /market. The SDK picks the right route for each topic.
- A WebSocket connection lasts at most 24 hours. The SDK reconnects and resubscribes; refresh private state on 'reconnected'.
- Error -1021 means the request timestamp is outside recvWindow: check the machine clock.
- Error -2015 means the key, IP restriction or permission does not match the request. |
|