Making API calls in python

Scope: This post will help you learn to make API calls using python to fetch data (GET requests).

Data has been given utmost importance these days because it drives applications/businesses. An application may be a website, data science or Machine Learning model, etc. Hence, it's important to learn how to get this data to serve your application and this post will help you understand the same.

Note: We will have a look at some code snippets to understand the concepts. Complete code can be found here

What's an API?

In basic terms, APIs is like channel which allows applications to communicate(send requests, receive data and vice versa) with one another.

source: medium

Make your API call

Wait, but how? Well, there are different ways to fetch data in python (ex: socket, etc.), for this post we will use the requests module in python, requests is a simple, yet elegant HTTP library. To install this use the following command

pip install requests

to check the installed version use this command

pip freeze | grep requests
 requests==2.22.0

More about requests

looks like your environment is ready now to throw some requests. Let's take the popular Dev.to API i.e. https://dev.to/api/articles

def get_data(self, api):
        response = requests.get(f"{api}")
        if response.status_code == 200:
            print("sucessfully fetched the data")
            self.formatted_print(response.json())
        else:
            print(f"Hello person, there's a {response.status_code} error with your request")

once the data is received it will look like this.
image

as you see above, we are first checking the status code and then printing the data. This code tells us about the response that's been received based on our requests. 200 code tells that we have received the info successfully. Multiple codes indicate different responses as below

Code Status Description
200 OK The request was completed.
201 Created A new resource was successfully created.
400 Bad Request The request was invalid.
401 Unauthorized The request did not include an authentication token or the authentication token was expired.
403 Forbidden The client did not have permission to access the requested resource.
404 Not Found The requested resource was not found.
405 Method Not Allowed The HTTP method in the request was not supported by the resource. For example, the DELETE method cannot be used with the Agent API.
409 Conflict The request could not be completed due to a conflict. For example,  POST ContentStore Folder API cannot complete if the given file or folder name already exists in the parent location.
500 Internal Server Error The request was not completed due to an internal error on the server-side.
503 Service Unavailable The server was unavailable.

similarly, we can also make the API call along with some parameters. In our case let's fetch articles of a particular user - me :).

parameters = {
            "username": "kedark"
        }

I have stored the parameters in a variable and now, let's make an API call with the same.

def get_user_data(self, api, parameters):
        response = requests.get(f"{api}", params=parameters)
        if response.status_code == 200:
            print("sucessfully fetched the data with parameters provided")
            self.formatted_print(response.json())
        else:
            print(
                f"Hello person, there's a {response.status_code} error with your request")

The data received will look like this i.e my posts on the dev

Well, you just received data using an API request.

To print the data in our program, we are using the response.json() method it Returns a JSON object of the result (if the result was written in JSON format, if not it raises an error).

Similarly, we have different methods which will return different information

Property/Method Description
apparent_encoding Returns the apparent encoding
close() Closes the connection to the server
content Returns the content of the response, in bytes
cookies Returns a CookieJar object with the cookies sent back from the server
elapsed Returns a time delta object with the time elapsed from sending the request to the arrival of the response
encoding Returns the encoding used to decode r.text
headers Returns a dictionary of response headers
history Returns a list of response objects holding the history of request (URL)
is_permanent_redirect Returns True if the response is the permanently redirected URL, otherwise False
is_redirect Returns True if the response was redirected, otherwise False
iter_content() Iterates over the response
iter_lines() Iterates over the lines of the response
json() Returns a JSON object of the result (if the result was written in JSON format, if not it raises an error)
links Returns the header links
next Returns a PreparedRequest object for the next request in a redirection
ok Returns True if status_code is less than 400, otherwise False
raise_for_status() If an error occur, this method returns a HTTPError object
reason Returns a text corresponding to the status code
request Returns the request object that requested this response
status_code Returns a number that indicates the status (200 is OK, 404 is Not Found)
text Returns the content of the response, in unicode
url Returns the URL of the response

source: W3

well, you can try them one by one. That will be a good exercise. This data can be used to feed your application now. If you prefer using CSV, the JSON received can be easily converted to CSV, this post on StackOverflow how-can-i-convert-json-to-csv can help you achieve this.

Code

You can use this code to make your first API request

That's all for this post folks. Hope you enjoyed it. Don't forget to ❀ or 📑 this article if you did.

Have a great day,
Happy Coding!

21