Fetching Data Using API In Python


Let me introduce the process of fetching data from the server using API in Python.
i will use the requests library. The requests library removes the complexity of making the requests to the source.

I tried

Let's begin with installing libraries.

In order to work with APIs in Python, we need tools that will make those requests. Execute the following commands:

pip install requests
pip install pandas

Import following libraries

import requests
import json
import pandas as pd

GET request

Multiple HTTP methods such as GET and POST, defines the action that we are trying to perform when we make HTTP request. GET is one of the most commonly used HTTP method. GET method is use to get or retrieve the data from a specified source.

Below is the python code example of making GET requests. We can make GET request to GitHub’s RootRest API.

Example Code


The get() function returns a response object. We can also save the response as an output. The return value is stored in the response variable as shown below.

response = requests.get('https://api.github.com')

Status of request

We can get to know the status of our request.

>>> response.status_code

Status_code is 200, which means that request was successful. If status_code is 404, it means that the resource is not found. you can read more about status codes here.

Get the data from API

Let's get the data from API with authentication and save the response as DataFrame. We must send the Url of api and access token for accessing api.

url = 'Your api url'

access_token  = 'access token for api'

headers = {'Authorization': 'Bearer' + access_token,

       'Content-Type': 'application/json; charset=utf-8'


Send the request to the API

Let’s send the request to the API contains information in the headers。

response = request.get(url, headers = headers)

We got the output as response from api.

Fetch the response in JSON format.

Files = response.json()

Convert JSON Data to DataFrame.

We can convert the output JSON Data to Dataframe.

Data = pd.DataFrame(Files)


So, i have successfully fetched Server data using API in Python. Also i have converted output JSON data to panda's DataFrame. Hope that this blog was informative and worth your time.