binance

sieblyioMIT活跃维护

币安交易所的 Node.js/JavaScript SDK,封装 REST API 和 WebSocket,带 TypeScript 类型和浏览器支持,方便快速接入行情与交易接口。

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.

v3.6.12发布于 2026-11-09 11:19 UTC933star +1 / 7 天284fork同步于 2026-11-10 11:52 UTC

怎么上手

由 README 提炼,执行前请确认
npm install binance --save
硬件要求按 README 说明

README 未说明

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

币安 REST 与 WebSocket 的 Node.js/TypeScript SDK,带类型和自动重连,省去自己封装接口

币安交易所的 Node.js/JavaScript SDK,封装 REST API 和 WebSocket,带 TypeScript 类型和浏览器支持,方便快速接入行情与交易接口。

核心能力

  • 覆盖现货、杠杆、期权、U 本位与币本位合约 REST 接口
  • WebSocket 心跳保活并自动重连,监听 key 自动续期
  • 支持 HMAC、RSA、Ed25519 三种签名方式
  • 完整 TypeScript 类型声明,请求响应强类型
  • WebSocket API 可像 REST 一样用 async/await 调用

容易踩的坑

私有接口需先在币安申请 API key,测试网与 Demo Trading 需分别申请;期权(Vanilla Options)接口尚未支持;WebSocket API 登录需 Ed25519 密钥,否则每个请求单独签名

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v3.6.12
  1. v3.6.12v3.6.12: Add sub account API key management APIs, add CANCEL_ONLY symbol status filters最新正式版2026-11-09 · 2026-11-09 11:19 UTC

    What's Changed

    Full Changelog: https://github.com/sieblyio/binance/compare/v3.6.11...v3.6.12

  2. v3.6.11v3.6.11: Improve robustness on derived user data key handling during teardown2026-10-26 · 2026-10-26 17:29 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/sieblyio/binance/compare/v3.6.10...v3.6.11

  3. v3.6.10v3.6.10: Binance REST API updates for PM income page parameter removal, Stable Rate loan deprecations, margin listen-key V22026-10-26 · 2026-10-26 13:04 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/sieblyio/binance/compare/v3.6.9...v3.6.10

  4. v3.6.9v3.6.9: Add position risk control filter, delistTime and commission discount docs2026-10-20 · 2026-10-20 12:31 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/sieblyio/binance/compare/v3.6.8...v3.6.9

  5. v3.6.8v3.6.8: Drop page from futures income history endpoint, add PM fund collection params2026-10-16 · 2026-10-16 09:51 UTC
    查看更新说明

    What's Changed

    Summary

    Binance stopped accepting page on USD-M and COIN-M income history on 16 Oct 2026. Paginate with startTime / endTime. This release also adds the other pending Binance field updates that were still missing, and bumps the package to 3.6.8.

    Changelog

    • Remove page from GetIncomeHistoryParams used by USDMClient.getIncomeHistory() (GET /fapi/v1/income) and CoinMClient.getIncomeHistory() (GET /dapi/v1/income). Binance removed page on 16 Oct 2026; JSDoc says to paginate with startTime and endTime.
    • Portfolio Margin fund collection (POST /sapi/v1/portfolio/asset-collection, POST /papi/v1/asset-collection): optional amount and futureType (UM | CM | OPTION); must be sent together.
    • VIP loan repayment history (GET /sapi/v1/loan/vip/repay/history): add repayInterest and repayPrincipal.
    • Spot algo open and historical orders: optional duration (seconds, TWAP only).
    • Margin / spot user-data executionReport: add allocation id a as a number, mapped to allocationId.
    • Version 3.6.8. Regenerated docs/endpointFunctionList.md and llms.txt.

    Full Changelog: https://github.com/sieblyio/binance/compare/v3.6.7...v3.6.8

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Node.js & JavaScript SDK for Binance REST APIs & WebSockets

    Build & Test npm version npm size users count npm downloads last commit CodeFactor Telegram Binance Tutorial

    [!TIP] Upcoming change: As part of the Siebly.io brand, this SDK is now hosted under our Siebly.io GitHub organisation. The migration is seamless and requires no user changes.

    Updated & performant JavaScript & Node.js SDK for the Binance REST APIs and WebSockets.

    Spot user data stream (listen key is created and kept alive for you):

    import { WebsocketClient } from 'binance';
    const ws = new WebsocketClient({ api_key: 'key', api_secret: 'secret' });
    ws.on('formattedMessage', (data) => console.log(data));
    ws.subscribeSpotUserDataStream();
    
    • Professional, robust & performant Binance SDK with leading trading volume in production (livenet).
    • Extensive integration with Binance REST APIs, WebSockets & WebSocket APIs.
    • Complete TypeScript support (with type declarations for all API requests & responses).
    • Supports Binance REST APIs for Binance Spot, Margin, Isolated Margin, Options, USDM & CoinM Futures.
      • Strongly typed requests and responses.
      • Automated end-to-end tests on most API calls, ensuring no breaking changes are released to npm.
    • Actively maintained with a modern, promise-driven interface.
    • Support for all authentication mechanisms available on Binance:
      • HMAC
      • RSA
      • Ed25519 (required for WS API login, else each request is signed).
      • Passing a private key as a secret will automatically detect whether to switch to RSA or Ed25519 authentication.
    • Supports WebSockets for all available product groups on Binance including Spot, Margin, Isolated Margin, Portfolio, Options, USDM & CoinM Futures.
      • Event driven messaging.
      • Smart WebSocket persistence
        • Automatically handle silent WebSocket disconnections through timed heartbeats, including the scheduled 24hr disconnect.
        • Automatically handle listenKey persistence and expiration/refresh.
        • Emit reconnected event when dropped connection is restored.
      • Strongly typed on most WebSocket events, with typeguards available for TypeScript users.
      • Optional:
        • Automatic beautification of WebSocket events (from one-letter keys to descriptive words, and strings with floats to numbers).
        • Automatic beautification of REST responses (parsing numbers in strings to numbers).
    • Supports WebSocket API on all available product groups, including Spot & Futures:
      • Use the WebsocketClient's event-driven sendWSAPIRequest() method, or;
      • Use the WebsocketAPIClient for a REST-like experience. Use the WebSocket API like a REST API! See examples/ws-api-client.ts for a demonstration.
    • Heavy automated end-to-end testing with real API calls.
      • End-to-end testing before any release.
      • Real API calls in e2e tests.
    • Proxy support via axios integration.
    • Active community support & collaboration in telegram: Node.js Algo Traders.
    • QuickStart Guide: Binance JavaScript QuickStart Guide
    • Binance JavaScript Tutorial: Binance JavaScript REST API & WebSocket Tutorial

    Table of Contents

    Installation

    npm install binance --save

    Examples

    Refer to the examples folder for implementation demos.

    Issues & Discussion

    • Issues? Check the issues tab.
    • Discuss & collaborate with other node devs? Join our Node.js Algo Traders engineering community on telegram.
    • Questions about Binance APIs & WebSockets? Ask in the official Binance API group on telegram.
    • Follow our announcement channel for real-time updates on X/Twitter

    Related Projects

    Check out our JavaScript/TypeScript/Node.js SDKs & Projects:

    Documentation

    Most methods accept JS objects. These can be populated using parameters specified by Binance's API documentation.

    Structure

    This project uses typescript. Resources are stored in 3 key structures:

    • src - the whole connector written in typescript
    • lib - the javascript version of the project (compiled from typescript). This should not be edited directly, as it will be overwritten with each release.
    • dist - the packed bundle of the project for use in browser environments.

    Usage

    Create API credentials at Binance

    Demo Trading vs Testnet

    Binance offers two testing environments:

    • Demo Trading: Uses real market data but simulated trading. This is ideal for testing strategies since market conditions match production. Available for Spot, USD-M Futures, and COIN-M Futures.
    • Testnet: Separate environment with simulated market data. Market conditions are very different from real markets and not recommended for strategy testing.

    To use demo trading, simply set demoTrading: true in the client options. See the demo trading examples for more information.

    REST API Clients

    There are several REST API modules as there are some differences in each API group.

    1. MainClient for most APIs, including: spot, margin, isolated margin, mining, BLVT, BSwap, Fiat & sub-account management.
    2. USDMClient for USD-M futures APIs.
    3. CoinMClient for COIN-M futures APIs.
    4. PortfolioClient for Portfolio Margin APIs.

    Vanilla Options is not yet available. Please get in touch if you're looking for this.

    REST Main Client

    The MainClient covers all endpoints under the main "api*.binance.com" subdomains, including but not limited to endpoints in the following product groups:

    • Spot
    • Cross & isolated margin
    • Convert
    • Wallet
    • Futures management (transfers & history)
    • Sub account management
    • Misc transfers
    • Auto & dual invest
    • Staking
    • Mining
    • Loans & VIP loans
    • Simple Earn
    • NFTs
    • C2C
    • Exchange Link
    • Alpha trading

    Refer to the following links for a complete list of available endpoints:

    Start by importing the MainClient class. API credentials are optional, unless you plan on making private API calls. More Node.js & JavaScript examples for Binance's REST APIs & WebSockets can be found in the examples folder on GitHub.

    import { MainClient } from 'binance';
    
    // or, if you prefer `require()`:
    // const { MainClient } = require('binance');
    
    const API_KEY = 'xxx';
    const API_SECRET = 'yyy';
    
    const client = new MainClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      // Connect to testnet environment
      // testnet: true,
    });
    
    client
      .getAccountTradeList({ symbol: 'BTCUSDT' })
      .then((result) => {
        console.log('getAccountTradeList result: ', result);
      })
      .catch((err) => {
        console.error('getAccountTradeList error: ', err);
      });
    
    client
      .getExchangeInfo()
      .then((result) => {
        console.log('getExchangeInfo inverse result: ', result);
      })
      .catch((err) => {
        console.error('getExchangeInfo inverse error: ', err);
      });
    

    See main-client.ts for further information on the available REST API endpoints for spot/margin/etc.

    REST USD-M Futures

    Start by importing the USDM client. API credentials are optional, unless you plan on making private API calls.

    import { USDMClient } from 'binance';
    
    // or, if you prefer `require()`:
    // const { USDMClient } = require('binance');
    
    const API_KEY = 'xxx';
    const API_SECRET = 'yyy';
    
    const client = new USDMClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      // Connect to testnet environment
      // testnet: true,
    });
    
    client
      .getBalance()
      .then((result) => {
        console.log('getBalance result: ', result);
      })
      .catch((err) => {
        console.error('getBalance error: ', err);
      });
    
    client
      .submitNewOrder({
        side: 'SELL',
        symbol: 'BTCUSDT',
        type: 'MARKET',
        quantity: 0.001,
      })
      .then((result) => {
        console.log('submitNewOrder result: ', result);
      })
      .catch((err) => {
        console.error('submitNewOrder error: ', err);
      });
    

    See usdm-client.ts for further information.

    REST COIN-M Futures

    Start by importing the coin-m client. API credentials are optional, though an error is thrown when attempting any private API calls without credentials.

    import { CoinMClient } from 'binance';
    
    // or, if you prefer `require()`:
    // const { CoinMClient } = require('binance');
    
    const API_KEY = 'xxx';
    const API_SECRET = 'yyy';
    
    const client = new CoinMClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      // Connect to testnet environment
      // testnet: true,
    });
    
    client
      .getSymbolOrderBookTicker()
      .then((result) => {
        console.log('getSymbolOrderBookTicker result: ', result);
      })
      .catch((err) => {
        console.error('getSymbolOrderBookTicker error: ', err);
      });
    

    See coinm-client.ts for further information.

    REST Portfolio Margin

    Start by importing the Portfolio client. API credentials are optional, though an error is thrown when attempting any private API calls without credentials.

    import { PortfolioClient } from 'binance';
    
    // or, if you prefer `require()`:
    // const { PortfolioClient } = require('binance');
    
    const API_KEY = 'xxx';
    const API_SECRET = 'yyy';
    
    const client = new PortfolioClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      // Connect to testnet environment
      // testnet: true,
    });
    
    client
      .getBalance()
      .then((result) => {
        console.log('getBalance result: ', result);
      })
      .catch((err) => {
        console.error('getBalance error: ', err);
      });
    
    client
      .submitNewUMOrder({
        side: 'SELL',
        symbol: 'BTCUSDT',
        type: 'MARKET',
        quantity: 0.001,
      })
      .then((result) => {
        console.log('submitNewUMOrder result: ', result);
      })
      .catch((err) => {
        console.error('submitNewUMOrder error: ', err);
      });
    

    See portfolio-client.ts for further information.

    WebSockets

    WebSocket Consumers

    All websockets are accessible via the shared WebsocketClient. As before, API credentials are optional unless the user data stream is required.

    The below example demonstrates connecting as a consumer, to receive WebSocket events from Binance:

    import { WebsocketClient } from 'binance';
    
    // or, if you prefer `require()`:
    // const { WebsocketClient } = require('binance');
    
    const API_KEY = 'xxx';
    const API_SECRET = 'yyy';
    
    /**
     * The WebsocketClient will manage individual connections for you, under the hood.
     * Just make an instance of the WS Client and subscribe to topics. It'll handle the rest.
     */
    const wsClient = new WebsocketClient({
      api_key: key,
      api_secret: secret,
      // Optional: when enabled, the SDK will try to format incoming data into more readable objects.
      // Beautified data is emitted via the "formattedMessage" event
      beautify: true,
      // Disable ping/pong ws heartbeat mechanism (not recommended)
      // disableHeartbeat: true,
      // Connect to testnet environment
      // testnet: true,
    });
    
    // receive raw events
    wsClient.on('message', (data) => {
      console.log('raw message received ', JSON.stringify(data, null, 2));
    });
    
    // notification when a connection is opened
    wsClient.on('open', (data) => {
      console.log('connection opened open:', data.wsKey, data.wsUrl);
    });
    
    // receive formatted events with beautified keys. Any "known" floats stored in strings as parsed as floats.
    wsClient.on('formattedMessage', (data) => {
      console.log('formattedMessage: ', data);
    });
    
    // read response to command sent via WS stream (e.g LIST_SUBSCRIPTIONS)
    wsClient.on('response', (data) => {
      console.log('log response: ', JSON.stringify(data, null, 2));
    });
    
    // receive notification when a ws connection is reconnecting automatically
    wsClient.on('reconnecting', (data) => {
      console.log('ws automatically reconnecting.... ', data?.wsKey);
    });
    
    // receive notification that a reconnection completed successfully (e.g use REST to check for missing data)
    wsClient.on('reconnected', (data) => {
      console.log('ws has reconnected ', data?.wsKey);
    });
    
    // Recommended: receive error events (e.g. first reconnection failed)
    wsClient.on('exception', (data) => {
      console.log('ws saw error ', data?.wsKey);
    });
    
    /**
     * Subscribe to public topics either one at a time or many in an array
     */
    
    // E.g. one at a time, routed to the coinm futures websockets:
    wsClient.subscribe('btcusd@indexPrice', 'coinm');
    wsClient.subscribe('btcusd@miniTicker', 'coinm');
    
    // Or send many topics at once to a stream, e.g. the usdm futures stream:
    wsClient.subscribe(
      ['btcusdt@aggTrade', 'btcusdt@markPrice', '!miniTicker@arr'],
      'usdm',
    );
    
    // spot & margin topics should go to "main"
    // (similar how the MainClient is for REST APIs in that product group)
    wsClient.subscribe(
      [
        // All Market Rolling Window Statistics Streams
        // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-rolling-window-statistics-streams
        '!ticker_1h@arr',
        // Individual Symbol Book Ticker Streams
        // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-book-ticker-streams
        'btcusdt@bookTicker',
        // Average Price
        // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#average-price
        'btcusdt@avgPrice',
        // Partial Book Depth Streams
        // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams
        'btcusdt@depth10@100ms',
        // Diff. Depth Stream
        // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream
        'btcusdt@depth',
      ],
      // Look at the `WS_KEY_URL_MAP` for a list of values here:
      // https://github.com/sieblyio/binance/blob/master/src/util/websockets/websocket-util.ts
      // "main" connects to wss://stream.binance.com:9443/stream
      // https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams
      'main',
    );
    
    /**
     * For the user data stream, these convenient subscribe methods open a dedicated
     * connection with the listen key workflow:
     */
    
    wsClient.subscribeSpotUserDataStream();
    wsClient.subscribeMarginUserDataStream();
    wsClient.subscribeIsolatedMarginUserDataStream('BTCUSDT');
    wsClient.subscribeUsdFuturesUserDataStream();
    wsClient.subscribePortfolioMarginUserDataStream();
    

    See websocket-client.ts for further information. Also see ws-userdata.ts for user data examples.

    Preserving large integers in WebSocket messages

    By default, messages are parsed using JSON.parse, which cannot precisely represent integers larger than Number.MAX_SAFE_INTEGER. If you need to preserve large integers (e.g., order IDs), provide a custom parser via customParseJSONFn.

    Example using RegEx below, although alternatives are possible too if desired. For more exampes check ws-custom-parser.ts in the examples folder:

    import { WebsocketClient } from 'binance';
    
    /**
     * ETHUSDT in futures can have unusually large orderId values, sent as numbers. See this thread for more details:
     * https://github.com/sieblyio/binance/issues/208
     *
     * If this is a problem for you, you can set a custom JSON parsing alternative using the customParseJSONFn hook injected into the WebsocketClient's constructor, as below:
     */
    const ws = new WebsocketClient({
      // Default behaviour, if you don't include this:
      // customParseJSONFn: (rawEvent) => {
      //   return JSON.parse(rawEvent);
      // },
    
      // Or, pre-process the raw event using RegEx, before using the same workflow:
      customParseJSONFn: (rawEvent) => {
        return JSON.parse(
          rawEvent.replace(/"orderId":\s*(\d+)/g, '"orderId":"$1"'),
        );
      },
    
      // Or, use a 3rd party library such as json-bigint:
      // customParseJSONFn: (rawEvent) => {
      //   return JSONbig({ storeAsString: true }).parse(rawEvent);
      // },
    });
    
    ws.on('message', (msg) => {
      console.log(msg);
    });
    
    // If you prefer native BigInt, beware JSON.stringify will throw on BigInt values.
    // Use a custom replacer or JSONbig.stringify if you need to log/serialize:
    // const replacer = (_k: string, v: unknown) => typeof v === 'bigint' ? v.toString() : v;
    // console.log(JSON.stringify(msg, replacer));
    

    WebSocket API

    Some of the product groups available on Binance also support sending requests (commands) over an active WebSocket connection. This is called the WebSocket API.

    Authentication

    HMAC, RSA and Ed25519 keys are supported for the WebSocket API. However, only Ed25519 keys support WebSocket API login. When using HMAC or RSA keys, each WebSocket API command will need to be individually signed.

    This is no issue for most use cases, but if you are latency sensitive, you should consider using Ed25519 keys. This will allow the WebSocket API client to authenticate once after the connection opens, and all commands can then be sent without additional request signatures.

    Event Driven API

    The WebSocket API is available in the WebsocketClient via the sendWSAPIRequest(wsKey, command, commandParameters) method.

    Each call to this method is wrapped in a promise, which you can async await for a response, or handle it in a raw event-driven design.

    Async Await API

    The WebSocket API is also available in a promise-wrapped REST-like format. Either, as above, await any calls to sendWSAPIRequest(...), or directly use the convenient WebsocketAPIClient. This class is very similar to existing REST API classes (such as the MainClient or USDMClient).

    It provides one function per endpoint, feels like a REST API and will automatically route your request via an automatically persisted, authenticated and health-checked WebSocket API connection.

    Below is an example showing how easy it is to use the WebSocket API without any concern for the complexity of managing WebSockets.

    import { WebsocketAPIClient } from 'binance';
    
    // or, if you prefer `require()`:
    // const { WebsocketAPIClient } = require('binance');
    
    /**
     * Note: the WebSocket API is fastest with Ed25519 keys. HMAC & RSA will
     * require each command to be individually signed.
     *
     * Check the rest-private-ed25519.md in this folder for more guidance
     * on preparing this Ed25519 API key.
     */
    
    const publicKey = `-----BEGIN PUBLIC KEY-----
    MCexampleQTxwLU9o=
    -----END PUBLIC KEY-----
    `;
    
    const privateKey = `-----BEGIN PRIVATE KEY-----
    MC4CAQAexamplewqj5CzUuTy1
    -----END PRIVATE KEY-----
    `;
    
    // API Key returned by binance, generated using the publicKey (above) via Binance's website
    const apiKey = 'TQpJexamplerobdG';
    
    // Make an instance of the WS API Client
    const wsClient = new WebsocketAPIClient({
      api_key: apiKey,
      api_secret: privateKey,
      beautify: true,
    
      // Enforce testnet ws connections, regardless of supplied wsKey
      // testnet: true,
    });
    
    // Optional, if you see RECV Window errors, you can use this to manage time issues. However, make sure you sync your system clock first!
    // https://github.com/sieblyio/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow
    // wsClient.setTimeOffsetMs(-5000);
    
    // Optional, see above. Can be used to prepare a connection before sending commands
    // await wsClient.connectWSAPI(WS_KEY_MAP.mainWSAPI);
    
    // Make WebSocket API calls, very similar to a REST API:
    
    wsClient
      .getFuturesAccountBalanceV2({
        timestamp: Date.now(),
        recvWindow: 5000,
      })
      .then((result) => {
        console.log('getFuturesAccountBalanceV2 result: ', result);
      })
      .catch((err) => {
        console.error('getFuturesAccountBalanceV2 error: ', err);
      });
    
    wsClient
      .submitNewFuturesOrder('usdm', {
        side: 'SELL',
        symbol: 'BTCUSDT',
        type: 'MARKET',
        quantity: 0.001,
        timestamp: Date.now(),
        // recvWindow: 5000,
      })
      .then((result) => {
        console.log('getFuturesAccountBalanceV2 result: ', result);
      })
      .catch((err) => {
        console.error('getFuturesAccountBalanceV2 error: ', err);
      });
    

    Market Maker Endpoints

    Binance provides specialized market maker endpoints for qualified high-frequency trading users who have enrolled in at least one of the Futures Liquidity Provider Programs, including the USDⓈ-M Futures Maker Program, COIN-M Futures Maker Program, and USDⓈ-M Futures Taker Program.

    These endpoints provide the same functionality as regular endpoints but with optimized routing for market makers. For more information about eligibility and enrollment, visit: https://www.binance.com/en/support/faq/detail/7df7f3838c3b49e692d175374c3a3283

    Using Market Maker Endpoints

    To use market maker endpoints, simply add the useMMSubdomain: true option when initializing any client (REST API clients, WebSocket clients, or WebSocket API clients):

    Market Maker REST API Clients
    import { USDMClient, CoinMClient } from 'binance';
    
    // USD-M Futures with MM endpoints
    const usdmClient = new USDMClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      useMMSubdomain: true, // Enable market maker endpoints
    });
    
    // COIN-M Futures with MM endpoints
    const coinmClient = new CoinMClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      useMMSubdomain: true, // Enable market maker endpoints
    });
    
    Market Maker WebSocket Clients
    import { WebsocketClient, WebsocketAPIClient } from 'binance';
    
    // WebSocket consumer with MM endpoints
    const wsClient = new WebsocketClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      useMMSubdomain: true, // Enable market maker endpoints
    });
    
    // WebSocket API client with MM endpoints
    const wsApiClient = new WebsocketAPIClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      useMMSubdomain: true, // Enable market maker endpoints
    });
    

    Note: Market maker endpoints are only available for futures products (USD-M and COIN-M). Spot, margin, and other product groups use the regular endpoints regardless of the useMMSubdomain setting. Market maker endpoints are also not available on testnet environments.

    Best practice

    Since market maker endpoints are only available for some of the futures endpoints, you may need to use multiple client instances if your algorithm needs to use both regular and MM endpoints.

    import { USDMClient } from 'binance';
    
    // MM client for USD-M futures
    const futuresMMClient = new USDMClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      useMMEndpoints: true, // Use MM endpoints for futures
    });
    
    // Regular client for USD-M futures
    const futuresRegularClient = new USDMClient({
      api_key: API_KEY,
      api_secret: API_SECRET,
      useMMEndpoints: false, // Use regular endpoints for futures
    });
    

    Customise Logging

    Pass a custom logger which supports the log methods trace, info and error, or override methods from the default logger as desired.

    import { WebsocketClient, DefaultLogger } from 'binance';
    
    // or, if you prefer `require()`:
    // const { WebsocketClient, DefaultLogger } = require('binance');
    
    // Enable all logging on the trace level (disabled by default)
    DefaultLogger.trace = (...params) => {
      console.trace('trace: ', params);
    };
    
    // Pass the updated logger as the 2nd parameter
    const ws = new WebsocketClient(
      {
        api_key: key,
        api_secret: secret,
        beautify: true,
      },
      DefaultLogger
    );
    
    // Or, create a completely custom logger with the 3 available functions
    const customLogger = {
      trace: (...params: LogParams): void => {
        console.trace(new Date(), params);
      },
      info: (...params: LogParams): void => {
        console.info(new Date(), params);
      },
      error: (...params: LogParams): void => {
        console.error(new Date(), params);
      },
    }
    
    // Pass the custom logger as the 2nd parameter
    const ws = new WebsocketClient(
      {
        api_key: key,
        api_secret: secret,
        beautify: true,
      },
      customLogger
    );
    

    Browser/Frontend Usage

    Import

    This is the "modern" way, allowing the package to be directly imported into frontend projects with full typescript support.

    1. Install these dependencies
      npm install crypto-browserify stream-browserify
      
    2. Add this to your tsconfig.json
      {
        "compilerOptions": {
          "paths": {
            "crypto": [
              "./node_modules/crypto-browserify"
            ],
            "stream": [
              "./node_modules/stream-browserify"
            ]
      }
      
    3. Declare this in the global context of your application (ex: in polyfills for angular)
      (window as any).global = window;
      

    Webpack

    This is the "old" way of using this package on webpages. This will build a minified js bundle that can be pulled in using a script tag on a website.

    Build a bundle using webpack:

    • npm install
    • npm build
    • npm pack

    The bundle can be found in dist/. Altough usage should be largely consistent, smaller differences will exist. Documentation is still TODO.

    Use with LLMs & AI

    This SDK includes a bundled llms.txt file in the root of the repository. If you're developing with LLMs, use the included llms.txt with your LLM - it will significantly improve the LLMs understanding of how to correctly use this SDK.

    This file contains AI optimised structure of all the functions in this package, and their parameters for easier use with any learning models or artificial intelligence.


    Contributions & Thanks

    Have my projects helped you? Share the love, there are many ways you can show your thanks:

    Contributions & Pull Requests

    Contributions are encouraged, I will review any incoming pull requests. See the issues tab for todo items.

    Used By

    Repository Users Preview Image

    Star History

    Star History Chart