Calling API using Python

Posted by Afsal on 26-Jan-2024

Hi Pythonistas!

Calling external API one of the most common functionality involved in software development. Today we will learn how to call an API using python. For this post we are using a free open API available in https://reqres.in/.  We are using the most commonly used package called requests

Let us dive into the code

Installation

pip install requests

Fetching data from an API

import requests

url = "https://reqres.in/api/users?page=2"
response = requests.get(url)

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Error: {response.status_code}")
    print(response.text)

We can see that it is very simple to fetch from an API. The above request is a GET request.

So request.get(<url to call>). If the API is successfully returned then status code will be 200. 

We print the data on successful return. Else we are showing the error message from the API

Output

{'page': 2, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [{'id': 7, 'email': 'michael.lawson@reqres.in', 'first_name': 'Michael', 'last_name': 'Lawson', 'avatar': 'https://reqres.in/img/faces/7-image.jpg'}, {'id': 8, 'email': 'lindsay.ferguson@reqres.in', 'first_name': 'Lindsay', 'last_name': 'Ferguson', 'avatar': 'https://reqres.in/img/faces/8-image.jpg'}, {'id': 9, 'email': 'tobias.funke@reqres.in', 'first_name': 'Tobias', 'last_name': 'Funke', 'avatar': 'https://reqres.in/img/faces/9-image.jpg'}, {'id': 10, 'email': 'byron.fields@reqres.in', 'first_name': 'Byron', 'last_name': 'Fields', 'avatar': 'https://reqres.in/img/faces/10-image.jpg'}, {'id': 11, 'email': 'george.edwards@reqres.in', 'first_name': 'George', 'last_name': 'Edwards', 'avatar': 'https://reqres.in/img/faces/11-image.jpg'}, {'id': 12, 'email': 'rachel.howell@reqres.in', 'first_name': 'Rachel', 'last_name': 'Howell', 'avatar': 'https://reqres.in/img/faces/12-image.jpg'}], 'support': {'url': 'https://reqres.in/#support-heading', 'text': 'To keep ReqRes free, contributions towards server costs are appreciated!'}}

Posting data to an API

import requests

url = "https://reqres.in/api/users"
request_data = {
    "name": "Parseltongue",
    "job": "Software engineer"
}

response = requests.post(url, json=request_data)

if response.status_code == 201:
    data = response.json()
    print(data)
else:
    print(f"Error: {response.status_code}")
    print(response.text)

Here the API is post API so we use requests.post also we need to pass the request data for the API. If we pass the data properly status code will be 201 for that we are print the result else we are showing the error message

Output

{'name': 'Parseltongue', 'job': 'Software engineer', 'id': '891', 'createdAt': '2024-01-26T04:36:05.700Z'}

requests module is very simple to use to know deeply about this please visit the official documentation

I hope you have learned something from this post . Please share your valuable suggestions with afsal@parseltongue.co.in