0%

tomoon | BINANCE API 的对接

主要分为

  • RestFul
  • Websocket

具体使用请参考

SPOT

Websocket

非认证

服务地址输入

1
wss://stream.binance.com:9443/stream?streams=

发包内容

1
{"method": "SUBSCRIBE","params":["btcusdt@depth5@100ms","ethusdt@depth5@100ms"],"id": 1}

这里有几个需要注意的点。

  • 内容全部是小写
  • 内容不能用单引号,必须全是双引号

USWAP

Restful

认证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
async def sign(self, request: RhinoRequest) -> NoReturn:
try:
cex_key = request.extra.get("key")
cex_secret = request.extra.get("secret")
encode_str = ""
if request.method == "GET" or request.method == "DELETE":
if len(request.params) > 0:
for key, value in request.params.items():
encode_str += str(key) + "=" + str(value) + "&"
elif request.method == "POST":
aa = urllib.parse.urlencode(request.data)
if len(request.data) > 0:
encode_str = urllib.parse.urlencode(request.data) + "&"
encode_str = bytes(encode_str[:-1], encoding="utf8")
signature = hmac.new(cex_secret.encode('utf-8'), encode_str, hashlib.sha256).hexdigest()
if request.method == "GET" or request.method == "DELETE":
request.params["signature"] = signature
elif request.method == "POST":
request.data["signature"] = signature
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"X-MBX-APIKEY": cex_key
}
request.headers = headers
return request
except Exception as e:
self.gateway.logger.error(f"{self.gateway.exchange_sub} sign is error")
self.gateway.logger.error(traceback.format_exc())

Websocket

输入

1
wss://fstream.binance.com/ws

发包内容

1
{"method": "SUBSCRIBE","params":["btcusdt@depth5@100ms","ethusdt@depth5@100ms"],"id": 1}
请我喝杯咖啡吧~