Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

binance

Package Overview
Dependencies
Maintainers
1
Versions
171
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binance

node.js wrapper for the Binance REST and WebSocket APIs

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm downloads testing status code coverage

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])

Response

[
    {
        symbol: 'ETHBTC',
        price: '0.04789700'
    },{
        symbol: 'LTCBTC',
        price: '0.01885200'
    },
    ...
]

allBookTickers([callback function])

Response

[
    {
        symbol: 'ETHBTC',
        bidPrice: '0.04790000',
        bidQty: '15.00000000',
        askPrice: '0.04801200',
        askQty: '13.86000000'
    },
    {
        symbol: 'LTCBTC',
        bidPrice: '0.01885200',
        bidQty: '3.13000000',
        askPrice: '0.01889000',
        askQty: '0.07000000'
    },
    ...
]

ping([callback function])

Response

{}

time([callback function])

Response

{ serverTime: 1513800453289 }

depth(query object|string, [callback function])

Query Parameters

NameTypeDetails
symbolstring
limit (optional)integerDefault and maximum of 100

Response

{
    lastUpdateId: 52347642,
    bids: [
        [
            '0.04838500', // Price
            '0.18400000', // Quantity
            [] // Ignored
        ],
        ...
    ],
    asks: [
        [
            '0.04843900',
            '10.73400000',
            []
        ],
        ...
    ]
}

aggTrades(query object|string, [callback function])

Query Parameters

NameTypeDetails
symbolstring
fromId (optional)longinclude to retrieve trades with ID >= fromId
startTime (optional)longTimestamp in ms, include to retrieve trade.timestamp >= startTime
endTime (optional)longTimestamp in ms, include to retrieve trade.timestamp <= endTime
limit (optional)integerDefault and maximum of 500

Response

[
    {
        aggTradeId: 7891757,
        price: '0.04840900',
        quantity: '0.02800000',
        firstTradeId: 8516629,
        lastTradeId: 8516629,
        timestamp: 1513801086350,
        maker: false,
        bestPriceMatch: true
    },
    {
        aggTradeId: 7891758,
        price: '0.04841100',
        quantity: '0.10600000',
        firstTradeId: 8516630,
        lastTradeId: 8516630,
        timestamp: 1513801086350,
        maker: false,
        bestPriceMatch: true
    },
    ...
]

klines(query object, [callback function])

Query Parameters

NameTypeDescription
symbolstring
intervalstringOptions: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
startTime (optional)longTimestamp in ms
endTime (optional)longTimestamp in ms
limit (optional)integerDefault and maximum of 500

Response

[
    {
        openTime: 1513802580000,
        open: '0.04837200',
        high: '0.04859200',
        low: '0.04837000',
        close: '0.04837100',
        volume: '181.69100000',
        closeTime: 1513802639999,
        quoteAssetVolume: '8.79608770',
        trades: 249,
        takerBaseAssetVolume: '75.74200000',
        takerQuoteAssetVolume: '3.67104146',
        ignored: '0'
    },
    {
        openTime: 1513802640000,
        open: '0.04837100',
        high: '0.04850100',
        low: '0.04837000',
        close: '0.04837200',
        volume: '93.56300000',
        closeTime: 1513802699999,
        quoteAssetVolume: '4.53177905',
        trades: 149,
        takerBaseAssetVolume: '20.14700000',
        takerQuoteAssetVolume: '0.97561723',
        ignored: '0'
    },
    ...
]

ticker24hr(query object|string, [callback function])

Query Parameters

NameTypeDescription
symbolstring

Response

{
    symbol: 'ETHBTC',
    priceChange: '0.00177600',
    priceChangePercent: '3.809',
    weightedAvgPrice: '0.04668755',
    prevClosePrice: '0.04662400',
    lastPrice: '0.04840000',
    lastQty: '4.91500000',
    bidPrice: '0.04831400',
    bidQty: '0.76500000',
    askPrice: '0.04844200',
    askQty: '7.13800000',
    openPrice: '0.04662400',
    highPrice: '0.04900000',
    lowPrice: '0.04400000',
    volume: '306627.07000000',
    quoteVolume: '14315.66748465',
    openTime: 1513716659596,
    closeTime: 1513803059596,
    firstId: 8168545,
    lastId: 8523993,
    count: 355449
}

newOrder(query object, [callback function])

Query Parameters

NameTypeDescription
symbolstring
sideenumBUY, SELL
typeenumLIMIT, MARKET
timeInForceenumGTC, IOC (Good till cancelled, Immediate or Cancel)
quantitydecimal
pricedecimal
newClientOrderId (optional)stringA unique id for the order, will be generated if not sent
stopPrice (optional)decimalUsed with stop orders
icebergQty (optional)decimalUsed with iceberg orders

Response

{
    "symbol": "LTCBTC",
    "orderId": 1,
    "clientOrderId": "myOrder1",
    "transactTime": 1499827319559
}

testOrder(query object, [callback function])

Query Parameters

NameTypeDescription
symbolstring
sideenumBUY, SELL
typeenumLIMIT, MARKET
timeInForceenumGTC, IOC (Good till cancelled, Immediate or Cancel)
quantitydecimal
pricedecimal
newClientOrderId (optional)stringA unique id for the order, will be generated if not sent
stopPrice (optional)decimalUsed with stop orders
icebergQty (optional)decimalUsed with iceberg orders

Response

{}

queryOrder(query object|string, [callback function])

Query Parameters

NameTypeDescription
symbolstring
orderId (optional)longIf not present, origClientOrderId must be sent
origClientOrderId (optional)stringIf not present, orderId must be sent

Response

{
    "symbol": "LTCBTC",
    "orderId": 1,
    "clientOrderId": "myOrder1",
    "price": "0.1",
    "origQty": "1.0",
    "executedQty": "0.0",
    "status": "NEW",
    "timeInForce": "GTC",
    "type": "LIMIT",
    "side": "BUY",
    "stopPrice": "0.0",
    "icebergQty": "0.0",
    "time": 1499827319559
}

cancelOrder(query object|string, [callback function])

Query Parameters

NameTypeDescription
symbolstring
orderId (optional)longIf not present, origClientOrderId must be sent
origClientOrderId (optional)stringIf not present, orderId must be sent
newClientOrderId (optional)stringUsed to uniquely identify this cancel, generated by default

Response

{
    "symbol": "LTCBTC",
    "origClientOrderId": "myOrder1",
    "orderId": 1,
    "clientOrderId": "cancelMyOrder1"
}

openOrders(query object|string, [callback function])

Query Parameters

NameTypeDescription
symbolstring

Response

[
    {
        "symbol": "LTCBTC",
        "orderId": 1,
        "clientOrderId": "myOrder1",
        "price": "0.1",
        "origQty": "1.0",
        "executedQty": "0.0",
        "status": "NEW",
        "timeInForce": "GTC",
        "type": "LIMIT",
        "side": "BUY",
        "stopPrice": "0.0",
        "icebergQty": "0.0",
        "time": 1499827319559
    },
    ...
]

allOrders(query object|string, [callback function])

Query Parameters

NameTypeDescription
symbolstring
orderId (optional)longIf set, retrieve orders with ID >= orderId, otherwise most recent are returned
limit (optional)integerDefault and maximum of 500

Response

[
    {
        "symbol": "LTCBTC",
        "orderId": 1,
        "clientOrderId": "myOrder1",
        "price": "0.1",
        "origQty": "1.0",
        "executedQty": "0.0",
        "status": "NEW",
        "timeInForce": "GTC",
        "type": "LIMIT",
        "side": "BUY",
        "stopPrice": "0.0",
        "icebergQty": "0.0",
        "time": 1499827319559
    },
    ...
]

account(query object, [callback function])

Response

{
    "makerCommission": 15,
    "takerCommission": 15,
    "buyerCommission": 0,
    "sellerCommission": 0,
    "canTrade": true,
    "canWithdraw": true,
    "canDeposit": true,
    "updateTime": 1502765854332,
    "balances": [
        {
            "asset": "BTC",
            "free": "4723846.89208129",
            "locked": "0.00000000"
        },
        ...
    ]
}

myTrades(query object|string, [callback function])

Query Parameters

NameTypeDescription
symbolstring
fromId (optional)longTradeId to fetch from. Retrieves most recent trades by default
limit (optional)integerDefault and maximum of 500

Response

[
    {
        id: 1068299,
        orderId: 5496403,
        price: '0.00050431',
        qty: '30.00000000',
        commission: '0.00001513',
        commissionAsset: 'BTC',
        time: 1513275761857,
        isBuyer: false,
        isMaker: false,
        isBestMatch: true
    },
    ...
]

withdraw(query object|string, [callback function])

Query Parameters

NameTypeDescription
assetstring
addressstring
addressTag (optional)stringSecondary address identifier for coins like XRP, XMR, etc...
amountdecimal
name (optional)stringDescription of the address

Response

{
    "msg": "success",
    "success": true,
    "id": "7213fea8e94b4a5593d507237e5a555b"
}

withdrawHistory(query object|string, [callback function])

Query Parameters

NameTypeDescription
asset (optional)string
status (optional)integer{ 0: 'Email Sent', 1: 'Cancelled', 2: 'Awaiting Approval', 3: 'Rejected', 4: 'Processing', 5: 'Failure', 6: 'Completed' }
startTime (optional)longTimestamp in ms
endTime (optional)longTimestamp in ms

Response

{
    "withdrawList": [
        {
            "id": "7213fea8e94b4a5593d507237e5a555b"
            "amount": 1,
            "address": "0x6915f16f8791d0a1cc2bf47c13a6b2a92000504b",
            "asset": "ETH",
            "txId": "0xdf33b22bdb2b28b1f75ccd201a4a4m6e7g83jy5fc5d5a9d1340961598cfcb0a1",
            "applyTime": 1508198532000,
            "status": 4
        },
        ...
    ],
    "success": true
}

depositHistory(query object|string, [callback function])

Query Parameters

NameTypeDescription
asset (optional)string
status (optional)integer{ 0: 'Pending', 1: 'Success' }
startTime (optional)longTimestamp in ms
endTime (optional)longTimestamp in ms

Response

{
    "depositList": [
        {
            "insertTime": 1508298532000,
            "amount": 1000,
            "asset": "XMR",
            "address": "463tWEBn5XZJSxLU34r6g7h8jtxuNcDbjLSjkn3XAXHCbLrTTErJrBWYgHJQyrCwkNgYvyV3z8zctJLPCZy24jvb3NiTcTJ",
            "addressTag": "342341222",
            "txId": "b3c6219639c8ae3f9cf010cdc24fw7f7yt8j1e063f9b4bd1a05cb44c4b6e2509",
            "status": 1
        },
        ...
    ],
    "success": true
}

depositAddress(query object|string, [callback function])

Query Parameters

NameTypeDescription
assetstring

Response

{
    "address": "0x6915f16f8791d0a1cc2bf47c13a6b2a92000504b",
    "success": true,
    "addressTag": "1231212",
    "asset": "BNB"
}

startUserDataStream([callback function])

Response

{ "listenKey": "pqia91ma19a5s61cv6a81va65sdf19v8a65a1a5s61cv6a81va65sdf19v8a65a1" }

keepAliveUserDataStream(query object|string, [callback function])

Response

{}

closeUserDataStream(query object|string, [callback function])

Response

{}

WebSocket APIs

onDepthUpdate(symbol, eventHandler)

Function call returns the websocket, an instance of https://www.npmjs.com/package/ws

Response

{
    eventType: 'depthUpdate',
    eventTime: 1513807798461,
    symbol: 'BNBBTC',
    U: 17962354,
    updateId: 17962357, // syncs with updateId on depth route
    bidDepthDelta: [
        {
            price: '0.00031239',
            quantity: '0.00000000', //quantity of 0 means remove it
            ignored: []
        },
        ...
    ],
    askDepthDelta: [
        {
            price: '0.00031388',
            quantity: '0.00000000',
            ignored: []
        },
        ...
    ]
}

onKline(symbol, interval, eventHandler)

Returns the websocket, an instance of https://www.npmjs.com/package/ws

Response

{
    eventType: 'kline',
    eventTime: 1513808234049,
    symbol: 'ETHBTC',
    kline:
    {
        startTime: 1513808220000,
        endTime: 1513808279999,
        symbol: 'ETHBTC',
        interval: '1m',
        firstTradeId: 8542266,
        lastTradeId: 8542357,
        open: '0.04854000',
        close: '0.04865100',
        high: '0.04865200',
        low: '0.04854000',
        volume: '53.30600000',
        trades: 92,
        final: false,
        quoteVolume: '2.59145375',
        volumeActive: '41.01100000',
        quoteVolumeActive: '1.99422739',
        ignored: '0'
    }
}

onAggTrade(symbol, eventHandler)

Returns the websocket, an instance of https://www.npmjs.com/package/ws

Response

{
    eventType: 'aggTrade',
    eventTime: 1513808427335,
    symbol: 'ETHBTC',
    tradeId: 7915993,
    price: '0.04858100',
    quantity: '0.06100000',
    firstTradeId: 8543066,
    lastTradeId: 8543066,
    time: 1513808427329,
    maker: false,
    ignored: true
}

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

Responses

{
    eventType: 'executionReport',
    eventTime: 1513808673916,
    symbol: 'IOTABTC',
    newClientOrderId: '81gmMRozYdU73D27Ho1W1K',
    side: 'SELL',
    orderType: 'LIMIT',
    cancelType: 'GTC',
    quantity: '10.00000000',
    price: '0.00030120',
    P: '0.00000000',
    F: '0.00000000',
    g: -1,
    C: 'null',
    executionType: 'TRADE',
    orderStatus: 'FILLED',
    rejectReason: 'NONE',
    orderId: 9409314,
    lastTradeQuantity: '10.00000000',
    accumulatedQuantity: '10.00000000',
    lastTradePrice: '0.00030120',
    commission: '0.00000301',
    commissionAsset: 'BTC',
    tradeTime: 1513808673912,
    tradeId: 3023119,
    I: 21799081,
    w: false,
    maker: true,
    M: true
}
{
    eventType: 'outboundAccountInfo',
    eventTime: 1513808673916,
    makerCommission: 10,
    takerCommission: 10,
    buyerCommission: 0,
    sellerCommission: 0,
    canTrade: true,
    canWithdraw: true,
    canDeposit: true,
    balances: [
        {
            asset: 'BTC',
            availableBalance: '0.00301025',
            onOrderBalance: '0.00000000'
        },
        ...
    ]
}

License

MIT

Keywords

FAQs

Package last updated on 22 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc