How to use the API and the variables

Variables and API are currently the way we can interact with different services and functions for our skill

There are some cases when you need to save some data from the user (age, first name, favorite dish, city of residence, etc.) for further use or pass it to a 3rd party API it to get a more tailor made experience in the skill.

In this case let's suppose we want a voice application that would be able to gather information about the current weather in a city.

To properly gather the data from the user, we would need to go through three steps:

Create the variable name

From the menu Variables you must create a variable with the content you are expecting to get [More Info]

For our skill, we will set the slot with data type US_CITY



Create the request

Creating a request is rather easy:

Just go on the flow, press Add a Step and choose Request, then add the informations:


 

Create the response

From the dropdown menu select "API call", this will allows you to send GET/POST request to the external APIs to pull/push some data from there and use it later in the skill.

  • Label — like "get weather" or anything you can come up with.
  • Method — HTTP method of your API request (GET or POST).
  • URL — URL of the requested API .
  • Headers — some information that you send through API request, like tokens and authentication keys, in JSON format.
  • API Response — get the value for the specific API call, in JSON format, in this case, main.temp

Just as example here is the 'structure' of the JSON answer that we are analyzing:

$ curl --silent 'https://api.openweathermap.org/data/2.5/weather?q=Milan&appid=yourapikey' |jq
{
  "coord": {
    "lon": 9.19,
    "lat": 45.47
  },
  "weather": [
    {
      "id": 521,
      "main": "Rain",
      "description": "shower rain",
      "icon": "09d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 287.32,
    "pressure": 1017,
    "humidity": 87,
    "temp_min": 283.71,
    "temp_max": 289.82
  },
  "visibility": 8000,
  "wind": {
    "speed": 4.1,
    "deg": 80
  },
  "clouds": {
    "all": 75
  },
  "dt": 1556214476,
  "sys": {
    "type": 1,
    "id": 6742,
    "message": 0.0078,
    "country": "IT",
    "sunrise": 1556166084,
    "sunset": 1556216449
  },
  "id": 6542283,
  "name": "Milan",
  "cod": 200
}

So, when we write: main.temp we are referring to the section main and key temp to fetch his value as shown here:

$ curl --silent 'https://api.openweathermap.org/data/2.5/weather?q=Milan&appid=yourapikey' |jq '.main.temp'
286.91



That's it. If you have any more questions about variables, take a look at our "Variables FAQ" or message our support, we'd happy to help. 🙂


Didn't find an answer to your question?

Get in touch with us for details on additional services and custom work pricing

Open a Ticket