Installations
npm install @celebimustafa/binance-futures-connector
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=12.22.3
Node Version
16.13.0
NPM Version
8.1.0
Score
75.9
Supply Chain
97.9
Quality
74.5
Maintenance
50
Vulnerability
100
License
Releases
Unable to fetch releases
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Download Statistics
Total Downloads
232
Last Day
1
Last Week
2
Last Month
5
Last Year
50
Bundle Size
61.51 kB
Minified
14.01 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.0.1-beta-1
Package Id
@celebimustafa/binance-futures-connector@0.0.1-beta-1
Unpacked Size
215.32 kB
Size
28.99 kB
File Count
40
NPM Version
8.1.0
Node Version
16.13.0
Oops! Something went wrong.
Binance connector in Nodejs
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
- 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
1npm install @binance/connector
Documentation
https://binance.github.io/binance-connector-node/
RESTful APIs
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.
RSA Key based Authentication
1const { Spot } = 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' 9 10const client = new Spot(apiKey, apiSecret, { 11 privateKey, 12 privateKeyPassphrase // only used for encrypted key 13}) 14 15// Get account information 16client.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:
1// provide the testnet base url 2const 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.
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)) 8
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.
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))
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:
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.
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
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.
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.
- Response header - Please refer to
// 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 })
- This is thrown when server returns
-
Server error
- This is thrown when server returns
5XX
, it's an issue from server side.
- This is thrown when server returns
Websocket
1const { Spot } = require('@binance/connector') 2 3const client = new Spot('', '', { 4 wsURL: 'wss://testnet.binance.vision' // If optional base URL is not provided, wsURL defaults to wss://stream.binance.com:9443 5}) 6 7const callbacks = { 8 open: () => client.logger.log('open'), 9 close: () => client.logger.log('closed'), 10 message: data => client.logger.log(data) 11} 12const aggTrade = client.aggTradeWS('bnbusdt', callbacks) 13 14// unsubscribe the stream above 15setTimeout(() => client.unsubscribe(aggTrade), 3000) 16 17// support combined stream 18const combinedStreams = client.combinedStreams(['btcusdt@miniTicker', 'ethusdt@ticker'], callbacks)
More websocket examples are available in the examples
folder
Unsubscribe a Stream
Unsubscription is achieved by closing the connection. If this method is called without any connection established, the console will output a message No connection to close.
1// client initialization is skipped 2const wsRef = client.aggTradeWS('bnbusdt', callbacks) 3 4// The connection (bnbusdt@aggTrade) is closed after 3 secs. 5setTimeout(() => client.unsubscribe(wsRef), 3000) 6
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.
1 2const { Console } = require('console') 3const { Spot } = require('@binance/connector') 4 5const logger = new Console({ stdout: process.stdout, stderr: process.stderr }); 6const client = new Spot('', '', { logger }) 7 8const callbacks = { 9 open: () => logger.info('open'), 10 close: () => logger.info('closed'), 11 message: data => logger.info(data) 12} 13 14const wsRef = client.userData('the_listen_key', callbacks) 15 16setInterval(() => { 17 client.pingServer(wsRef) 18}, 1000 * 10) 19
Custom Logger Integration
1const { Console } = require('console') 2const fs = require('fs') 3const Spot = require('@binance/connector') 4 5const output = fs.createWriteStream('./logs/stdout.log') 6const errorOutput = fs.createWriteStream('./logs/stderr.log') 7 8// make sure the logs/ folder is created beforehand 9const logger = new Console({ stdout: output, stderr: errorOutput }) 10const client = new Spot('', '', {logger}) 11 12const callbacks = { 13 open: () => client.logger.log('open'), 14 close: () => client.logger.log('closed'), 15 message: data => client.logger.log(data) 16} 17 18const wsRef = client.aggTradeWS('bnbusdt', callbacks) 19setTimeout(() => client.unsubscribe(wsRef), 5000) 20// check the output file 21
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
1npm install 2 3npm run test 4
Limitation
Futures and Vanilla Options APIs are not supported:
/fapi/*
/dapi/*
/vapi/*
- Associated Websocket Market and User Data Streams
License
MIT

No vulnerabilities found.

No security vulnerabilities found.