Developing API Alexa Skills
|
If you have content in your existing API, perhaps it is time to consider creating an Alexa Skill to make that content available. I’ll cite 3 examples in this post ranging from simple to complex. If you’re wondering what the heck an Alexa Skill is and why in the world you would need to create one, please start with my earlier post on Getting Started Creating Alexa Skills here.
Can Alexa call an API? Yes.
Example #1 – Cocktail Roulette
My first API Alexa Skill was Cocktail Roulette. This skill randomly provides a cocktail, ingredients, and instructions on how to make said cocktail.
To create this skill, I found an API that generated a random cocktail here – https://www.thecocktaildb.com/api/json/v1/1/random.php. I added some phrasing around the raw data, a few utterances, and voilà, I authored a new Alexa skill. The key here is for the API to return something that we can code the skill to parse and present back to the user.
In less than 50 lines of code, I was able to create a skill with literally hundreds of unique answers. Check out Cocktail Roulette here.
What about Google Home?
In this previous post, I mentioned that I used a tool called ‘Alexa Importer‘ from Google successfully exactly once. (All other Alexa Importer tries were unsuccessful.) This Cocktail Roulette skill was the first and only skill I have been able to convert for Google Home using the Alexa Importer.
Example #2 – State Population
Now let’s look at a sample provided by Amazon of a basic API call. https://github.com/alexa/alexa-cookbook/blob/master/external-calls/httpsGet/src/index.js
This sample triggers the state population API and returns the population of Texas.
Try it for yourself by clicking here -> https://cp6gckjt97.execute-api.us-east-1.amazonaws.com/prod/stateresource?usstate=Texas
If you clicked on the link you would get:
{“Name”:”Texas”,”population”:27500000,”rank”:2}
From here, you parse the population 27500000 and script Alexa to say, “The population of Texas is 27 million, five hundred thousand.”
The code is extremely simple, instead of hard-coding 50 states and their respective populations, and subsequently updating this information, the skill simply pulls the latest information via API.
That’s why API skills are so popular, instead of updating a skill with the latest information, your API pulls the latest and greatest information from your existing database using your standard API calls.
Now, let’s get to a more practical application of API skills.
Example #3 – Activity Tracker
One of my favorite apps to track my workouts is Strava. How does Strava work? I workout, it automatically uploads my activity (usually a run) with GPS data, mileage, pace, etc to my public Strava profile. Then my Strava Friends can give me kudos for a particular workout. Great app to keep me motivated to continue my workout regimen.
This skill is a little more complex as I have to opt-in and authenticate my existing Strava account with this skill. I’ll cover how we opt-in and authenticate using Account Linking in a future post. If you need an immediate answer, check out this post from Amazon on 5 Easy Steps to Account Linking.
Assuming we have authenticated, now I want to call the Strava API to provide me with specific information about my running stats. Running stats is one simple example, but imagine the possibilities now that you can run an API command with the sound of your voice!
We see APIs in action every day as we fill out forms on websites that return specific information. The challenge here is less content, and creating a “voice first” experience for your skill user.
More likely, you’ll want to make Alexa conversational, where Alexa will ask the next question based on the user’s answer to the previous question in a multi-turn conversation. This is called ‘request-chaining,’ where the user input drives the next output from Alexa. Want to learn more? Check out this Medium post from Lars Trieloff on combining REST API calls.
As an enterprise business, API calls to a database, followed by a write API call to your CRM is most likely the most useful way to create your Alexa Skill. Please contact me if you seek my expertise in this area.