· 5 years ago · Sep 19, 2020, 01:02 PM
1const axios = require("axios");
2const crypto = require("crypto");
3
4const baseUrl = "https://api.binance.com/";
5const getAccountData = "api/v3/account";
6let timeOffset = 0;
7const recvWindow = 5000;
8
9function buildQuery(data) {
10 return Object.keys(data)
11 .reduce(function (a, k) {
12 a.push(k + "=" + encodeURIComponent(data[k]));
13 return a;
14 }, [])
15 .join("&");
16}
17
18function getAccount(apiKey, apiSecret, data = {}) {
19 if (!apiKey || !apiSecret) {
20 throw new Error(
21 "You need to pass an API key and secret to make authenticated calls."
22 );
23 }
24 data.timestamp = new Date().getTime() + timeOffset;
25 if (typeof data.recvWindow === "undefined") data.recvWindow = recvWindow;
26 const query = buildQuery(data);
27 const signature = crypto
28 .createHmac("sha256", apiSecret)
29 .update(query)
30 .digest("hex");
31 const url = baseUrl + getAccountData + "?" + query + "&signature=" + signature;
32 return axios
33 .get(url, {
34 headers: {
35 "X-MBX-APIKEY": apiKey,
36 },
37 })
38 .then(x=>x.data)
39 .catch((err) => console.log("error:", err));
40}
41
42const access_key =
43 "xxxxxxxxxxxxxxxxxxxxxxxx";
44const secret_key =
45 "xxxxxxxxxxxxxxxxxxxxxxxx";
46
47getAccount(access_key, secret_key, {}).then(account=>{
48 console.log("account", account)
49})