Gathering detailed insights and metrics for @binance/connector
Gathering detailed insights and metrics for @binance/connector
Gathering detailed insights and metrics for @binance/connector
Gathering detailed insights and metrics for @binance/connector
@binance-chain/bsc-connector
A simple, maximally extensible, dependency minimized framework for building modern Bsc dApps
binance-futures-connector
binance-futures-spot 币安-合约+现货-sdk,持续更新,欢迎PR一起完善。微信:wkc19891
@gm-network/binance-connector
gm-network sdk
@binance/connector-typescript
This is a lightweight library that works as a connector to the Binance public API.
npm install @binance/connector
Typescript
Module System
Min. Node Version
Node Version
NPM Version
79.2
Supply Chain
99.6
Quality
78.7
Maintenance
100
Vulnerability
100
License
TypeScript (99.78%)
JavaScript (0.22%)
Total Downloads
564,355
Last Day
202
Last Week
2,530
Last Month
10,800
Last Year
161,827
MIT License
658 Stars
320 Commits
188 Forks
9 Watchers
2 Branches
17 Contributors
Updated on Jul 04, 2025
Minified
Minified + Gzipped
Latest Version
3.6.1
Package Id
@binance/connector@3.6.1
Unpacked Size
207.55 kB
Size
30.22 kB
File Count
42
NPM Version
8.19.4
Node Version
16.20.2
Published on
Jan 08, 2025
Cumulative downloads
Total Downloads
Last Day
-41.6%
202
Compared to previous day
Last Week
-20.1%
2,530
Compared to previous week
Last Month
-1.9%
10,800
Compared to previous month
Last Year
-25.9%
161,827
Compared to previous year
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.
/api/*
/sapi/*
1npm install @binance/connector
https://binance.github.io/binance-connector-node/
1const { Spot } = require('@binance/connector') 2 3const apiKey = '' 4const apiSecret = '' 5const client = new Spot(apiKey, apiSecret) 6 7// Get account information 8client.account().then(response => client.logger.log(response.data)) 9 10// Place a new order 11client.newOrder('BNBUSDT', 'BUY', 'LIMIT', { 12 price: '350', 13 quantity: 1, 14 timeInForce: 'GTC' 15}).then(response => client.logger.log(response.data)) 16 .catch(error => client.logger.error(error))
Please find examples
folder to check for more endpoints.
1const { Spot, PrivateKeyAlgo } = require('@binance/connector') 2 3const apiKey = '' 4const apiSecret = '' // has no effect when RSA private key is provided 5 6// load private key 7const privateKey = fs.readFileSync('/Users/john/ssl/private_key_encrypted.pem') 8const privateKeyPassphrase = 'password' 9const privateKeyAlgo = PrivateKeyAlgo.RSA // for RSA key 10const privateKeyAlgo = PrivateKeyAlgo.ED25519 // for Ed25519 key 11 12const client = new Spot(apiKey, apiSecret, { 13 privateKey, 14 privateKeyPassphrase, // only used for encrypted key 15 privateKeyAlgo 16}) 17 18// Get account information 19client.account().then(response => client.logger.log(response.data))
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:
1// provide the testnet base url 2const client = new Spot(apiKey, apiSecret, { baseURL: 'https://testnet.binance.vision'})
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 are encapsulated to a single object as the last function parameter.
1const { Spot } = require('@binance/connector') 2 3const apiKey = '' 4const apiSecret = '' 5const client = new Spot(apiKey, apiSecret) 6 7client.account({ recvWindow: 2000 }).then(response => client.logger.log(response.data))
The API supports different time units for timestamp values. By default, timestamp values are provided in milliseconds. You can specify the time unit in the request parameters:
1const { Spot, TimeUnit } = require('@binance/connector') 2 3const apiKey = '' 4const apiSecret = '' 5const client = new Spot(apiKey, apiSecret) 6 7// Using milliseconds (default) 8client.exchangeInfo({ timeUnit: TimeUnit.MILLISECOND }).then(response => client.logger.log(response.data)) 9 10// Using microseconds 11client.exchangeInfo({ timeUnit: TimeUnit.MICROSECOND }).then(response => client.logger.log(response.data))
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.
1const { Spot } = require('@binance/connector') 2 3const apiKey = '' 4const apiSecret = '' 5const client = new Spot(apiKey, apiSecret, { timeout: 1000 }) 6 7client.account() 8 .then(response => client.logger.log(response.data)) 9 .catch(error => client.logger.error(error.message))
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:
1const { Spot } = require('@binance/connector') 2 3const apiKey = '' 4const apiSecret = '' 5const client = new Spot(apiKey, apiSecret, 6 { 7 proxy: { 8 protocol: 'https', 9 host: '127.0.0.1', 10 port: 9000, 11 auth: { 12 username: 'proxy_user', 13 password: 'password' 14 } 15 } 16 } 17)
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
.
1const tunnel = require('tunnel') 2 3const agent = tunnel.httpsOverHttp({ 4 proxy: { 5 host: "127.0.0.1", 6 port: 3128 7 } 8}) 9 10const client = new Spot(null, null, 11 { 12 baseURL: "https://api.binance.com", 13 httpsAgent: agent 14 } 15) 16 17client.time() 18 .then(response => client.logger.log(response.data)) 19 .catch(error => client.logger.error(error)) 20
This comment provides more details.
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']))
1const Spot = require('@binance/connector') 2const fs = require('fs') 3const { Console } = require('console') 4 5// make sure the logs/ folder is created beforehand 6const output = fs.createWriteStream('./logs/stdout.log') 7const errorOutput = fs.createWriteStream('./logs/stderr.log') 8 9const logger = new Console({ stdout: output, stderr: errorOutput }) 10const client = new Spot('', '', {logger: logger}) 11 12client.exchangeInfo().then(response => client.logger.log(response.data)) 13// check the output file 14
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.
There are 2 types of error that may be returned from the API server and the user has to handle it properly:
Client error
4XX
, it's an issue from client side.Response Metadata
section for more details.-1102
Unknown order sent.
// 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
5XX
, it's an issue from server side.1const { WebsocketStream } = require('@binance/connector') 2const logger = new Console({ stdout: process.stdout, stderr: process.stderr }) 3 4// define callbacks for different events 5const callbacks = { 6 open: () => logger.debug('Connected with Websocket server'), 7 close: () => logger.debug('Disconnected with Websocket server'), 8 message: data => logger.info(data) 9} 10 11// initialize websocket stream with microseconds as the preferred time unit 12const websocketStreamClient = new WebsocketStream({ logger, callbacks, timeUnit: TimeUnit.MICROSECOND }) 13// subscribe ticker stream 14websocketStreamClient.ticker('bnbusdt') 15// close websocket stream 16setTimeout(() => websocketStreamClient.disconnect(), 6000)
1// unsubscribe websocket stream 2websocketStreamClient.unsubscribe('bnbusdt@kline_1m')
1const { WebsocketAPI, TimeUnit } = require('@binance/connector') 2const logger = new Console({ stdout: process.stdout, stderr: process.stderr }) 3 4// callbacks for different events 5const callbacks = { 6 open: (client) => { 7 logger.debug('Connected with Websocket server') 8 // send message to get orderbook info after connection open 9 client.orderbook('BTCUSDT') 10 client.orderbook('BNBUSDT', { limit: 10 }) 11 }, 12 close: () => logger.debug('Disconnected with Websocket server'), 13 message: data => logger.info(data) 14} 15 16// initialize WebsocketAPI client with microseconds as the preferred time unit 17const websocketAPIClient = new WebsocketAPI(null, null, { logger, callbacks, timeUnit: TimeUnit.MICROSECOND }) 18 19// disconnect the connection 20setTimeout(() => websocketAPIClient.disconnect(), 20000) 21
More websocket examples are available in the examples
folder
If there is a close event not initiated by the user, the reconnection mechanism will be triggered in 5 secs.
It is possible to ping server from client, and expect to receive a PONG message.
1websocketStreamClient.pingServer()
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>
.
1npm install 2 3npm run test 4
Futures and Vanilla Options APIs are not supported:
/fapi/*
/dapi/*
/vapi/*
MIT
No vulnerabilities found.
No security vulnerabilities found.