Node.js

A simple express app with the following dependencies:

  "dependencies": {
    "express": "^4.13.4",
    "nodedump": "*",
    "dateformat": "*"
  }

To see an example Node.js project go to: https://github.com/lingkio/event-triggered-recipe-nodejs

Example app

var express = require('express');
var crypto = require("crypto"); 
var https = require('https');
var qs = require('querystring');
var dateFormat = require('dateformat');
var nodedump = require('nodedump').dump;
var app = express();

var host = 'www.lingkapis.com';
var apikey = '[yourkey]';
var secret = '[yoursecret]';

app.get('/', function (req, res) {
   var d = new Date();
   var requestPath = '/v1/@self/ps/studentauthentications';
   var requestMethod = "GET";
   var formattedDate = dateFormat(d,"GMT:ddd, dd mmm yyyy HH:MM:ss Z");
   var message = "date: "+ formattedDate + "\n(request-target): " + requestMethod.toLowerCase() + " " + requestPath;
   var hmacer = crypto.createHmac('sha1', secret);
   hmacer.write(message);
   hmacer.setEncoding('base64');
   hmacer.end();

   var sig = hmacer.read();  

   // options for API request
   var options = {
        host: host,
        path: requestPath,
        method: requestMethod,
        headers: {
            'Date': formattedDate, 
            'Authorization': 'Signature keyId="'+apikey+'",algorithm="hmac-sha1",headers="date (request-target)",signature="'+qs.escape(sig)+'"'
        }
   }


 // callback for API Call
   callback = function(response) {

    var body = ''
    response.on('data', function (chunk) {
        body += chunk;
    });

    response.on('end', function () {
        try {

                var parsed = JSON.parse(body);
                //capture dump 
                var output = nodedump(parsed);

                // write response to the browser 
                res.send(
                    '<html>'
                        + '<head>'
                            + '<title>Lingk API Example</title>'
                        + '</head>'
                        +'<body>'
                            +output
                        +'</body>'
                    +'</html>'
                );                
            } catch (err) {
                res.send('Unable to parse response as JSON', err.stack);
            }
        }).on('error', function(err) {
        // handle errors with the request itself
        res.send('Error with the request:', err.message);
        }); 
   }

   // API request
    var httpreq = https.request(options, callback);
    httpreq.end();

   console.log("date " + formattedDate);
   console.log("message " + message);
   console.log("secret " + secret);
   console.log("signature " + sig); 

});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

Last updated