MCPcopy Index your code
hub / github.com/ccxt/node-binance-api

github.com/ccxt/node-binance-api @1.0.32

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.0.32 ↗ · + Follow
628 symbols 1,619 edges 29 files 291 documented · 46%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Node Binance API

telegram Yearly Downloads CCXT on X

This project is designed to help you make your own projects that interact with the Binance API. You can stream candlestick chart data, market depth, or use other advanced features such as setting stop losses and iceberg orders. This project seeks to have complete API coverage including WebSockets.

Futures API & StreamsSpot Trading API & StreamsMargin APILending API

ExamplesTroubleshootingChangelogSupport

Installation

npm install node-binance-api

NPM

Community Telegram Chat

https://t.me/nodebinanceapi

This project is powered by

Actively maintained, typed, and safe SDK for the Binance REST APIs and Websockets. Supports ESM and CJS out of the box.

Features

  • Spot, Margin, Futures and Delivery API (including the new algoOrder service)
  • Demo trading support
  • Testnet support (deprecated)
  • Proxy support (REST and WS, including the new WS-API)
  • Customizable HTTP headers
  • Customizable request parameters
  • RSA/ECDSA support
  • Portfolio Margin API *soon*
  • Websocket handling with automatic reconnection
  • RecvWindow and automatic timestamps generation
  • Ability to call any endpoint, even if not supported directly by the library
  • Overridable hostnames (.us, .jp, etc)
  • Verbose mode to debug http requests/responses

Upgrading to v1.0.0+

We highly advise you to update from 0.0.X but minor adjustments might be needed.

The library was fully refactored to use a modern and typed JavaScript/Typescript version, using the built-in await/async syntax and unifying some methods' signatures. Some important changes include the removal of callbacks as parameters of REST methods, adaptation of signatures to directly receive some important request values (symbol, orderId, ...), among others.

Getting started (ESM)

import Binance from 'node-binance-api';
async function run() {
    const exchange = new Binance();
    const res = await exchange.futuresTime();
    console.log( res );
}

Getting started (CJS)

const Binance = require('node-binance-api');
const binance = new Binance({
  APIKEY: '<key>',
  APISECRET: '<secret>',
  test: true, // if you want to use the sandbox/testnet
});

Binance Futures API

Futures Prices

console.info( await binance.futuresPrices() );

Futures Account Balances & Positions

console.info( await binance.futuresAccount() );

Futures Balances

console.info( await binance.futuresBalance() );

Futures Limit Buy

console.info( await binance.futuresBuy( 'LIMIT', 'BTCUSDT', 0.1, 8222 ) );

Futures Limit Sell

console.info( await binance.futuresSell( 'LIMIT', 'BTCUSDT', 0.5, 11111 ) );

Futures Market Buy

console.info( await binance.futuresMarketBuy( 'BNBUSDT', 5 ) );

Futures Market Sell

console.info( await binance.futuresMarketSell( 'TRXUSDT', 1 ) );

Futures Place Multiple Orders

let orders = [
  {
  symbol:"BTCUSDT",
  side: "BUY",
  type: "MARKET",
  quantity: "0.01",
  },
  {
  symbol:"BNBUSDT",
  side: "SELL",
  type: "MARKET",
  quantity: "0.5",
  }
]
console.info( await binance.futuresMultipleOrders(orders) );

Futures Market Orders: Get the fill price using newOrderRespType

console.info( await binance.futuresMarketBuy( 'BNBUSDT', amount, { newOrderRespType: 'RESULT' } ) );

Futures reduceOnly Order Example

if ( side == 'LONG' ) order = await binance.futuresMarketSell( obj.symbol, amount, {reduceOnly: true} )
else order = await binance.futuresMarketBuy( obj.symbol, amount, {reduceOnly: true} )

Get Futures Positions

console.info( await binance.futuresPositionRisk() );

View Example

let position_data = await binance.futuresPositionRisk(), markets = Object.keys( position_data );
for ( let market of markets ) {
  let obj = position_data[market], size = Number( obj.positionAmt );
  if ( size == 0 ) continue;
  console.info( `${leverage}x\t${market}\t${obj.unRealizedProfit}` );
  //console.info( obj ); //positionAmt entryPrice markPrice unRealizedProfit liquidationPrice leverage marginType isolatedMargin isAutoAddMargin maxNotionalValue
}

Adjust Leverage (1-125x)

console.info( await binance.futuresLeverage( 'ETHUSDT', 50 ) );

Adjust Margin Type (ISOLATED, CROSSED)

console.info( await binance.futuresMarginType( 'BTCUSDT', 'ISOLATED' ) );

Adjust Position Margin

// Type: 1: Add postion margin,2: Reduce postion margin
console.info( await binance.futuresPositionMargin( "TRXUSDT", amount, type ) );
console.info( await binance.futuresTime() );
console.info( await binance.futuresExchangeInfo() );
console.info( await binance.futuresCandles( "TRXUSDT", "1m" ) );
console.info( await binance.futuresDepth( "ADAUSDT" ) );
console.info( await binance.futuresQuote() );
console.info( await binance.futuresQuote( "BCHUSDT" ) );
console.info( await binance.futuresDaily() );
console.info( await binance.futuresOpenInterest( "BTCUSDT" ) );
console.info( await binance.futuresMarkPrice() );
console.info( await binance.futuresMarkPrice( "ETHUSDT" ) );
console.info( await binance.futuresTrades( "LTCUSDT" ) );
console.info( await binance.futuresAggTrades( "XTZUSDT" ) );
console.info( await binance.futuresLiquidationOrders() );
console.info( await binance.futuresFundingRate() );
console.info( await binance.futuresHistoricalTrades( "XMRUSDT" ) );
console.info( await binance.futuresLeverageBracket( "LINKUSDT" ) );
console.info( await binance.futuresIncome() );
console.info( await binance.futuresCancelAll( "BTCUSDT" ) );
console.info( await binance.futuresCancel( "BTCUSDT", "1025137386" ) );
console.info( await binance.futuresCountdownCancelAll( "BTCUSDT", 45000 ) );
console.info( await binance.futuresOrderStatus( "BTCUSDT", "1025137386") );
console.info( await binance.futuresOpenOrders() );
console.info( await binance.futuresOpenOrders( "BTCUSDT" ) );
console.info( await binance.futuresAllOrders() );
console.info( await binance.futuresAllOrders( "BTCUSDT" ) );
console.info( await binance.futuresUserTrades( "BTCUSDT" ) );
console.info( await binance.futuresGetDataStream() );
console.info( await binance.futuresPositionMarginHistory( "TRXUSDT" ) );
console.info( await binance.futuresPublicRequest( 'v1/time' ) );
console.info( await binance.spotPublicRequest( 'v1/time')); // call any method by providing the path
console.info( await binance.privateFuturesRequest('v3/account')); // custom futures private call
// Batch orders, remaining WebSocket streams, and better documentation will be come later

Proxy support

In some specific cases using a proxy is required, for example: - Exchange is not available in your location - You need to make a large amount of requests without getting blocked - ...

This package supports the following proxy types, httpsProxy, proxyUrl and socksProxy

httpsProxy

To set a real http(s) proxy for your scripts, you need to have an access to a remote http or https proxy, so calls will be made directly to the target exchange, tunneled through your proxy server:

client.httpsProxy = 'http://1.2.3.4:8080/';

proxyUrl

This property prepends an url to API requests. It might be useful for simple redirection or bypassing CORS browser restriction.

client.proxyUrl = 'YOUR_PROXY_URL';

socksProxy

Tou can also use socks proxy with the following format:

client.socksProxy = 'socks5://1.2.3.4:8080/';

Futures Historical Bulk Data Download API

Get Download ID
console.info( await binance.futuresHistDataId(
  "BTCUSDT", {
    startTime: new Date().getTime() - 24 * 60 * 60 * 1000,
    endTime: new Date().getTime(),
    dataType: 'T_TRADE'
  } )
)
Get Download Link
console.info( await binance.futuresDownloadLink(7343) )

Futures WebSocket Streams

Futures miniTicker stream for all symbols

binance.futuresMiniTickerStream( miniTicker => {
    console.info( miniTicker );
} );

Futures miniTicker stream for a symbol

binance.futuresMiniTickerStream( 'BTCUSDT', console.log );

Futures bookTicker stream for all symbols

binance.futuresBookTickerStream( console.log );

Futures bookTicker stream for a symbol

binance.futuresBookTickerStream( 'BTCUSDT', console.log );

Futures prevDay ticker stream for all symbols

binance.futuresTickerStream( console.log );

Futures prevDay ticker stream for a symbol

binance.futuresTickerStream( 'BTCUSDT', console.log );

Futures mark price stream for all symbols

binance.futuresMarkPriceStream( console.log );

Futures mark price stream for a symbol

binance.futuresMarkPriceStream( 'BTCUSDT', console.log );

Futures aggregate trade stream for a symbol

binance.futuresAggTradeStream( 'BTCUSDT', console.log );

Futures complete chart cache

binance.futuresChart( 'BTCUSDT', '1m', console.log );

Futures Liquidation Stream for all symbols

binance.futuresLiquidationStream( console.log );

Futures Liquidation Stream for a symbol

binance.futuresLiquidationStream( 'BTCUSDT', console.log );

Connect to a custom endpoint. Easier shortcut functions will come later

binance.futuresSubscribe( 'btcusdt@kline_4h', console.log );

Terminate an existing socket

binance.futuresTerminate( 'btcusdt@kline_4h' );

Extension points exported contracts — how you extend this code

Dictionary (Interface)
(no doc)
src/node-binance-api.ts
Response (Interface)
(no doc)
src/types.ts
IWebsockets (Interface)
(no doc)
deprecated/node-binance-api.d.ts
Candle (Interface)
(no doc)
src/types.ts
IConstructorArgs (Interface)
(no doc)
deprecated/node-binance-api.d.ts
OrderFill (Interface)
(no doc)
src/types.ts
Order (Interface)
(no doc)
src/types.ts
FuturesOrder (Interface)
(no doc)
src/types.ts

Core symbols most depended-on inside this repo

log
called by 232
src/node-binance-api.ts
promiseRequest
called by 90
deprecated/node-binance-api.js
signedRequest
called by 66
deprecated/node-binance-api.js
getSpotUrl
called by 49
deprecated/node-binance-api.js
debug
called by 42
tests/live-tests.cjs
privateFuturesRequest
called by 41
src/node-binance-api.ts
classifyFuturesStream
called by 32
src/node-binance-api.ts
friendlyData
called by 32
deprecated/node-binance-api.js

Shape

Method 447
Function 138
Interface 39
Class 4

Languages

TypeScript100%

Modules by API surface

src/node-binance-api.ts392 symbols
deprecated/node-binance-api.js116 symbols
src/types.ts75 symbols
deprecated/node-binance-api.d.ts23 symbols
tests/binance-ws-api-userdata.test.ts3 symbols
tests/static-tests.mjs2 symbols
tests/margin-ws-api-live.test.ts2 symbols
tests/live-tests.cjs2 symbols
examples/binanceStreams.js2 symbols
tests/ws-endpoints-migration.test.ts1 symbols
tests/package-test/test-esm.mjs1 symbols
tests/package-test/test-cjs.cjs1 symbols

For agents

$ claude mcp add node-binance-api \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact