In this tutorial, we will learn how to fetch data from an API using Python. APIs, or Application Programming Interfaces, are a way for different software applications to communicate with one another.
When we fetch data from an API, we’re typically requesting data from a server and receiving that data as a response. Python’s built-in libraries and third-party libraries such as Requests and JSON make it straightforward to fetch data from APIs in a few simple steps.
Step 1: Install Required Libraries
To fetch data from an API, we require the Requests library in Python, which makes it easy to send HTTP requests. If you don’t have the library installed, you can install it using the following command:
bash
pip install requests
Step 2: Choose an API to Fetch Data from
In this tutorial, we will use a free and open API called JSONPlaceholder, which provides placeholder data typically used for testing and prototyping. You can access the API at https://jsonplaceholder.typicode.com/.
For example, let’s fetch a list of users from the JSONPlaceholder API. The API endpoint for this is https://jsonplaceholder.typicode.com/users.
Step 3: Send a Request Using the Requests Library
Now that we have chosen an API to fetch data from, let’s use the Requests library to send an HTTP GET request and receive the response. Here’s how to do it:
1 2 3 4 |
import requests url = "https://jsonplaceholder.typicode.com/users" response = requests.get(url) |
Step 4: Check the Response Status
Before we parse the data received from the API, we need to check if the response status code is 200. A status code of 200 indicates a successful HTTP request. If it’s not 200, there might be an issue with the request.
1 2 3 4 |
if response.status_code == 200: print("Success! Data received.") else: print("Request failed.") |
Step 5: Parse the JSON Data
Most APIs return data in JSON (JavaScript Object Notation) format. Python makes it easy to parse JSON data using its built-in json
module. Let’s convert the JSON data received from the API into a Python data structure, such as a list of dictionaries:
1 2 3 |
import json data = json.loads(response.text) |
Now that we’ve fetched and parsed the data, we can use it as required. For instance, let’s print the name and email of each user:
1 2 3 4 |
for user in data: print("Name: ", user['name']) print("Email: ", user['email']) print() |
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import requests import json url = "https://jsonplaceholder.typicode.com/users" response = requests.get(url) if response.status_code == 200: print("Success! Data received.") else: print("Request failed.") data = json.loads(response.text) for user in data: print("Name: ", user['name']) print("Email: ", user['email']) print() |
Output
Success! Data received. Name: Leanne Graham Email: [email protected] Name: Ervin Howell Email: [email protected] ...
Conclusion
In this tutorial, we learned how to fetch data from an API in Python using the Requests library. We sent an HTTP GET request to the JSONPlaceholder API, checked the response status, and parsed the JSON data using Python’s built-in json
module. You can use the same approach to fetch data from any API and manipulate it according to your needs.