Skip to main content

Making API Requests

Fetching Data from Web Services

· 3 min read

In the previous unit, we explored Python's standard library. Now let's learn how to fetch data from the web using APIs.

Making API Requests

What Is an API?

An API (Application Programming Interface) is a set of rules that lets programs talk to each other. When you want data from a web service, you send a request to its API, and it sends back the data you asked for.

Think of an API like a restaurant menu. The menu lists what's available, you place an order, and the kitchen prepares your food. You don't need to know how the kitchen works. Similarly, an API tells you what data you can request, and the server handles the details.

The requests Library

Python's requests library makes it simple to work with web APIs. Install it first:

pip install requests

Here's a basic example that fetches data from dummyJSON, a free testing API:

import requests

response = requests.get('https://dummyjson.com/posts/1')

if response.status_code == 200:
print("Success!")
data = response.json()
print(data['title'])
else:
print("Request failed")

The get() method sends a request to the URL. The response object contains the status code and data. The json() method converts the response into a Python dictionary.

HTTP Methods

Web APIs use different HTTP methods for different actions:

GET retrieves data:

response = requests.get('https://dummyjson.com/posts')
posts = response.json()

POST sends data to create something:

new_post = {'title': 'My Post', 'body': 'Content here', 'userId': 1}
response = requests.post('https://dummyjson.com/posts/add', json=new_post)

There are also PUT (update), DELETE (remove), and others, but GET and POST handle most common tasks.

Status Codes

The status code tells you what happened:

if response.status_code == 200:
print("Success!")
elif response.status_code == 404:
print("Not found")
else:
print(f"Error: {response.status_code}")

Common codes: 200 means success, 404 means not found, 500 means server error.

For cleaner error handling, use raise_for_status():

try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
except requests.exceptions.HTTPError as err:
print(f"HTTP error: {err}")

Practical Tips

Set timeouts to avoid hanging forever:

response = requests.get(url, timeout=5)  # 5 second timeout

Send custom headers when needed:

headers = {'User-Agent': 'my-python-app'}
response = requests.get(url, headers=headers)

Use sessions for multiple requests to the same service:

with requests.Session() as session:
session.headers.update({'User-Agent': 'my-app'})
response1 = session.get(url1)
response2 = session.get(url2)

Sessions persist settings across requests and can improve performance.

In the next unit, we'll dive deeper into working with JSON data, the format most APIs use to send and receive information.