Let’s decode one of the fanciest terminology in data science, the API.
What is an API?
Application Program Interface. Wait you didn’t ask for its expansion, you wanted to know what an API actually meant and what it is used for; but sadly, this what you get to know once you google for an API and then you get extremely confused. As always, we’re here to help.
An API, enables developers to integrate one app with another. They are the interfaces provided by servers that you can use to, among others, retrieve and send data using code.
A simpler explanation?
Let’s assume you wanted to ask Github to send you all the user details for your study. Even though you wanted the data for the study, Github cannot give you login credentials (email, password, etc.) of other users in your hands. But Github can do one thing, it can provide you with a portal/interface from where you could fetch limited user data (like only no. of commits made, no. of repositories, etc.). This interface is called an API. An API enables developers (like you ;)) to integrate one app (your python code) with another (like Github). They are the interfaces provided by servers that you can use to, among others, retrieve and send data using code.
Why use APIs?
APIs can help you fetch real-time data for your application in a well-structured way. They help in automating things that might have taken months if you did them manually using your web browser.
How do APIs send data?
Before we deep dive into using APIs, we need to know how the server will respond to our requests. When a request fails, we can have different reasons for the same; maybe the server wasn’t functioning properly or we requested for something strange. All of this information is communicated using HTTP status codes, which are 3-digit numbers divided into categories.
Informational responses (100–199),
Successful responses (200–299), =>This is what we aim for
Client errors (400–499) => The errors from client side (due to us)
and Server errors (500–599) => The errors from server side
What is a request?
You would encounter three major terminologies that sum up to make a request:
Endpoints => This is usually a url from which you need to fetch data
Methods => either GET, PUT, POST, or DELETE.
Headers => this contains information regarding authentication.
When are we going to actually work with APIs?
Now, Let’s fetch data from covid19api.com. First, we need to find the right endpoint. You can do so by looking up the API documentation of the same https://documenter.getpostman.com/view/10808728/SzS8rjbc?version=latest#7934d316-f751-4914-9909-39f1901caeb8
We would be using an inbuilt library json (to convert json response to python object) and install the requests library (to use its methods to make requests for data)
Let’s Print json_response to see what we got print(json_response)
You can see we received an array of dictionaries. Now we need to iterate between the array to access data (keys and values) inside dictionary.
Let’s plot the Dates with the no. of cases.
And we told you! API’s did it. They can do much more wonders when used the right way for the right projects. You can even create one (but that’s a blog for another day). Hope you have a good understanding of the use case of this beautiful piece of technology in data science.
Recommended » Benford's Law