• Stars
    star
    543
  • Rank 81,437 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple connector to Binance Public API

Binance connector in Nodejs

npm version Node version Standard-Js License: MIT

This is a lightweight library that works as a connector to Binance public API. It’s designed to be simple, clean, and easy to use with minimal dependencies.

  • Supported APIs:
    • /api/*
    • /sapi/*
    • Spot Websocket Market Stream
    • Spot User Data Stream
    • Spot Websocket API
  • Inclusion of test cases and examples
  • Customizable base URL
  • Support request timeout and HTTP proxy (since v2)
  • Response metadata can be displayed
  • Customizable Logger

Installation

npm install @binance/connector

Documentation

https://binance.github.io/binance-connector-node/

RESTful APIs

const { Spot } = require('@binance/connector')

const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret)

// Get account information
client.account().then(response => client.logger.log(response.data))

// Place a new order
client.newOrder('BNBUSDT', 'BUY', 'LIMIT', {
  price: '350',
  quantity: 1,
  timeInForce: 'GTC'
}).then(response => client.logger.log(response.data))
  .catch(error => client.logger.error(error))

Please find examples folder to check for more endpoints.

Key Pair Based Authentication

const { Spot, PrivateKeyAlgo } = require('@binance/connector')

const apiKey = ''
const apiSecret = '' // has no effect when RSA private key is provided

// load private key
const privateKey = fs.readFileSync('/Users/john/ssl/private_key_encrypted.pem')
const privateKeyPassphrase = 'password'
const privateKeyAlgo = PrivateKeyAlgo.RSA // for RSA key
const privateKeyAlgo = PrivateKeyAlgo.ED25519 // for Ed25519 key

const client = new Spot(apiKey, apiSecret, {
  privateKey,
  privateKeyPassphrase // only used for encrypted key
  privateKeyAlgo
})

// Get account information
client.account().then(response => client.logger.log(response.data))

Testnet

While /sapi/* endpoints don't have testnet environment yet, /api/* endpoints can be tested in Spot Testnet. You can use it by changing the base URL:

// provide the testnet base url
const client = new Spot(apiKey, apiSecret, { baseURL: 'https://testnet.binance.vision'})

Base URL

If base_url is not provided, it defaults to api.binance.com.

It's recommended to pass in the base_url parameter, even in production as Binance provides alternative URLs in case of performance issues:

  • https://api1.binance.com
  • https://api2.binance.com
  • https://api3.binance.com

Optional Parameters

Optional parameters are encapsulated to a single object as the last function parameter.

const { Spot } = require('@binance/connector')

const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret)

client.account({ recvWindow: 2000 }).then(response => client.logger.log(response.data))

Timeout

It's easy to set timeout in milliseconds in request. If the request take longer than timeout, the request will be aborted. If it's not set, there will be no timeout.

const { Spot } = require('@binance/connector')

const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret, { timeout: 1000 })

client.account()
  .then(response => client.logger.log(response.data))
  .catch(error => client.logger.error(error.message))

Proxy

The axios package is used as the http client in this library. A proxy settings is passed into axios directly, the details can be found at here:

const { Spot } = require('@binance/connector')

const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret,
  {
    proxy: {
      protocol: 'https',
      host: '127.0.0.1',
      port: 9000,
      auth: {
        username: 'proxy_user',
        password: 'password'
      }
    }
  }
)

You may have a HTTP proxy, that can bring the problem that you need to make a HTTPS connection through the HTTP proxy. You can do that by build a HTTPS-over-HTTP tunnel by npm package tunnel, and then pass the turnnel agent to httpsAgent in axios.

const tunnel = require('tunnel')

const agent = tunnel.httpsOverHttp({
  proxy: {
    host: "127.0.0.1",
    port: 3128
  }
})

const client = new Spot(null, null,
  {
    baseURL: "https://api.binance.com",
    httpsAgent: agent
  }
)

client.time()
  .then(response => client.logger.log(response.data))
  .catch(error => client.logger.error(error))

This comment provides more details.

Response Metadata

The Binance API server provides weight usages in the headers of each response. This information can be fetched from headers property. x-mbx-used-weight and x-mbx-used-weight-1m show the total weight consumed within 1 minute.

// client initialization is skipped

client.exchangeInfo().then(response => client.logger.log(response.headers['x-mbx-used-weight-1m']))

Custom Logger Integration

const Spot = require('@binance/connector')
const fs = require('fs')
const { Console } = require('console')

// make sure the logs/ folder is created beforehand
const output = fs.createWriteStream('./logs/stdout.log')
const errorOutput = fs.createWriteStream('./logs/stderr.log')

const logger = new Console({ stdout: output, stderr: errorOutput })
const client = new Spot('', '', {logger: logger})

client.exchangeInfo().then(response => client.logger.log(response.data))
// check the output file

The default logger defined in the package is Node.js Console class. Its output is sent to process.stdout and process.stderr, same as the global console.

Error

There are 2 types of error that may be returned from the API server and the user has to handle it properly:

  • Client error

    • This is thrown when server returns 4XX, it's an issue from client side.
    • The following properties may be helpful to resolve the issue:
      • Response header - Please refer to Response Metadata section for more details.
      • HTTP status code
      • Error code - Server's error code, e.g. -1102
      • Error message - Server's error message, e.g. Unknown order sent.
      • Request config - Configuration send to the server, which can include URL, request method and headers.
    // client initialization is skipped
    client.exchangeInfo({ symbol: 'invalidSymbol' })
      .then(response => client.logger.log(response.data))
      .catch(err => {
        client.logger.error(err.response.headers) // full response header
        client.logger.error(err.response.status) // HTTP status code 400
        client.logger.error(err.response.data) // includes both error code and message
        client.logger.error(err.response.config) // includes request's config
      })
    
    
  • Server error

    • This is thrown when server returns 5XX, it's an issue from server side.

Websocket

Websocket Stream

const { WebsocketStream } = require('@binance/connector')
const logger = new Console({ stdout: process.stdout, stderr: process.stderr })

// define callbacks for different events
const callbacks = {
  open: () => logger.debug('Connected with Websocket server'),
  close: () => logger.debug('Disconnected with Websocket server'),
  message: data => logger.info(data)
}

const websocketStreamClient = new WebsocketStream({ logger, callbacks })
// subscribe ticker stream
websocketStreamClient.ticker('bnbusdt')
// close websocket stream
setTimeout(() => websocketStreamClient.disconnect(), 6000)

Unsubscribe Websocket Stream

// unsubscribe websocket stream
websocketStreamClient.unsubscribe('bnbusdt@kline_1m')

WebSocket API

const { WebsocketAPI } = require('@binance/connector')
const logger = new Console({ stdout: process.stdout, stderr: process.stderr })

// callbacks for different events
const callbacks = {
  open: (client) => {
    logger.debug('Connected with Websocket server')
    // send message to get orderbook info after connection open
    client.orderbook('BTCUSDT')
    client.orderbook('BNBUSDT', { limit: 10 })
  },
  close: () => logger.debug('Disconnected with Websocket server'),
  message: data => logger.info(data)
}

const websocketAPIClient = new WebsocketAPI(null, null, { logger, callbacks })

// disconnect the connection
setTimeout(() => websocketAPIClient.disconnect(), 20000)

More websocket examples are available in the examples folder

Auto Reconnect

If there is a close event not initiated by the user, the reconnection mechanism will be triggered in 5 secs.

Ping Server

It is possible to ping server from client, and expect to receive a PONG message.

websocketStreamClient.pingServer()

Custom Logger Integration

The default logger defined in the package is Node.js Console class. Its output is sent to process.stdout and process.stderr, same as the global console.

Note that when the connection is initialized, the console outputs a list of callbacks in the form of listen to event: <event_name>.

Test

npm install

npm run test

Limitation

Futures and Vanilla Options APIs are not supported:

  • /fapi/*
  • /dapi/*
  • /vapi/*
  • Associated Websocket Market and User Data Streams

License

MIT

More Repositories

1

binance-spot-api-docs

Official Documentation for the Binance Spot APIs and Streams
3,842
star
2

binance-connector-python

Simple connector to Binance Public API
Python
1,848
star
3

binance-public-data

Details on how to get Binance public data
Python
1,430
star
4

binance-api-postman

Postman collection for Binance Public API, including spot, margin, futures, etc.
1,307
star
5

binance-futures-connector-python

Python
762
star
6

binance-connector-java

Java
375
star
7

binance-signature-examples

Examples of generating HMAC and RSA signature for Binance API
Python
238
star
8

binance-connector-dotnet

Lightweight connector for integration with Binance API
C#
204
star
9

binance-websocket-examples

Example code in Nodejs that demonstrate how to subscribe to Binance Websocket server.
JavaScript
151
star
10

binance-connector-go

Go
147
star
11

binance-api-swagger

Swagger for the Binance Public API
HTML
124
star
12

binance-futures-connector-java

Java
116
star
13

zkmerkle-proof-of-solvency

This is proof of solvency tool for Centralized exchanges built by Binance. Please raise bugs and security issues to https://bugcrowd.com/binance
Go
113
star
14

binance-spot-connector-rust

Rust
111
star
15

binance-toolbox-python

Some useful scripts that help users to validate
Python
95
star
16

asymmetric-key-generator

This simple tool can be used to generate an RSA PKCS#8 or Ed25519 key pairs.
JavaScript
75
star
17

binance-connector-php

This is a thin library that working as a connector to the Binance public API.
PHP
65
star
18

desktop

Binance desktop application release channel.
55
star
19

ai-trading-prototype

Free open source crypto AI trading bot prototype.
Python
50
star
20

binance-connector-typescript

TypeScript
47
star
21

binance-connector-ruby

a simple connector to Binance Public API
Ruby
34
star
22

websocket-demo

a live demo site for subscribing to websocket server
JavaScript
22
star
23

ai-trading-prototype-backtester

Headline Sentiment Analysis Backtester. Backtests trading strategy from ai-trading-prototype trading bot.
Python
21
star
24

binance-cli

JavaScript
20
star
25

ai-trading-prototype-headlines

News Headlines Fetcher. Outputs headlines intended for use with the ai-trading-prototype sentiment-based trading bot.
Python
17
star
26

binance-pay-signature-examples

Python
14
star
27

binance-pay-connector-python

A lightweight library that works as a connector to Binance pay public API
Python
14
star
28

binance-sbe-rust-sample-app

Rust
12
star
29

binance-pay-postman-collection

Postman collection for Binance Pay API
11
star
30

binance-toolbox-java

Java
10
star
31

binance-futures-connector-node

JavaScript
9
star
32

binance-mp-demo

JavaScript
9
star
33

binance-toolbox-go

Go
8
star
34

binance-toolbox-php

PHP
6
star
35

binance-sbe-java-sample-app

Sample app that decodes Binance "exchangeInfo" endpoint's SBE response to YAML.
Java
5
star
36

binance-sbe-cpp-sample-app

C++
4
star
37

binance-toolbox-ruby

Ruby
2
star
38

binance-toolbox-nodejs

JavaScript
1
star
39

binance-toolbox-typescript

TypeScript
1
star
40

binance-futures-java-toolbox

Java
1
star