node.jsaws-lambdaalexa-skills-kitapi-authorization

Alexa skill that uses data from an external API with API-KEY


Im trying to access an external API from the alexa back end code using lambda that runs on node.js 8.1,the code can access any endpoint that doesnt require an api-key but i cant find a way to include my authoraztion (api-key) in the code so i can retrieve the data that im looking for. the api documentation that im trying to access is as follows:

curl --request GET -H 'Authorization: Bearer ' -H 'Content-Type: application/json' "https://some-end-point/path/i/want"

this is for the alexa-skills-kit,it uses a lambda after the skill is invoked and tries to access an external api whith an api-key.The code can retrieve info to any endpoint that doesnt require any key. I already tried including the key as a parameter in the URL (api key + URL),since im new to alexa,lambda,nodejs im not sure how to debug it but i just dont get the desire output(which is alexa turning the text to speech with the info that got from the external api).

pd:asuming my api key is: xxxx-xxxx-xxxx

// endpoint that i want
url = https://some-end-point/path/i/want

await getRemoteData(url)
      .then((response) => {
        const data = JSON.parse(response);
        outputSpeech = `the data thati want is ${data.records.length} `;
        for (let i = 0; i < data.records.length; i++) {
          if (i === 0) {
            //first record
            outputSpeech = outputSpeech + data.records[i].fields.name + ', '
          } else if (i === data.records.length - 1) {
            //last record
            outputSpeech = outputSpeech + 'y '+data.records[i].fields.name + 
             ', '
          } else {
            //middle record(s)
            outputSpeech = outputSpeech + data.records[i].fields.name + ', '
          }
        }
      })



//function getRemoteData
const getRemoteData = function (url) {
  return new Promise((resolve, reject) => {
    const client = url.startsWith('https') ? require('https') : require('http');
    const request = client.get(url,(response) => {
      if (response.statusCode < 200 || response.statusCode > 299) {
        reject(new Error('Failed with status code: ' + response.statusCode));
      }
      const body = [];
      response.on('data', (chunk) => body.push(chunk));
      response.on('end', () => resolve(body.join('')));
    });
    request.on('error', (err) => reject(err))
  })
};

the code above can acces any endpoint without errors but i dont know how to include the api key so it can acces the api,the output expected is to have access to the api by including the api-key

Any help on this problem would be gladly apreciated from this newbie ....


Solution

  • You need to pass a options object as the second parameter of client.get. For example:

    const options = {
      headers: {
        'Authorization': 'Bearer <your API key>'
      }
    }
    
    

    Then where you do the request:

    const request = client.get(url, options, (response) => {
      // Do the rest of your stuff here...  
    }
    

    You can find more details on the options here.