Python 3 REST API Call Example – Step by Step Guide

Logan Jul 13, 2026

In the current digital landscape, interacting with REST APIs has become a staple for developers, enabling communication with servers and performing a myriad of tasks. Python, with its simplicity and extensive libraries, is a popular choice for making API calls. Let's dive into making REST API calls using Python 3, with special emphasis on the requests library.

🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍
🌍✨ PYTHON FOR WORKING WITH APIs: INTEGRATE WITH THE WEB ✨🌍

Before we proceed, ensure that you have the requests library installed. If not, you can install it using pip:

API Error Handling in Python: Complete Guide with Code Examples
API Error Handling in Python: Complete Guide with Code Examples

pip install requests

Understanding the requests Library

Python: Simple Rest API Example and String Formatting
Python: Simple Rest API Example and String Formatting

The requests library in Python allows you to send HTTP requests using syntax similar to the one you would use in a web browser.

Before we explore sending different types of requests, let's first understand the basics by sending a GET request:

Python API Tutorial + Flet + JSON + Weather App
Python API Tutorial + Flet + JSON + Weather App

Sending a GET Request

A GET request is used to retrieve information from the server. Here's a simple GET request to fetch JSON data from a public API, e.g., JSONPlaceholder (https://jsonplaceholder.typicode.com):

import requests

Understanding FastAPI,Celery and RabbitMQ in simple terms
Understanding FastAPI,Celery and RabbitMQ in simple terms

response = requests.get('https://jsonplaceholder.typicode.com/todos/1')

print(response.json())

The response.json() method is used to parse the JSON response text from the server. You can also use response.text to get the response as a string.

Build Your First Python API in 6 Steps | Flask Tutorial for Beginners
Build Your First Python API in 6 Steps | Flask Tutorial for Beginners

Handling Errors and Status Codes

It's crucial to handle errors that might occur during API calls. The requests library simplifies this by providing an attribute, status_code, and a method, raise_for_status().

an image of a computer screen with many lines and numbers on the bottom half of it
an image of a computer screen with many lines and numbers on the bottom half of it
an image of a computer screen with the text python's choice sheet
an image of a computer screen with the text python's choice sheet
PYTHON BASICS
PYTHON BASICS
Python APIs Explained — Fetch Real Internet Data for Beginners
Python APIs Explained — Fetch Real Internet Data for Beginners
30 Python Projects For Beginners | Easy To Advanced Python Project Ideas
30 Python Projects For Beginners | Easy To Advanced Python Project Ideas
a white sheet with the words 50 python interview questions to practice on it and an image of
a white sheet with the words 50 python interview questions to practice on it and an image of
a screen shot of a web page with the text python if - else inside print
a screen shot of a web page with the text python if - else inside print
Python Unit 5 Cheat Sheet 🐍 | File Handling & OOP Basics (AKTU)
Python Unit 5 Cheat Sheet 🐍 | File Handling & OOP Basics (AKTU)
Python Requests Library: GET, POST & DELETE Examples | API Cheat Sheet
Python Requests Library: GET, POST & DELETE Examples | API Cheat Sheet
Python Requests: PUT, DELETE & Error Handling Guide | API Cheat Sheet Part 2
Python Requests: PUT, DELETE & Error Handling Guide | API Cheat Sheet Part 2
Python Request Module
Python Request Module
an iphone screen showing the web page for python central, which is open to several different languages
an iphone screen showing the web page for python central, which is open to several different languages
Python If Else Examples
Python If Else Examples
Send Email using python
Send Email using python
an info sheet with the text what is an app?
an info sheet with the text what is an app?
Patient Information System in Python with Source Code and Database
Patient Information System in Python with Source Code and Database
Python Lists in Python Programming
Python Lists in Python Programming
python aesthetic
python aesthetic
How to read Multiple Columns from CSV File in Python? With Source Code
How to read Multiple Columns from CSV File in Python? With Source Code

Let's update the previous example to handle errors:

import requests

response = requests.get('https://jsonplaceholder.typicode.com/invalid_url')

try:

response.raise_for_status()

except requests.exceptions.HTTPError as err:

print(f'HTTP error occurred: {err}')

else:

print('Success!')

print(response.json())

Making Other Types of API Calls

Besides GET, you can send various other types of requests using the requests library, such as POST, PUT, PATCH, and DELETE. Let's explore sending a POST request to create a new resource.

Sending a POST Request

A POST request is used to send data to a server to create/update a resource. Here's how you can use requests to send a POST request with JSON data:

import requests

import json

payload = {'key1': 'value1', 'key2': 'value2'}

headers = {'Content-Type': 'application/json'}

response = requests.post('https://httpbin.org/post', data=json.dumps(payload), headers=headers)

print(response.json())

Remember to set the Content-Type header appropriately based on the type of data you're sending. Also, ensure that the server accepts requests from your client.

Sending POST Requests with Files

Sometimes, you might need to send files as data. The requests library provides a convenient way to do this. Here's an example of sending a POST request with a file:

import requests

files = {'file': open('filename.txt', 'rb')}

response = requests.post('https://httpbin.org/post', files=files)

print(response.json())

It's essential to close the file after sending the request, which is done automatically when you use the 'with' statement.

In conclusion, understanding and mastering the requests library is vital for any Python developer looking to interact with REST APIs. With the right mindset and practice, you can harness the power of this library to communicate with a multitude of servers and retrieve or send data seamlessly. Happy coding!"