API
The base endpoint is: https://api.binance.com
If there are performance issues with the endpoint above, these API clusters are also available:
Websocket
DOCS:
Dùng 1 stream
let ws = new WebSocket('wss://stream.binance.com:9443/ws/solusdt@trade');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log('xxxONMESSAGE', msg);
}
Dùng nhiều stream
let ws = new WebSocket('wss://stream.binance.com:9443/stream?streams=solusdt@trade/ethusdt@trade');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log('Stream = ', msg.stream);
console.log('Data = ', msg.data);
}
Send a message
var msg = {
foo: "bar"
};
ws.send(JSON.stringify(msg));
Hoặc kết nối rồi mới subscribe
let ws = new WebSocket('wss://stream.binance.com:9443/ws');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
var msg = {
"method": "SUBSCRIBE",
"params":
[
"solusdt@aggTrade",
"solusdt@depth"
],
"id": 1
}
ws.send(JSON.stringify(msg));
};
ws.onmessage = function (event) {
console.log('xxxONMESSAGE', event.data);
}
Cấu trúc thông tin của trade
stream
{
"e": "trade", // Event type
"E": 123456789, // Event time
"s": "BNBBTC", // Symbol
"t": 12345, // Trade ID
"p": "0.001", // Price
"q": "100", // Quantity
"b": 88, // Buyer order ID
"a": 50, // Seller order ID
"T": 123456785, // Trade time
"m": true, // Is the buyer the market maker?
"M": true // Ignore
}
Cấu trúc thông tin của aggTrade
stream
{
"e": "aggTrade", // Event type
"E": 123456789, // Event time
"s": "BNBBTC", // Symbol
"a": 12345, // Aggregate trade ID
"p": "0.001", // Price
"q": "100", // Quantity
"f": 100, // First trade ID
"l": 105, // Last trade ID
"T": 123456785, // Trade time
"m": true, // Is the buyer the market maker?
"M": true // Ignore
}
Lấy thông tin candlestick
Format: kline_<interval>
let ws = new WebSocket('wss://stream.binance.com:9443/stream?streams=solusdt@kline_15m');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log('Stream = ', msg.stream);
console.log('Data = ', msg.data);
}
Cấu trúc kết quả:
{
"e": "kline", // Event type
"E": 123456789, // Event time
"s": "BNBBTC", // Symbol
"k": {
"t": 123400000, // Kline start time
"T": 123460000, // Kline close time
"s": "BNBBTC", // Symbol
"i": "1m", // Interval
"f": 100, // First trade ID
"L": 200, // Last trade ID
"o": "0.0010", // Open price
"c": "0.0020", // Close price
"h": "0.0025", // High price
"l": "0.0015", // Low price
"v": "1000", // Base asset volume
"n": 100, // Number of trades
"x": false, // Is this kline closed?
"q": "1.0000", // Quote asset volume
"V": "500", // Taker buy base asset volume
"Q": "0.500", // Taker buy quote asset volume
"B": "123456" // Ignore
}
}
24h mini ticker
let ws = new WebSocket('wss://stream.binance.com:9443/stream?streams=solusdt@miniTicker');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log('Stream = ', msg.stream);
console.log('Data = ', msg.data);
}
Xem sổ lệnh
let ws = new WebSocket('wss://stream.binance.com:9443/stream?streams=solusdt@bookTicker');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log('Stream = ', msg.stream);
console.log('Data = ', msg.data);
}
Cấu trúc kết quả:
{
"u":400900217, // order book updateId
"s":"BNBUSDT", // symbol
"b":"25.35190000", // best bid price
"B":"31.21000000", // best bid qty
"a":"25.36520000", // best ask price
"A":"40.66000000" // best ask qty
}
IMPORTANT - Xem top các lệnh trong sổ
let ws = new WebSocket('wss://stream.binance.com:9443/stream?streams=solusdt@depth20');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log('Stream = ', msg.stream);
console.log('Data = ', msg.data);
}
IMPORTANT - Xem tất cả các lệnh pending trong sổ
let ws = new WebSocket('wss://stream.binance.com:9443/stream?streams=solusdt@depth');
ws.onopen = function (event) {
console.log('xxxOPEN', event)
};
ws.onmessage = function (event) {
var msg = JSON.parse(event.data);
console.log('Stream = ', msg.stream);
console.log('Data = ', msg.data);
}
Unsubscribe
Có một nút stop khi không cần xem thông tin thị trường nữa:
var msg = {
"method": "UNSUBSCRIBE",
"params":
[
"solusdt@aggTrade",
"solusdt@depth"
],
"id": 1
}
ws.send(JSON.stringify(msg));
Để đặt lệnh giao dịch
References