Show Menu
Cheatography

Binance API in JavaScript (binance npm package) Cheat Sheet (DRAFT) by

Quick reference for the Binance REST and WebSocket APIs from Node.js and TypeScript with the open-source binance package: install, keys, market data, Spot and USD-M futures orders, WebSocket streams and private user data.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Install and set up

The binance package is an open-s­ource, MIT-li­censed SDK with TypeScript types for the Binance REST APIs, WebSocket streams and the WebSocket API. I help maintain it (Siebly). Full tutorial: https:­//s­ieb­ly.i­o/­sdk­/bi­nan­ce/­jav­asc­rip­t/t­utorial

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 MainCl­ient() is enough.
- Keep keys in enviro­nment variables, never in code.
- Give a bot only the permis­sions it uses (Reading, Spot and Margin Trading, Futures). Leave Withdr­awals off.
- Add an IP restri­ction for production servers.
- HMAC, RSA and Ed25519 keys all work with the SDK.
- Practise first: pass testnet: true (Spot testnet) or demoTr­ading: 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 exchan­geInfo, 2 Dec 2026); USD-M has 2,400.
- Spot account events now come from the WebSocket API subscr­iption (Block 7). The old Spot listenKey request (POST /api/v­3/u­ser­Dat­aSt­ream) 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 resubs­cribes; refresh private state on 'recon­nec­ted'.
- Error -1021 means the request timestamp is outside recvWi­ndow: check the machine clock.
- Error -2015 means the key, IP restri­ction or permission does not match the request.