Reputation: 1302
I just starting to learn how to develop Amazon Alexa Skills using the Alexa Skills Kit and AWS Lambda. I don't quite understand how to call an API and retrieve data from it. I found this template code from a Jordan Leigh video:
var endpoint = ""; // ENDPOINT GOES HERE
var body = "";
https.get(endpoint, (response) => {
response.on('data', (chunk) => body += chunk);
response.on('end', () => {
var data = JSON.parse(body);
var subscriberCount = data.items[0].statistics.subscriberCount;
context.succeed(
generateResponse(
buildSpeechletResponse(`Current subscriber count is ${subscriberCount}`, true),
{}
)
);
});
});
I understand that the endpoint
variable will hold the url for the API, but I am unsure about the rest. In this code I believe he using a YouTube API for the current subscriber count. If I wanted to, for example, use the Dark Sky API to extract weather info, how would I go about this using this similar format?
Upvotes: 0
Views: 3026
Reputation: 944
Pretty much the same.
const https = require('https');
var body = "";
const url = "https://api.darksky.net/forecast/your-secret-key/37.8267,-122.4233"
var req = https.request(url, (res) => {
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
var data = JSON.parse(body);
console.log("daily weather: ", data.daily);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
Upvotes: 2