1.0.2 • Published 1 year ago

@ixjb94/binance-connector v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

CodeFactor npm version npm size npm downloads last commit Known Vulnerabilities

Structure

binance-connector contains this components

ComponentInfoStatusDocument Link
SpotSpot APICompletedSpot
FuturesFutures APICompletedFutures
Coin-MCoin-M APICompletedCoin-M
OptionsEuropean APICompletedOptions
BLVT StreamsBLVT StreamsCompletedBLVT
WebsocketAbstract WebsocketCompleted
HttpAbstract HttpCompleted

Spot: contains: Wallet, Sub-Account, Market Data, Spot, Margin, Savings, Staking, Mining, Futures, Futures Algo, Portfolio, BLVT, BSwap, Fiat, C2C, VIP Loans, Crypto Loans, Crypto Loans, Pay, Convert, Rebate, NFT, Binance Code (all available endpoints in binance spot doc)

Websocket & Http: can connect/request to any of binance ws/rest endpoints

Requirement

you need to enable node ES6 module
package.json -> "type": "module"

Installation

npm i @ixjb94/binance-connector

Usage

import { Futures } from "binance-connector"

Examples

Note: Everything isPromised so you need to do .then or await

All Examples

  • Rest (Public)
import { Futures } from "./index.js"

let myFuture = new Futures({
    isTestNet: true,
})

// exchange info
myFuture.exchangeInfo()

// candles data
myFuture.klines({
    interval: "1m",
    symbol: "BTCUSDT",
    limit: 10,
})
  • Rest (Private)
import { Futures } from "./index.js"

let myFuture = new Futures({
    api_key: "MyApiKey",
    api_secret: "MyApiSecret",
    isTestNet: true,
})

// get account balance
myFuture.balance()

// place new order
myFuture.newOrder({
    symbol: "BTCUSDT",
    side: "BUY",
    type: "MARKET",
    quantity: 0.01,
})
  • Websocket (Public)
import { Futures } from "./index.js"

let myFuture = new Futures({
    isTestNet: true,
})

// subscribe to two market data
myFuture.ws.subscribe(["btcusdt@kline_1m", "ethusdt@kline_3m"], 1, "MyMarketData")

// listen for data coming from binance
myFuture.ws.addListener("MyMarketData", (socket) => {

    socket.addEventListener("message", (event) => {
        let data = event.data
        data = JSON.parse(data)

        console.log(data)
    })

})
  • Websocket (Private)
import { Futures } from "./index.js"

let myFuture = new Futures({
    api_key: "MyApiKey",
    api_secret: "MyApiSecret",
    isTestNet: true,
})

async function Run() {

    // 1- get the listenKey
    let reqListenKey = await myFuture.newListenKey()
    let listenKey = reqListenKey.listenKey

    // 2- subscribe to User Data Stream
    myFuture.ws.userStream(listenKey, "MyUserData")

    // 3- Listen for data coming from binance
    myFuture.ws.addListener("MyUserData", (socket) => {
        
        socket.addEventListener("message", (event) => {
            let data = event.data
            data = JSON.parse(data)

            console.log(data)
        })

    })
}
Run()

Types & Intellisense

img1 img2

Endpoints Naming

StartsExampleHttp Method
newnewOrderPOST
changechangeLeveragePOST | PUT
deletedeleteOrderPOST | DELETE
(nothing)exchangeInfoGET

Notes

  • ws data are either Buffer OR Raw string
    so you need to JSON.parse them
    example
import { Futures } from "binance-connector"

let myFuture = new Futures({
    isTestNet: true,
})

myFuture.ws.subscribe(["btcusdt@kline_1m"], 1)

myFuture.ws.addListener("DATA", (socket) => {

    // Buffer
    socket.addListener("message", (data, siBinary) => {
        console.log(data)
    })

    // Raw string data
    socket.addEventListener("message", (event) => {
        // Its raw
        let data = event.data

        // Now its parsed
        data = JSON.parse(data)
    })

})
  • You don't need to import Http OR Websocket directly
    you can access them with .http. OR .ws.
    example
import { Futures } from "binance-connector"

let myFuture = new Futures({
    isTestNet: true,
})

// websocket example
myFuture.ws.subscribe(["btcusdt@kline_1m"], 1)

// http example
myFuture.http.publicGET("/fapi/v1/exchangeInfo")

Documentation

Binance Doc