Yash Agrawal
Yash Agrawal

Reputation: 1

API Key in NodeJS for Alexa Lambda

How do I call an API in Node.Js with an API Key and a HTTPS: Request? Here's What I'm trting to do but to no avail. Also where should I put the API Key? I haven't put it here.

    var options = {
      host: 'demo4444447.mockable.io',
      port: 80,
      method: 'GET',
      path: '/alexa-skill.json'
    }

    var req = http.request(options, res => {
        res.setEncoding('utf8');
        var returnData = "";

        res.on('data', chunk => {
            returnData = returnData + chunk;
        });

        res.on('end', () => {
          var result = JSON.parse(returnData);

          //callback(result);
          this.response.speak(`The current temperature is ${result.temperature} degrees with a humidity of ${result.humidity} and a cloud cover of ${result.cloudcover}.`);

         this.emit(':responseReady');
        });

    });
    req.end();

Upvotes: 0

Views: 219

Answers (1)

K Mo
K Mo

Reputation: 2155

API keys are normally sent as headers. Example:

var options = {
  host: 'demo4444447.mockable.io',
  port: 80,
  method: 'GET',
  headers: {'headername': 'headervalue'},
  path: '/alexa-skill.json'
}

You'll need to know the header name though, e.g. 'x-api-key'.

Upvotes: 1

Related Questions