节点连接符
这是一个轻量级的库 ,作为连接器 二进制公共 API它设计得简单、干净、易于使用,而且依赖性极小.
- 支持的 API :
/api/*/sapi/*- Spot Websocket 市场流
- Spot 用户数据流
- 点点 Websocket API
- 包括测试案例和实例
- 自定义基址
- 支持请求超时和 HTTP 代理( 自 v2)
- 可显示响应元数据
- 可自定义的 Logger
安装
npm install @binance/connector
文档
https://binance.github.io/binance-connectionor-node/ 互联网档案馆的存檔,存档日期2014-12-21
剩余 APIs
const { Spot } = require('@binance/connector')
const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret)
// Get account information
client.account().then(response => client.logger.log(response.data))
// Place a new order
client.newOrder('BNBUSDT', 'BUY', 'LIMIT', {
price: '350',
quantity: 1,
timeInForce: 'GTC'
}).then(response => client.logger.log(response.data))
.catch(error => client.logger.error(error))
请找到 examples 要检查更多终点的文件夹.
基于密钥对等认证
const { Spot, PrivateKeyAlgo } = require('@binance/connector')
const apiKey = ''
const apiSecret = '' // has no effect when RSA private key is provided
// load private key
const privateKey = fs.readFileSync('/Users/john/ssl/private_key_encrypted.pem')
const privateKeyPassphrase = 'password'
const privateKeyAlgo = PrivateKeyAlgo.RSA // for RSA key
const privateKeyAlgo = PrivateKeyAlgo.ED25519 // for Ed25519 key
const client = new Spot(apiKey, apiSecret, {
privateKey,
privateKeyPassphrase, // only used for encrypted key
privateKeyAlgo
})
// Get account information
client.account().then(response => client.logger.log(response.data))
测试网
虽然 /sapi/* 端点还没有测试网环境, /api/* 结束点可以在
点点测试网。您可以通过更改基址来使用它 :
// provide the testnet base url
const client = new Spot(apiKey, apiSecret, { baseURL: 'https://testnet.binance.vision'})
基础 URL
若为 base_url 未提供,默认为 api.binance.com。 。 。 .
建议通过 base_url 参数,即使在生产中,因为Binance在出现性能问题时提供替代的URL:
https://api1.binance.comhttps://api2.binance.comhttps://api3.binance.com
可选参数
可选参数被封装到单个对象上,作为最后一个函数参数.
const { Spot } = require('@binance/connector')
const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret)
client.account({ recvWindow: 2000 }).then(response => client.logger.log(response.data))
时间单位
API支持时间戳值的不同时间单位. 默认情况下,时间戳值以毫秒提供. 您可以在请求参数中指定时间单位 :
const { Spot, TimeUnit } = require('@binance/connector')
const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret)
// Using milliseconds (default)
client.exchangeInfo({ timeUnit: TimeUnit.MILLISECOND }).then(response => client.logger.log(response.data))
// Using microseconds
client.exchangeInfo({ timeUnit: TimeUnit.MICROSECOND }).then(response => client.logger.log(response.data))
超时
在请求中以毫秒的速度设置超时是很容易的. 如果请求需要超过超时,则请求将被中止. 如未定出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出入出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出出.
const { Spot } = require('@binance/connector')
const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret, { timeout: 1000 })
client.account()
.then(response => client.logger.log(response.data))
.catch(error => client.logger.error(error.message))
代理服务器
这个 axios 软件包被用作此库中的 http 客户端 。 一个代理设置被传递到 axios 详情可在 这里数字 :
const { Spot } = require('@binance/connector')
const apiKey = ''
const apiSecret = ''
const client = new Spot(apiKey, apiSecret,
{
proxy: {
protocol: 'https',
host: '127.0.0.1',
port: 9000,
auth: {
username: 'proxy_user',
password: 'password'
}
}
}
)
您可能拥有一个 HTTP 代理, 它可以带来您需要通过 HTTP 代理实现 HTTP 连接的问题 。 您可以通过 npm 包建造 HTTPS- over- HTTP 隧道来做到这一点 隧道,然后将转子代理传递给 httpsAgent 内 axios。 。 。 .
const tunnel = require('tunnel')
const agent = tunnel.httpsOverHttp({
proxy: {
host: "127.0.0.1",
port: 3128
}
})
const client = new Spot(null, null,
{
baseURL: "https://api.binance.com",
httpsAgent: agent
}
)
client.time()
.then(response => client.logger.log(response.data))
.catch(error => client.logger.error(error))
本评论 提供更详细的情况.
响应元数据
Binance API 服务器在每个响应头提供重用. 此信息可以从 headers 属性. x-mbx-used-weight 财务报告和审定财务报表 x-mbx-used-weight-1m 显示1分钟内消耗的总重量.
// client initialization is skipped
client.exchangeInfo().then(response => client.logger.log(response.headers['x-mbx-used-weight-1m']))
自定义 Logger 集成
const Spot = require('@binance/connector')
const fs = require('fs')
const { Console } = require('console')
// make sure the logs/ folder is created beforehand
const output = fs.createWriteStream('./logs/stdout.log')
const errorOutput = fs.createWriteStream('./logs/stderr.log')
const logger = new Console({ stdout: output, stderr: errorOutput })
const client = new Spot('', '', {logger: logger})
client.exchangeInfo().then(response => client.logger.log(response.data))
// check the output file
软件包中定义的默认日志是 节点控制台类。其输出发送给 process.stdout 财务报告和审定财务报表 process.stderr,与全球控制台相同.
错误
API服务器可能还原出两种出错类型,用户必须妥善处理:
-
Client error- 当服务器返回时, 将丢弃此选项
4XX这是客户端的问题. - 下列财产可能有助于解决问题:
- 回复信头 - 请参见
Response Metadata详细情况. - HTTP 状态代码
- 错误代码 - 服务器错误代码, 例如 .
-1102 - 错误消息 - 服务器的错误消息, 例如 .
Unknown order sent. - 请求配置 - 配置发送到服务器,可以包括URL,请求方法和信头.
- 回复信头 - 请参见
// client initialization is skipped client.exchangeInfo({ symbol: 'invalidSymbol' }) .then(response => client.logger.log(response.data)) .catch(err => { client.logger.error(err.response.headers) // full response header client.logger.error(err.response.status) // HTTP status code 400 client.logger.error(err.response.data) // includes both error code and message client.logger.error(err.response.config) // includes request's config }) - 当服务器返回时, 将丢弃此选项
-
Server error- 当服务器返回时, 将丢弃此选项
5XX服务器那边的问题.
- 当服务器返回时, 将丢弃此选项
网页
网页串流
const { WebsocketStream } = require('@binance/connector')
const logger = new Console({ stdout: process.stdout, stderr: process.stderr })
// define callbacks for different events
const callbacks = {
open: () => logger.debug('Connected with Websocket server'),
close: () => logger.debug('Disconnected with Websocket server'),
message: data => logger.info(data)
}
// initialize websocket stream with microseconds as the preferred time unit
const websocketStreamClient = new WebsocketStream({ logger, callbacks, timeUnit: TimeUnit.MICROSECOND })
// subscribe ticker stream
websocketStreamClient.ticker('bnbusdt')
// close websocket stream
setTimeout(() => websocketStreamClient.disconnect(), 6000)
取消订阅 Websocket 流
// unsubscribe websocket stream
websocketStreamClient.unsubscribe('bnbusdt@kline_1m')
WebSocket API 服务器
const { WebsocketAPI, TimeUnit } = require('@binance/connector')
const logger = new Console({ stdout: process.stdout, stderr: process.stderr })
// callbacks for different events
const callbacks = {
open: (client) => {
logger.debug('Connected with Websocket server')
// send message to get orderbook info after connection open
client.orderbook('BTCUSDT')
client.orderbook('BNBUSDT', { limit: 10 })
},
close: () => logger.debug('Disconnected with Websocket server'),
message: data => logger.info(data)
}
// initialize WebsocketAPI client with microseconds as the preferred time unit
const websocketAPIClient = new WebsocketAPI(null, null, { logger, callbacks, timeUnit: TimeUnit.MICROSECOND })
// disconnect the connection
setTimeout(() => websocketAPIClient.disconnect(), 20000)
网页上有更多的网页实例 examples 文件夹
自动重接
如果有用户未启动的相近事件,则重联机制将在5秒后被触发.
Ping 服务器
可以从客户端调取服务器, 并期望收到 PONG 消息 .
websocketStreamClient.pingServer()
自定义 Logger 集成
软件包中定义的默认日志是 节点控制台类。其输出发送给 process.stdout 财务报告和审定财务报表 process.stderr,与全球控制台相同.
请注意,当连接被初始化时,控制台会以下列形式输出一个召回列表: listen to event: <event_name>。 。 。 .
测试
npm install
npm run test
限制
未来和香草选项 API 不支持:
/fapi/*/dapi/*/vapi/*- 相关 Websocket 市场和用户数据流
许可证
麻省理工学院