Gathering detailed insights and metrics for @celebimustafa/binance-futures-connector
Gathering detailed insights and metrics for @celebimustafa/binance-futures-connector
Gathering detailed insights and metrics for @celebimustafa/binance-futures-connector
Gathering detailed insights and metrics for @celebimustafa/binance-futures-connector
npm install @celebimustafa/binance-futures-connector
Typescript
Module System
Min. Node Version
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
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 } = 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))
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)) 8
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 { 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
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
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.
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
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>
.
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.