All Authenticated APIs require a body object with url and timeStamp_nonce as mandatory keys along with the required params of the API being called. Create a signature using the payload and api secret and pass it along with the payload in headers.
Copy let data = {
"key": <YOUR_API_KEY>,
"Secret": <YOUR_API_SECRET>,
}
const crypto = require('crypto');
const request = require('request');
const base_url = "https://api.autosnipe.ai/sniper-api";
async function callAuthAPI(end_point, body = {}, method) {
const timeStamp_nonce = Date.now().toString();
body.url = base_url + end_point;
body.timeStamp_nonce = timeStamp_nonce;
let payload = getPayload(body);
let signature = getSignature(payload, data.Secret);
let headers = {
'x-autosnipe-apikey': data.key,
'x-autosnipe-signature': signature,
'Content-Type': 'application/json'
};
let options = {
url: body.url,
method: method,
headers: headers,
body: JSON.stringify(body)
}
request(options, function(error, res, body) {
if (!error) {
try {
console.log({ error, res, body });
} catch (err) {
console.log("Error ", err);
}
} else {
console.log("Error2 ", error);
}
});
}
function getPayload(body) {
const content = {
url: body.url,
timeStamp_nonce: body.timeStamp_nonce,
body: JSON.stringify(body)
};
return Buffer.from(JSON.stringify(content)).toString('base64');
}
function getSignature(payload, apiSecretKey) {
return crypto.createHmac('sha512', apiSecretKey)
.update(payload)
.digest('hex');
}