Skip to Content
Course content

10.1.1 Using requests to interact with APIs

The requests library in Python is widely used to interact with APIs. It simplifies the process of sending HTTP requests and receiving responses. By using the requests library, you can send requests to various endpoints, handle responses, and parse the data in different formats, such as JSON.

Here’s how you can use requests to interact with APIs effectively:

1. Installing the requests Library

If you haven't installed the requests library, you can do so via pip:

pip install requests

2. Basic API Request

To start interacting with an API, you'll often use a GET request. The GET method is used to retrieve data from an API. Here is an example of how to make a basic GET request:

import requests

# Define the API endpoint URL
url = "https://api.example.com/data"

# Send GET request to the API
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    data = response.json()  # Convert the response to a Python dictionary
    print(data)  # Print the data
else:
    print(f"Failed to retrieve data. Status code: {response.status_code}")

Explanation:

  • requests.get(url) sends a GET request to the specified URL.
  • response.status_code checks if the request was successful (200 OK).
  • response.json() converts the JSON data returned by the API into a Python dictionary.

3. Handling Query Parameters

APIs often accept parameters in the URL to customize the response (e.g., pagination, filtering). You can pass these parameters as a dictionary in the params argument.

import requests

url = "https://api.example.com/data"
params = {
    "page": 2,  # Request the second page of results
    "limit": 5   # Limit the results to 5 items
}

# Send GET request with parameters
response = requests.get(url, params=params)

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Failed to retrieve data. Status code: {response.status_code}")

Here, the parameters page and limit are added to the request URL.

4. Sending Data with POST Requests

When you want to send data to an API (e.g., to create or update a resource), you can use the POST method. Here's an example of sending data with a POST request:

import requests

url = "https://api.example.com/create-item"
data = {
    "name": "New Item",
    "price": 100
}

# Send POST request with data
response = requests.post(url, json=data)

if response.status_code == 201:
    print("Item created successfully!")
    print(response.json())  # Print the response data
else:
    print(f"Failed to create item. Status code: {response.status_code}")

Explanation:

  • requests.post(url, json=data) sends a POST request with the data in JSON format.
  • The json parameter automatically serializes the dictionary to JSON.
  • A status code of 201 indicates that the resource was created successfully.

5. Adding Headers to API Requests

Sometimes, APIs require custom headers for authorization or content type. You can add headers using the headers parameter.

import requests

url = "https://api.example.com/data"
headers = {
    "Authorization": "Bearer YOUR_ACCESS_TOKEN",  # Use OAuth tokens for authorization
    "Content-Type": "application/json"  # Specify that the data is in JSON format
}

# Send GET request with headers
response = requests.get(url, headers=headers)

if response.status_code == 200:
    data = response.json()
    print(data)
else:
    print(f"Failed to retrieve data. Status code: {response.status_code}")

Explanation:

  • The Authorization header is often used to send API tokens (e.g., OAuth tokens).
  • The Content-Type header specifies the format of the data being sent or received (e.g., application/json).

6. Error Handling in API Requests

While interacting with APIs, it’s important to handle potential errors, such as timeouts or invalid responses. You can use try-except blocks to catch errors and prevent your program from crashing.

import requests
from requests.exceptions import Timeout, RequestException

url = "https://api.example.com/data"

try:
    # Send GET request with a timeout
    response = requests.get(url, timeout=10)  # 10 seconds timeout
    response.raise_for_status()  # Raise an error for bad responses (4xx/5xx status codes)
    
    data = response.json()  # Parse the response
    print(data)

except Timeout:
    print("The request timed out.")
except RequestException as e:
    print(f"An error occurred: {e}")

Explanation:

  • timeout=10 sets a 10-second limit for the request.
  • raise_for_status() raises an exception for unsuccessful HTTP responses (e.g., 404, 500).
  • Specific exceptions, such as Timeout and RequestException, are caught and handled.

7. Working with JSON Data

Many APIs return data in JSON format. The requests library makes it easy to parse this data into Python objects.

import requests

url = "https://api.example.com/data"
response = requests.get(url)

if response.status_code == 200:
    data = response.json()  # Parse JSON into a Python dictionary
    print(data)  # Work with the data
else:
    print(f"Request failed. Status code: {response.status_code}")

Explanation:

  • response.json() converts the JSON data into a Python dictionary or list.

8. Making Asynchronous Requests

For efficiency, especially when dealing with multiple API requests, you may use asynchronous programming with libraries like aiohttp to make non-blocking API calls. This allows multiple requests to run concurrently.

import aiohttp
import asyncio

async def fetch_data(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            return await response.json()

async def main():
    url = "https://api.example.com/data"
    data = await fetch_data(url)
    print(data)

# Run the asynchronous code
asyncio.run(main())

Explanation:

  • aiohttp allows for asynchronous API calls.
  • async with ensures that the request is handled asynchronously, improving performance when dealing with many requests.

Summary of Key Concepts:

  1. GET Requests: Retrieve data from an API.
  2. POST Requests: Send data to an API (e.g., creating or updating resources).
  3. Query Parameters: Pass parameters to filter or paginate results.
  4. Headers: Add authentication tokens or specify content types.
  5. Error Handling: Use try-except to manage timeouts and errors.
  6. JSON Parsing: Automatically parse JSON responses into Python objects.
  7. Asynchronous Requests: Use aiohttp for efficient concurrent API calls.

By mastering these techniques, you'll be able to interact with almost any API, handle various scenarios, and efficiently retrieve and send data from/to external services.

Commenting is not enabled on this course.