Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

How to Connect and Call APIs in Python?

MAKE A CALL TO AN API IN PYTHON

Hello, readers! In this article, we’ll talk about the different ways to connect and call APIs in Python. So, let us get started!


What is an API?

API is an acronym for Application programming Interface. It can be understood as a composition of rules that enables us to access an external service on web through our systems.

Thus, an API determines and sets certain formats wherein which we can access the service and the data to and from a model. Through a programming language perspective such as Python, an API is considered as a data source available on web which can be accessed through particular libraries of a programming language.


Types of requests to an API

While working with APIs, the below are some list of common instructions or commands we use to do certain sort of actions on the API–

  1. GET command : It enables the users to fetch the data from the APIs onto their system in a specific format that is usually JSON.
  2. POST command : This command enables us to add data to the API i.e. to the service on web.
  3. DELETE command: It enables us to delete certain information from the API service on web.
  4. PUT command : Using PUT command, we can update an existing data or information in the API service on web.

Status/Response codes of an API

On connecting to an API, it returns certain response code which determines the status of our connection that is made to the API on web. Let us have a look at some of the status codes–

  1. 200 : OK. It means we have a healthy connection with the API on web.
  2. 204: It depicts that we can successfully made a connection to the API, but did not return any data from the service.
  3. 401 : Authentication failed!
  4. 403 : Access is forbidden by the API service.
  5. 404 : The requested API service is not found on the server/web.
  6. 500 : Internal Server Error has occurred.

Steps to Connect and Call APIs using Python

Let us now discuss the steps to make a healthy connection to an API using Python as the scripting language.

Example 1: Connecting to an URL on web

In this example, we would be following the below steps to form a healthy connection to an URL on web.

1. Import the necessary library

In order to connect to and API and perform actions on it, we need to import Python requests library into the environment.

import requests

2. Perform an action to connect to the API

Here, we have used GET command to connect to the API as shown–

response_API = requests.get('https://www.askpython.com/')

We have passed the url to which the connection needs to be made into the get() function.

3. Print the response code

The status_code variable enables us to have a look at the status of our connection to the API.

response_API.status_code

You can find the entire code below!

import requests
response_API = requests.get('https://www.askpython.com/')
print(response_API.status_code)

Output:

200

Example 2: Connecting to a GMAIL API

In this example, we would form a healthy connection to an Open Source GMAIL API from this link.

Have a look at the below piece of code!

Example:

import requests
response_API = requests.get('https://gmail.googleapis.com/$discovery/rest?version=v1')
print(response_API.status_code)

Output:

200

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python, stay tuned with Python @ AskPython and till then, Happy Learning!! 🙂