binance-connector
contains this components
Component | Info | Status | Document Link |
---|---|---|---|
Spot | Spot API | Completed | Spot |
Futures | Futures API | Completed | Futures |
Coin-M | Coin-M API | Completed | Coin-M |
Options | European API | Completed | Options |
BLVT Streams | BLVT Streams | Completed | BLVT |
Websocket | Abstract Websocket | Completed | |
Http | Abstract Http | Completed |
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
you need to enable node ES6 module
package.json -> "type": "module"
npm install binance-connector
import { Futures } from "binance-connector"
Note: Everything isPromised
so you need to do .then
or await
- 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()
Starts | Example | Http Method |
---|---|---|
new | newOrder | POST |
change | changeLeverage | POST | PUT |
delete | deleteOrder | POST | DELETE |
(nothing) | exchangeInfo | GET |
- ws data are either
Buffer
ORRaw string
so you need toJSON.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")