Example: Node.js Device API

Sample code to consume the Device API using Node.js.


var moment = require('moment');
var CryptoJS = require("crypto-js");
var https = require('https');
const apiPath = '/api/device';
const timestamp = moment().utc().format('YYYY-MM-DD HH:mm:ss');
console.log(timestamp);
const SECRET = "INSERT_SECRET";
const API_KEY = "INSERT_API_KEY";
const secretMessage = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256('GET' + timestamp + apiPath, SECRET));
const options = {
    "method": "GET",
    "hostname": "www.kbremote.net",
    "port": 443,
    "path": apiPath,
    "headers": {
        "Authentication": API_KEY + ':' + secretMessage,
        "Timestamp": timestamp
    }
};
const req = https.request(options, function (res) {
    const chunks = [];
    res.on("data", function (chunk) {
        chunks.push(chunk);
    });
    res.on("end", function () {
        const body = Buffer.concat(chunks);
        httpRequest.next(body.toString())
    });
});
req.on('error', error => {
    httpRequest.next(error);
});
req.end();

This article was helpful for 1 person. Is this article helpful for you?