Introduction
In this tutorial, we will be discussing in detail on how to extract REST API data using Python. It is one of the most popular APIs and is used by a majority of applications. We will be using VS code editor for executing the python code. The extracted API data would be in a CSV or text file format.
Now, let us look at what we mean by a REST API.
What is a REST API?
So let’s get started with the basics – what is an API? The term API stands for Application Programming Interface. They are basically mechanisms that enable two software components to communicate with each other using pre-defined protocols. This communication is facilitated by means of requests and responses.
Speaking of the architecture of APIs, there is a client-side as well as a server-side. The application that sends the request is called the client while the application that sends the response is called the server. There are several ways in which an API works. One such way is REST API.
A RESTful API (also known as REST API) is one of the most flexible and popular APIs at present. The term REST stands for Representational State Transfer. It is a set of architectural constraints (not a protocol or standard). When a request is made via REST API, a representation of the state of the resource is sent by it to the requester. This representation could be in several formats such as JSON, HTML, Python, plain text, etc.
Extracting REST API using Python
Selecting an API
The first step is to select a free API. Search rapid API in Google. Click on the first link as shown below.
Note that we are not promoting any API here, we are just using one for demonstration purposes. Also note that in order to get the API Key, we need to create an account on the platform.
Next, scroll down the page and click on the option shown below.
Next, search for currency scoop in the search bar as shown below:
Subsequently, you will be able to see a section for an API key and Host details. Note that the API key will be visible only after signing in to your account
Now, let’s get to the coding part.
Python code for extracting REST API data
Importing modules
Here, we will be looking at the code part-by-part for better understanding. The first part of the coding involves importing some modules as shown below:
import requests
import pandas as pd
import json
The modules that we would need are requests, pandas, and json. The requests module is used to send HTTP requests using Python. This module is essential for connecting with the internet and extracting the data from the URL. Following this, we import the Pandas module which is efficient for data cleaning and analysis. Next, we import the json module which allows us to work with data in JSON format.
NOTE: The requests module and pandas module are not in-built modules, unlike the json module. You will have to install them in case you haven’t installed them.
Extracting the data
url = "https://currencyscoop.p.rapidapi.com/latest"
headers = {
"X-RapidAPI-Key": "type-your token-here",
"X-RapidAPI-Host": "currencyscoop.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
myjson = response.json()
myjson_data = myjson['response']
Next, we need to create a variable url and paste the URL given in the code. Following this, we define our headers section where we specify the API Key and the Host details. Headers represent the metadata associated with the API request and response.
Subsequently, we use the request function of the requests module. The first parameter is GET which is used to retrieve data from a server at the specified resource. The second parameter is the URL to the webpage. The third parameter is the headers.
Next, we parse the text retrieved as JSON using the response.json() function. Finally, we create another variable named myjson_data which is assigned to myjson[‘response’]. Note that this is done to get the data within the response section of the data retrieved (this is done so that when we extract data to a CSV file, it is in proper format).
Writing the data
Now, let’s look at how to write this data onto a CSV file.
#writing data in csv format
data_csv = pd.DataFrame(myjson_data)
data_csv.to_csv('currency_rates.csv')
First, we convert the data into a Pandas Dataframe. Next, we convert this to a CSV file using the .to_csv command. This result will be as follows:
TIP: In order to view the columns entirely in Excel, first select everything using Ctrl+A and then go to Format -> Autofit Column Width.
Now, let us look at how to write data to a text file.
#writing data to a text file
with open('currency_rates.txt','w') as f:
json.dump(myjson, f, indent=4)
Now, using the with open function, we open a new text file named currency_rates.txt in write mode. Next, using the json.dump() function, we write all the data into this newly created file. The results are as follows:
Conclusion
So, we have successfully extracted REST API data using Python. We have seen how to choose an API, how to extract it, and how to write it into CSV and text formats. Hope that this tutorial was informative and worth your time!
Happy Learning!