Fermat
Search
⌃K

Friendly APIs

Learning how to make a simple Weather App
Fermat is even better when use external services to modify or add elements to your personal sessions.
In this example we'll demonstrate how to get data from a common open service such as the Open Weather API
Check your city weather within Fermat
To use many external we'll use the fetch command to get a resource from the network which will return a response we can use. The simplest form is to fetch an URL and wait for a response, usually in JSON format. Then, a common structure we'll use is the following:
do fetch apiID then (response):
print response
set me.result to (do parseJson response)
end
Particularly for our Weather app, we use Built-in API request by city name, that needs both a city and an API Key as parameters.
An API key is used to control and limit users requests, so you can update the corresponding parameter with your key easily in the code.
set city to "Barcelona"
set me.value to "Get Weather API"
set me.getWeather to (city):
set yourKey to "yourAPIKey"
set api to "https://api.openweathermap.org/data/2.5/weather?q="+city+"&appid="+yourKey
do fetch api then (response):
print response
set me.result to (do parseJson response)
end
end
when click me :
do me.getWeather with city
end
To get your key, you can follow the steps described in the Open weather website where you can choose the plan that better fits your needs (you can try the free one which already allows 60 requests/minute).
When we Run this code, using the Inspector we can explore the different parameters that we get from the Weather API:
A good practice when using APIs is to define a result variable within the block to easily inspect the API result
We are gonna use only parameters related to the current temperature, a weather description and a corresponding icon. So, we firstly create some blocks to fill with the API result and we'll later update them when receiving the response:
Defining IDs from blocks
Finally, we use our me.result property to fill them with the API data
when click me :
do me.getData with "e152be6a9637f40935609808ebf117b1" and yourCity.value
set temperatureBlock.value to (me.result["main"]["temp"]) as text + "°"
set descriptionBlock.value to me.result["weather"][0]["main"]
set iconURL to "http://openweathermap.org/img/w/" + me.result["weather"][0]["icon"] + ".png"
set resultImage.source to iconURL
end
Final Weather app with code
set yourCity to "yourTextBlockID"
set temp to "yourTempBlockID"
set description to "yourDescriptionBlockID"
set resultImg to "yourImageBlockID"
set me.getWeather to (city):
set yourKey to "yourKey"
set api to "https://api.openweathermap.org/data/2.5/weather?q="+city+"&units=metric&appid="+yourKey
do fetch api then (response):
print response
set me.result to (do parseJson response)
end
end
when click me :
do me.getWeather with yourCity.value
set temp.value to (me.result["main"]["temp"]) as text + "°"
set description.value to me.result["weather"][0]["main"]
set iconURL to "http://openweathermap.org/img/w/" + me.result["weather"][0]["icon"] + ".png"
set resultImg.source to iconURL
end