The REST function allows users and the bot to communicate and interact with online applications through a REST endpoint and to store the results for later use.
Here you can define the inputs which you can then use in the path, headers and body by surrounding them with square brackets (This is an [Input]).
You can choose all of the possible REST methods, GET to get information, POST to send information (with a JSON body), PUT and PATCH to edit information and DELETE to delete information.
Here you can put the full function path as provided by the endpoint host.
The header contains metadata about your request. For example the content type or authentication information.
The body is only available for the POST, PUT and PATCH methods. Here you need to provide a JSON in the correct format as provided by the endpoint host. The body accepts FormData or raw JSON data.
As a result of making any rest call, the endpoint will respond with a JSON, which can contain information about your call, for example if it was successful, and any information you requested. You can define the path of any value you want to extract from the JSON by giving the consecutive element names separated by dots in the object path. If you need to address a list within the JSON (square brackets) you can do this by giving the index of the element you want, starting at zero.
Within the Blits platform it is possible to make use of reverse indexing (-1 will give you the last element of the list, -2 will give you the second to last element in the list and so on)
With the show output function you can show the result of your REST call. The default values will be used for the variables. If you do not define any outputs it will show you the full JSON output. If you do define outputs, it will show your output values.
In this example we will get weather information for a city. We will use the API from OpenWeatherMaps, whose documentation can be found here: https://openweathermap.org/current
Click add a function. The name can be anything, description is optional. Function type is of course REST.
For the input you must define the city for which you want to find weather info, so call the input variable city with default value Amsterdam. As we will only be getting information, the method will be GET. The API Path can be found on the open weather map website and is:
https://api.openweathermap.org/data/2.5/weather?q=[City]&appid=4252a8f378a51e841eb91528c5ef2c00&units=metric
The first part until the first question-mark is the function we want to call. After that we will input settings. firstly we will pass the city variable. we define that we want to pass more variables with the ampersand (&). after that we pass our personal API key, which can be found on the OpenWeatherMap website. Finally we tell the API that we want to use metric units.
For the header we will write down “content-type: application/json” (without the “”) This is used to tell the endpoint that we are making an API call and wish to obtain a JSON.
Finally we define for the outputs that we want to obtain the weather description and temperature. To this end we define two outputs: WeatherCondition and Temperature
For the first one the object path is weather.0.description and for the second variable the path is main.temp