Getting started with APIs | A simple hands-on practice
2 min readMay 3, 2022
Open-Meteo offers free weather forecast APIs for non-commercial use and no API key is required. Since this is just a simple practice for beginners, let’s use it to get a gist of how API calls work. For most APIs, you will need to register for an account and get an API key for authentication. Some APIs have pricing plans to support different data needs.
For this practice, we want to get the current weather condition.
- Go to their documentation. Skim through the available parameters and understand the return object.
2. Run the code
import requestsurl = “https://api.open-meteo.com/v1/forecast"
params = {‘latitude’: 52.52, ‘longitude’: 13.41, ‘current_weather’:’True’}
response = requests.get(url, params=params)
print(response.text)
- requests is a python library that allows use to send http requests easily. The GET method is used to request data from the specified resource.
- The url that we want to send in would be https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t_weather=True. Using the params argument, we can pass the parameters key-value pairs as a dictionary.
- response will be the response code returned by the server. If the request was successful, print(response) will return Response[200].
- To view the content as a string, we can use response.text and Requests will automatically decode the content.
3. Get current weather
json_data = response.json()
json_data[‘current_weather’]
- Requests has a built-in json decoder. We can use response.json() to get the data as a python dictionary.
Hope you like this simple hands-on practice!