APIs are a set of functions that allows applications to access data and interact with external software components, operating systems, or micro services. They can be sources of data that are designed to take care of a company/individual needs.
They also offer services that can be complemented with personal software i.e Individuals can build their software/application on an API.
Basically, anytime an app like Facebook is used, an instant message is sent, or weather is checked on the phone, an API is used.
Some of the reasons APIs are used include:
- To provide a source for data that changes quickly e.g Weather API. Data such as weather forecast changes all the time. How do we have access to such data? APIs.
- To get a small piece of a larger data.
To reduce repeated computation(a very good example is not having to scrape a site all the time).
APIs are hosted on web servers, but instead of browsers asking for web pages, it asks for data in JSON(JavaScript Object Notation) format.
Endpoints
are server route for retrieving specific data from an API e.g /comments, /users.
/comments
might be used to return all comments made by a user.
Developers use almost any modern programming language(like JavaScript, Python, Java et.c) to develop APIs. In this article, Python will be used to interact with API.
TODO
Determine the the weather of a country from an API.
PSEUDOCODE
- Go to Open Weather Map website
- Sign up for the free plan
- Navigate to
API keys
on the user dashboard - Create a key and you’re ready to go.
SHELL CODE
> pip install requests
PYTHON CODE
import os
import requests
url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": "Nigeria",
"appid": the_generated_key, # " 'the_generated_key' is replaced with the key generated from open weather map"
"units": "metric"
}
response = requests.get(url, params=params)
print(response.json())
CODE EXPLANATION
- The
requests
module is imported. A parameter(params) containing;
q
- required field - name of country/city.appid
- required field - the generated key from open weather map.units
- optional - this is the unit of measurement.A request is sent to
http://api.openweathermap.org/data/2.5/weather
withparams
passed as an argument.response.json()
returns a JSON object that contains the information about the weather ofNigeria
.
{'coord': {'lon': 8, 'lat': 10},
'weather': [
{'id': 500,
'main': 'Rain',
'description': 'light rain',
'icon': '10d'}
],
'base': 'stations',
'main': {'temp': 297.39,
'feels_like': 297.88,
'temp_min': 297.39,
'temp_max': 297.39,
'pressure': 1017,
'humidity': 77,
'sea_level': 1017,
'grnd_level': 930},
'visibility': 10000,
'wind': {'speed': 3.68, 'deg': 242, 'gust': 4.9},
'rain': {'1h': 0.19},
'clouds': {'all': 83},
'dt': 1628505953,
'sys': {'country': 'NG',
'sunrise': 1628486316,
'sunset': 1628531310},
'timezone': 3600,
'id': 2328926,
'name': 'Nigeria',
'cod': 200}
Most APIs require authentication for security as well as "Rate Limiting". Rate Limiting is to prevent too many request too quickly.
There are different types of request. Examples are: GET
, POST
, PATCH
, PUT
, DELETE
.
NB:
/weather
is an endpoint. Another endpoint on open weather map is/solar_radiation
In order to interact with APIs, one must be able to read and understand API documentation. HERE is the documentation of the API used in this article.