npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

binance-node-api

v0.2.0

Published

Node.js API wrapper for Binance cryptocurrency exchange

Downloads

49

Readme

binance-node-api

This is an open source project created to utilize the Binance API to support automated, algorithmic trading. The project was made and tested for Node 8.0+. It takes advantage of Axios for Promise based HTTP requests and Chai for automated tests.

There are no guarentees towards the stability or effectiveness of this project. Comments, contributions and stars are, however, all welcome.

Installation

npm install binance-node-api

Getting Started

To begin using the API wrapper, require it, create a config object that contains your API key and Secret key provided by Binance, create a new object, delegating the behaviour from Binance. Then run the custom init() function with your config object as a parameter. Example code is as follows:

const Binance = require('binance-node-api')

config = {
  apiKey: 'xXXXXXXXXXXXXXXXXXXXXxxxxxxxxxxxxxxxxxxxxxXXX',
  secretKey: 'xxxxxxxxXXXXXXXXXXXXXxxXXXXXXXXXXXXXxxxXXX'
}

api = Object.create(Binance)
api.init(config)

Using the API Wrapper

Once the API wrapper object is created, you can call any of the associated functions. They will return a Promise which can be utlized with .then/.catch or async/await. Note that the Promise based approach will return directly whereas the async/await approach requires calling the function; although it can be used as an Immediately-Invoked Function Expression (IIFE).

Simple example:

// Promise based approach for getting account information (private & signed)
api.getAccountInfo({timestamp: Date.now()}).then((r) => {
  console.log(r.data)
})
.catch((e) => {
  console.log(e))
})

// Async/Await get account info example (private & signed)
async function getAccountInfo() {
  try {
    let accountInfo = await api.getAccountInfo({timestamp: Date.now()})
    console.log(accountInfo.data)
  } catch(err) {
    console.log(err)
  } 
}

This approach allows for more complex multi-call asynchronous functionality, especially useful for automated trading.

More examples can be found in the example.js file.

Public Endpoints (REST)

Public endpoints to not require an API Key and Secret Key.

api.testConnectivity()
api.getServerTime()
api.getOrderBook(options)

options = {
  symbol: 'string',   MANDATORY
  limit: integer      Default: 100; max 100
} 

api.getAggregateTrades(options)

options = {
  symbol: 'string',   MANDATORY
  fromId: integer,
  startTime: integer, UNIX dateTime
  endTime: integer,   UNIX dateTime
  limit: integer      Default: 500; max 500
}

api.getCandles(options)

options = {
  symbol: 'string',   MANDATORY
  interval: ENUM,     MANDATORY
  limit: integer,     Default: 500; max 500
  startTime: integer, UNIX dateTime
  endTime: integer    UNIX dateTime
}

api.get24hrTicker(options)

options = {
  symbol: 'string'    MANDATORY
}

api.getTicker()
api.getAllBookTicker

Account Endpoints (REST)

All account endpoints require a Binance provided API key and Secret key for signing requests. All crytographic signing is handled automatically by the api wrapper.

api.placeOrder(options)

options = {
  symbol: 'string',         MANDATORY
  side: ENUM,               MANDATORY 
  type: ENUM,               MANDATORY
  timeInForce: ENUM,        MANDATORY
  quantity: 'stringFloat',  MANDATORY
  price: 'stringFloat',     MANDATORY
  newClientOrderId: 'string',
  stopPrice: 'stringFloat',
  icebergQty: 'stringFloat',
  recvWindow: integer
  timestamp: integer        MANDATORY UNIX dateTime
}

api.testNewOrder(options)

options = {
  symbol: 'string',         MANDATORY
  side: ENUM,               MANDATORY 
  type: ENUM,               MANDATORY
  timeInForce: ENUM,        MANDATORY
  quantity: 'stringFloat',  MANDATORY
  price: 'stringFloat',     MANDATORY
  newClientOrderId: 'string',
  stopPrice: 'stringFloat',
  icebergQty: 'stringFloat',
  recvWindow: integer,
  timestamp: integer        MANDATORY UNIX dateTime
}

api.queryOrder(options)

options = {
  symbol: 'string',     MANDATORY
  orderId: integer,
  origClientOrderId: 'string',
  recvWindow: integer,
  timestamp: integer    MANDATORY UNIX dateTime
}

api.cancelOrder(options)

options = {
  symbol: 'string',     MANDATORY
  orderId: integer,
  origClientOrderId: 'string',
  newClientOrderId: 'string',
  recvWindow: integer,
  timestamp: integer    MANDATORY UNIX dateTime
}

api.getOpenOrders(options)

options = {
  symbol: 'string',     MANDATORY
  recvWindow: integer,
  timestamp: integer    MANDATORY UNIX dateTime
}

api.getAllOrder(options)

options = {
  symbol: 'string',     MANDATORY
  orderId: integer,
  limit: integer,       Default: 500; max 500
  recvWindow: integer,
  timestamp: integer    MANDATORY UNIX dateTime
}

api.getAccountInfo(options)

options = {
  recvWindow: integer,
  timestamp: integer    MANDATORY UNIX dateTime
}

api.getAccountTradeList(options)

options = {
  symbol: 'string',     MANDATORY
  limit: integer,       Default: 500; max 500
  fromId: integer,
  recvWindow: integer,
  timestamp: integer    MANDATORY UNIX dateTime
}

api.makeWithdrawl(options)

options = {
  asset: 'string',
  address: 'string',
  amount: 'stringFloat',
  name: 'string'
  recvWindow: integer,
  timestamp: integer   MANDATORY UNIX dateTime
}

api.getDepositHistory(options)

options = {
  asset: 'string',
  status: integer,     (0:pending, 1:sucess)
  startTime: integer,  MANDATORY UNIX dateTime
  endTime: integer,    MANDATORY UNIX dateTime
  recvWindow: integer,
  timestamp: integer   MANDATORY UNIX dateTime
}

api.getWithdrawlHistory(options)

options = {
  asset: 'string',
  status: integer,  
  startTime: integer,  MANDATORY UNIX dateTime
  endTime: integer,    MANDATORY UNIX dateTime
  recvWindow: integer,
  timestamp: integer   MANDATORY UNIX dateTime
}

Websockets

Basic websocket functionality is included. The second input parameter is a function that takes the socket response as a parameter. This provides flexible functionality. Simple examples that simply log the messages are:

api.getDepthStream('ETHUSDT', (msg) => {
  let data = JSON.parse(msg)
  console.log(data)    
})

api.getKlineStream('ETHUSDT', '1m', (msg) => {
  let data = JSON.parse(msg)
  console.log(data)
})

api.getAggTradeStream('ETHUSDT', (msg) => {
  let data = JSON.parse(msg)
  console.log(data)
})

License

This project is open source and uses the ISC license. Feel free to utilize in whatever way you see fit.