-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbithumb_order.js
59 lines (50 loc) · 1.75 KB
/
bithumb_order.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
import axios from 'axios';
import crypto from 'crypto';
import dotenv from 'dotenv';
dotenv.config();
// Function to create HMAC-SHA512 signature
const createApiSign = (endpoint, params, apiNonce, apiKey, apiSecret) => {
const queryString = Object.keys(params)
.map(key => `${key}=${params[key]}`)
.join('&');
const message = endpoint + '?' + queryString + apiNonce + apiKey;
const hmac = crypto.createHmac('sha512', apiSecret);
const sign = hmac.update(message).digest('base64');
return sign;
};
// Your API key and secret from environment variables
const api_key = process.env.Bithumb_API_KEY;
const api_secret = process.env.Bithumb_SECRET_KEY;
// Current timestamp in milliseconds
const apiNonce = Date.now().toString();
const encodedParams = new URLSearchParams();
encodedParams.set('order_currency', 'EL');
encodedParams.set('payment_currency', 'KRW');
encodedParams.set('units', '1000');
encodedParams.set('price', '7');
encodedParams.set('type', 'bid');
// Endpoint without the host part
const endpoint = '/trade/place';
// Calculate Api-Sign
const apiSign = createApiSign(endpoint, encodedParams, apiNonce, api_key, api_secret);
// Function to make the API request
const makeApiRequest = async () => {
try {
const response = await axios({
method: 'POST',
url: 'https://api.bithumb.com' + endpoint,
headers: {
'api-client-type': "2",
'Api-Key': api_key,
'Api-Nonce': apiNonce,
'Api-Sign': apiSign
},
data: new URLSearchParams(encodedParams).toString()
});
console.log(response.data);
} catch (error) {
console.error(error);
}
};
// Execute the function
makeApiRequest();