ataraxie67 / binance

A wrapper for the Binance API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Binance

A wrapper for the Binance REST and WebSocket APIs. 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
    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
const binanceWS = new api.BinanceWS();

binanceWS.onDepthUpdate('BNBBTC', (data) => {
    console.log(data);
});

/*
 * onUserData requires an instance of BinanceRest in order to make the necessary startUserDataStream and  
 * keepAliveUserDataStream calls
 */
binanceWS.onUserData(binanceRest, (data) => {
    console.log(data);
}, 60000); // Optional, how often the keep alive should be sent in milliseconds

binanceWS.onKline('BNBBTC', '1m', (data) => {
    console.log(data);
});

REST APIs

ping([callback function])

time([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])

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

License

MIT

About

A wrapper for the Binance API

License:MIT License


Languages

Language:JavaScript 100.0%