How to use an API in Alexa skill in Alexa-hosted(Python)

Rahul Sinha
6 min readMay 4, 2020

--

We know how to create Fact skill or Rock paper scissor skills in Alexa-Hosted(nodejs). So we are gonna make this time our skill in Alexa-Hosted(Python).

Before starting, We should know what we are making? Using an API will create an Alexa Skill. We will use OpenWeatherMap API to fetch data and show the accurate weather and temperature of a particular city. You should know firstly, what’s an API?

API stands for Application Programming Interface which helps third parties to use a server of an organization or company. We can’t understand by the name of API, but if we try to make it clear ourselves, so remove Application for a while and see on Programming Interface you will see that a program is written and its interface is been used by third parties in their Application.

API is used in various things; it’s been used in Trivago to show hotels and rooms in their website, in Goibibo to show flights in their application. API does this. These companies used a Unique key to integrate their app with companies’ servers. By this method, we will OpenWeatherMap API key to show the necessary details.

Here is a step by step guide to help to get started with API in Amazon Alexa Skill. Let’s start now.

Step 1: Go and login to OpenWeatherMap. After signing in you will get an alert that “ You are already signed in”. Below that alert, you will API Keys is written beside Services. Click on it to get your API keys.

OpenWeather Website

Step 2: On click API keys, you will be navigated to your API keys section. There you are gonna see one “ default API key” which already provided by OpenWeatherMap. We will use that API key. You can also create a new API key, but it takes some time to activate.

Create API key

Step 3: Now to see the JSON data of your API key, just copy this link and paste in your browser and replace {YOUR API KEY} with your API key and {CITY NAME} with the city name. You will see the JSON data of your API key.

http://api.openweathermap.org/data/2.5/weather?appid={YOUR_API_KEY}&q=${CITY_NAME}&units=metric
JSON Format of API

Step 4: Now come to our Alexa Developer Console,

  • Click on Create a new skill
  • Give a unique skill name and choose your country
  • Choose model as Custom
  • This time choose Alexa-Hosted(Python). This time it will be made in python. No problem you don’t know about Python. It’s a very high level and easy to understand language.
  • On the next page, Choose the Hello World template and click on choose. It will take some to build the model.
Name your skill & choose the model

Choose Alexa-hosted(Python)

Step 5: After successfully building the model, you will be navigated to the Front-end i.e is Build section.

  • Give invocation name in lower case as “weather forecast
  • Delete the predefined intent which “HelloWorldIntent” and your intent as “WeatherIntent
  • Add some sample utterances. We are using slots to make this skill. We gonna use predefined slots “AMAZON.city” and connect this slot to {city} variable.

say me weather of {city}
tell me weather of {city}
weather of {city}

NOTE: Don’t use {city} as single add some text with it, if you don’t add any extra text you will utterances conflict.

Frontend

Step 6: Now save the model and build the model to insure it. After building the model come to the “ Code” section where we have to deal with python codes.

Backend or coding section

Step 7: To use an API key, we have to first import requests module, just type at top “ import requests”. Then change the Launch request handler sqeakOutput text according to yourself or you can use an as welcome message”Welcome to Weather Forecast. I will provide you accurate weather of your city.”.

Add you code

Step 8: Now change “ HelloWorldIntentHandler” to your intent “ WeatherIntentHandler”. Agin in the third line you will see “ HelloWorldIntent” change to “ WeatherIntent”.

Change Intent name

Step 9: Remove speakOutput text to .reponse and paste this code which will perform all the functions.

city = handler_input.request_envelope.request.intent.slots[‘city’].valueapi_url= “http://api.openweathermap.org/data/2.5/weatherappid={your_api_key}&q={}&units=metric".format(city)json_data = requests.get(api_url).json()weather_json = json_data[‘weather’][0][‘main’]temp_json = round(json_data[‘main’][‘temp’] )speak_output = “The weather is {} and temperature is {} °C in {}. “.format(weather_json, temp_json, city)reprompt = “ Want to try again? Or you can say cancel to quit.”handler_input.response_builder.speak(speak_output + reprompt)
Add the code

See what we have done here, we took a variable “ city” to store the value the user will give. In python, we don’t have to use any datatypes to define a variable which makes it a dynamically typed language. Then we are store JSON data of API key in variable “ api_url”, just add your API key to fetch data.

Also, I used the variable “ json_data” to store converted JSON data to python-format data. We used get method of the request module to get JSON data and then used json() method to convert that data to python-format data.

Then we used the “ weather_json” variable in which we did indexing to get only the weather of that particular city and also made a variable “ temp_json” to store temperature and used round() method to show the int value instead of a float value.

At last, we speak_output the text. I also used string formatting to print the value of weather_json and temp_json. You can use reprompt to make this open session, it’s optional.

Step 10: Again come bottom of the code there you will see “ HelloWorldIntentHandler” change this too with your intent i.e. “ WeatherIntentHandler”. After all, this stuffs, Save and Deploy. After successful deployment, then come to the “ Test” section to test your skill in a simulator.

  • Make skill-testing enabled from Off to Development to testing your skill in the simulator.
  • Type your invocation like “open weather forecast” then use one of the sample utterances like “weather of delhi”.
Test your Skill

Hurray!! You successfully created your first skill of API in the Alexa developer console. Try to publish your skill in the Amazon Alexa skill store. Also, do share your weather forecast skill with your family and friends to test in Amazon Echo devices.

--

--

Rahul Sinha

Technology enthusiast. Keen interest is in Python, Voice technology, Azure, and IoT. Keen to learn new things.