forked from whitebit-exchange/api-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.js
81 lines (63 loc) · 1.77 KB
/
node.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let crypto = require("crypto");
let https = require("https");
const hostname = "whitebit.com";
const publicKey = "";
const secretKey = "";
/**
* @see https://whitebit-exchange.github.io/api-docs/private/http-auth/#body-data
*/
function getRequestBody(url, extendedBodyData) {
return Object.assign(
{
request: url,
nonce: Date.now(),
nonceWindow: true,
},
extendedBodyData
);
}
/**
* @see https://whitebit-exchange.github.io/api-docs/private/http-auth/#headers
*/
function getRequestOptions(method, params) {
const body = JSON.stringify(params);
const payload = Buffer.from(body).toString("base64");
const hash = crypto.createHmac("sha512", secretKey);
const signature = hash.update(payload).digest("hex");
const headers = {
"Content-Type": "application/json",
"X-TXC-APIKEY": publicKey,
"X-TXC-PAYLOAD": payload,
"X-TXC-SIGNATURE": signature,
};
return {
body,
options: { headers, method, hostname, path: params.request },
};
}
/**
* @see https://whitebit-exchange.github.io/api-docs/private/http-trade-v4/#trading-balance
*/
const requestUrl = "/api/v4/trade-account/balance";
const data = getRequestBody(requestUrl, { ticker: "BTC" });
const { body, options } = getRequestOptions("POST", data);
console.log(options);
const req = https.request(options, (res) => {
res.setEncoding("utf8");
console.log(`statusCode: ${res.statusCode}`);
let responseBody = "";
res.on("data", (chunk) => {
responseBody += chunk;
});
res.on("end", () => {
if (res.statusCode !== 200) {
console.error("Api call failed with response code", res.statusCode);
}
console.log("Body:", responseBody);
});
});
req.on("error", (error) => {
console.error("Request error", error);
});
req.write(body);
req.end();