This is an Alexa skill template that shows how to use results from a remote API in an Alexa response. Using data from an external API is common in Alexa skills. But if you're new to building skills, or new to Node.js/JavaScript, using data from an external API can seem tricky. Hopefully the example code in this template helps simplify things.
There is a live example of this skill that you can try from your Alexa device. Just enable the Ground Control Alexa skill. You can do that by saying: Alexa, enable Ground Control
and then Alexa, open Ground Control
.
The skill returns a list of astronauts currently in space. It gets the data from api.open-notify.org. However, the code is written so you can easily change it to call a different API.
-
If you don't have one already, create an Amazon Developer account.
-
Click the button below to deploy the code for this skill into your Alexa developer account.
-
In the Alexa developer console, set the skill's invocation name.
-
Test the skill with your device or the Alexa simulator.
-
Modify the skill code to call a different API.
There are a ton of ways to call an API using NodeJS. In this skill, the getRemoteData
function in the index.js uses the native http module or the native https module to make an HTTP GET request to a provided URL.
There are other node modules that could be used to call an API, like the popular axios module. The http/https modules were used here because they are native to NodeJS. However, the axios module can be a lot easier to work with - especially for more complex cases - for example, when you need to get results from multiple endpoints before returning a response.