
How to Make API Call Using Python
- Last Updated On: August 21, 2022
- By: Scriptcrunch Editorial
In this guide you will learn to make API call using Python script using a bearer token and data.
Make API Call using Python
To make an API call using python, you need to use the requests module.
First you need to install the requests module using the following command.
sudo pip3 install requests
Assuming you are making an API request for type application/x-www-form-urlencoded
, you need to have the following variables set.
- http endpoint
- data
- headers: Mostly Bearer token.
Here is an example.
endpoint = "https://endpoint-exampe.com/api"
data = {"scope": "345435"}
headers = {"Authorization": "Bearer dUQsdfErSd34MnV5S1Zhasdf34JHlkjsdklwer&}
Finally you need to use the request post method as shown below.
requests.post(endpoint, data=data, headers=headers).json()
Here is the full script.
endpoint = "https://endpoint-exampe.com/api"
data = {"scope": "345435"}
headers = {"Authorization": "Bearer dUQsdfErSd34MnV5S1Zhasdf34JHlkjsdklwer&}
print(requests.post(endpoint, data=data, headers=headers).json())
Possible Errors
While using the requests module, you might get the following error.
ModuleNotFoundError: No module named 'requests'
You need to install the request module using pip to rectify the ModuleNotFoundError
error.
You can install the requests module using the following command.
sudo pip3 install requests

Scriptcrunch Editorial
Other Interesting Blogs

Linux Foundation Coupon for November 2023
Hi Techies, I wanted to let you know about a pretty sweet deal with the Linux Foundation Coupon that is running now.


Best Black Friday Deals For Techies in 2023
In this blog, we have listed down all the best Black Friday deals and Cyber Monday deals for techies. This is the


CKA Exam Study Guide: Certified Kubernetes Administrator
Passing the cloud-native Certified Kubernetes Administrator (CKA) exam is not a cakewalk. To pass the CKA exam, you should have enough hands-on
