How to connect to API endpoint and query data using Python?

This recipe helps you connect to API endpoint and query data using Python

Recipe Objective

In big data scenarios , we are going to connect to multiple API endpoints and need to retrieve the data from the api which is the very first step of data extraction and perform some processing like parsing , cleaning, transformation on the data for deriving business value out of the data.

Access Snowflake Real-Time Project to Implement SCD's

System requirements :

  • Install the python module as follows if the below modules are not found:
  • pip install requests
  • The below codes can be run in Jupyter notebook , or any python console
  • In this scenario we are using an open api to make an requests and the link open api Click Here
  • Link to some public api’s for you Open-API's

Step 1: Import the module

The most common library for making requests and working with APIs is the requests library. You’ll need to import it. Let’s start with that important step:

import requests

Step 2: Making an HTTP request

To make a request to the API, there are different types of requests like GET, POST etc. GET request is the most commonly using one It used to get the data from api.when get the request is successful then it will give the response status code 200 , to make GET request we will use the get() method.

Sample code to make a request :

import requests response = requests.get('https://ghibliapi.herokuapp.com/films/') if response.status_code == 200: print("Succesful connection with API.") print('-------------------------------') data = response.json() print(data) elif response.status_code == 404: print("Unable to reach URL.") else: print("Unable to connect API or retrieve data.")

In the above code if the request response statuscode is 200, then it will print the "Successful connection with API." and also prints data in the json object format , otherwise it will print the "unable to reach URL".

Output of the above code:

Step 3: To query the data by sending the params

To query the specific data from the api will pass the "params" variable as send argument in the get method.

import requests response = requests.get('https://ghibliapi.herokuapp.com/films/', params={'id' :"4e236f34-b981-41c3-8c65-f8c9000b94e7"}) if response.status_code == 200: print("Successful connection with API.") print('-------------------------------') data = response.json() elif response.status_code == 404: print("Unable to reach URL.") else: print("Unable to connect API or retrieve data.") for record in data: print("Title: {},\n Release_Date: {},\n Director: {},\n".format(record['title'] , record['release_date'],record['director']))

Output of the above code: In the above code we query the specific data from the api and print the data in the sorted structure, Here the result is it will print specific id of data.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.