Node.js
const apid = ''; // put your key here
const secretHex = ''; // put your secret here
const { createHmac } = require('crypto');
const invokeAPI = async () => {
try {
const parseHexString = (str) => {
const result = [];
while (str.length >= 2) {
result.push(parseInt(str.substring(0, 2), 16));
str = str.substring(2, str.length);
}
return Buffer.from(result);
};
const time = Math.floor(new Date() / 1000);
const command = "{'command': 'test/copy/1', 'data1': 'THIS IS A TEST'}";
const message = time + command;
const secret = parseHexString(`${secretHex}`);
const hmac = createHmac('sha1', secret).update(message);
const hash = hmac.digest().toString('hex');
const url = `https://survey.saysoforgood.com/API?apid=${apid}&time=${time}&hash=${hash}`;
const params = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: command,
};
const response = await fetch(url, params);
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
};
invokeAPI();