forked from whitebit-exchange/api-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
61 lines (47 loc) · 1.71 KB
/
Copy pathauth.js
File metadata and controls
61 lines (47 loc) · 1.71 KB
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
let crypto = require('crypto');
let https = require('https');
let apiKey = ''; //put here your public key
let apiSecret = ''; //put here your secret key
let request = '/api/v4/trade-account/balance'; //put here request path. For obtaining trading balance use: /api/v4/trade-account/balance
let hostname = 'whitebit.com'; //domain without last slash. Do not use whitebit.com/
//If the nonce is similar to or lower than the previous request number, you will receive the 'too many requests' error message
let nonce = (Date.now() / 1000).toFixed(0);//nonce is a number that is always higher than the previous request number
let data = {
currency: "BTC", //for example for obtaining trading balance for BTC currency
request: request,
nonce: nonce
}
let dataJsonStr = JSON.stringify(data);
let payload = Buffer.from(dataJsonStr).toString('base64');
let hash = crypto.createHmac('sha512', apiSecret);
let signature = hash.update(payload).digest('hex');
let options = {
hostname: hostname,
path: request,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-TXC-APIKEY': apiKey,
'X-TXC-PAYLOAD': payload,
'X-TXC-SIGNATURE': signature
}
}
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(dataJsonStr);
req.end();