Package Exports
- binance
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (binance) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Binance
A wrapper for the Binance REST and WebSocket APIs. Uses both promises and callbacks, and beautifies the binance API responses that normally use lots of one letter property names. For more information on the API and parameters for requests visit https://www.binance.com/restapipub.html
Usage/Example
const api = require('binance');
const binanceRest = new api.BinanceRest({
key: 'api-key', // Get this from your account on binance.com
secret: 'api-secret', // Same for this
timeout: 15000, // Optional, defaults to 15000, is the request time out in milliseconds
recvWindow: 10000, // Optional, defaults to 5000, increase if you're getting timestamp errors
disableBeautification: false
/*
* Optional, default is false. Binance's API returns objects with lots of one letter keys. By
* default those keys will be replaced with more descriptive, longer ones.
*/
});
// You can use promises
binanceRest.allOrders({
symbol: 'BNBBTC' // Object is transformed into a query string, timestamp is automatically added
})
.then((data) => {
console.log(data);
})
.catch((err) => {
console.error(err);
});
/*
* Or you can provide a callback. Also, instead of passing an object as the query, routes
* that only mandate a symbol, or symbol and timestamp, can be passed a string.
*/
binanceRest.allOrders('BNBBTC', (err, data) => {
if (err) {
console.error(err);
} else {
console.log(data);
}
});
/*
* WebSocket API
*
* Each call to onXXXX initiates a new websocket for the specified route, and calls your callback with
* the payload of each message received. Each call to onXXXX returns the instance of the websocket
* client if you want direct access(https://www.npmjs.com/package/ws).
*/
const binanceWS = new api.BinanceWS();
binanceWS.onDepthUpdate('BNBBTC', (data) => {
console.log(data);
});
binanceWS.onAggTrade('BNBBTC', (data) => {
console.log(data);
});
binanceWS.onKline('BNBBTC', '1m', (data) => {
console.log(data);
});
/*
* onUserData requires an instance of BinanceRest in order to make the necessary startUserDataStream and
* keepAliveUserDataStream calls. The webSocket instance is returned by promise rather than directly
* due to needing to request a listenKey from the server first.
*/
binanceWS.onUserData(binanceRest, (data) => {
console.log(data);
}, 60000) // Optional, how often the keep alive should be sent in milliseconds
.then((ws) => {
// websocket instance available here
});
REST APIs
allPrices([callback function])
allBookTickersTi([callback function])
depth(query object|string, [callback function])
aggTrades(query object|string, [callback function])
klines(query object, [callback function])
ticker24hr(query object|string, [callback function])
newOrder(query object, [callback function])
testOrder(query object, [callback function]) - If this ends up making a real order it's the API, not this library
queryOrder(query object|string, [callback function])
cancelOrder(query object|string, [callback function])
openOrders(query object|string, [callback function])
allOrders(query object|string, [callback function])
account(query object, [callback function])
myTrades(query object|string, [callback function])
withdraw(query object|string, [callback function])
withdrawHistory(query object|string, [callback function])
depositHistory(query object|string, [callback function])
depositAddress(query object|string, [callback function])
startUserDataStream([callback function])
keepAliveUserDataStream(query object|string, [callback function])
closeUserDataStream(query object|string, [callback function])
WebSocket APIs
onDepthUpdate(symbol, eventHandler) - Returns the websocket, an instance of https://www.npmjs.com/package/ws
onKline(symbol, interval, eventHandler) - Returns the websocket, an instance of https://www.npmjs.com/package/ws
onAggTrade(symbol, eventHandler) - Returns the websocket, an instance of https://www.npmjs.com/package/ws
onUserData(binanceRest, eventHandler, [interval]) - Will return the websocket via promise, interval defaults to 60000, is the amount of time between calls made to keep the user stream alive, binanceRest should be an instance of BinanceRest that will be used to get the listenKey and keep the stream alive