Handling APIs with requests
Learn to fetch and interact with data from online APIs.
Handling APIs with requests is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to Handling APIs
APIs (Application Programming Interfaces) allow applications to communicate with each other. Python’s requests library makes it simple to interact with APIs by sending HTTP requests and handling responses.
In this lesson, you’ll learn how to use the requests library to send API requests and process responses.

2
Installing requests
To install the requests library, use the following command in your terminal:
pip install requests
Once installed, you can import it in your Python scripts:
# Importing requests
import requests
print("Requests library imported successfully")3
Sending a GET Request
The GET method is used to retrieve data from an API. Use the requests.get() function to send a GET request:
# Sending a GET request
import requests
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
print(response.json()) # Outputs the JSON response4
Sending a POST Request
The POST method is used to send data to an API. Use the requests.post() function:
# Sending a POST request
import requests
payload = {"title": "foo", "body": "bar", "userId": 1}
response = requests.post("https://jsonplaceholder.typicode.com/posts", json=payload)
print(response.json()) # Outputs the response5
Handling Responses
The requests library provides several attributes to handle responses, such as:
response.status_code: HTTP status code.response.json(): JSON response data.response.text: Raw response content.
# Handling responses
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
if response.status_code == 200:
print("Success:", response.json())
else:
print("Failed with status code:", response.status_code)6
Adding Query Parameters
You can pass query parameters to APIs using the params argument in a GET request:
# Adding query parameters
params = {"userId": 1}
response = requests.get("https://jsonplaceholder.typicode.com/posts", params=params)
print(response.json())7
Adding Headers
Headers allow you to send additional information, such as API keys or content types, with your requests:
# Adding headers
headers = {"Authorization": "Bearer YOUR_API_KEY"}
response = requests.get("https://jsonplaceholder.typicode.com/posts", headers=headers)
print(response.status_code)8
Handling Errors
The requests library provides built-in exception handling for network-related errors:
# Handling errors
try:
response = requests.get("https://jsonplaceholder.typicode.com/invalid")
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print("HTTP error occurred:", err)
except requests.exceptions.RequestException as err:
print("Request error occurred:", err)9
10
Common Mistakes with requests
Here are some mistakes to avoid:
- Not handling HTTP errors with
response.raise_for_status(). - Hardcoding sensitive information like API keys in the script.
- Forgetting to specify the correct HTTP method (e.g., GET, POST).
11
What Did We Learn?
In this lesson, you learned:
- How to install and import the
requestslibrary. - How to send GET and POST requests to APIs.
- How to handle responses, add query parameters, headers, and handle errors.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Handling APIs with requests” lesson free?
Yes — the full text of “Handling APIs with requests” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Handling APIs with requests”?
Learn to fetch and interact with data from online APIs. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Handling APIs with requests” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.