Working with REST APIs
Module 3
Fetching and integrating data from web APIs for machine learning and data analysis projects
© 2026 WayUp
Here's what happens when you call an API:
https://api.example.com/users| Method | Action | Example |
|---|---|---|
GET | Retrieve data | Get all users |
POST | Create new data | Add a user |
PUT | Update existing data | Update user info |
DELETE | Remove data | Delete a user |
Our focus will be on GET for data retrieval.
We use the requests library in Python:
import requests
# Define the API endpoint URL
url = "https://jsonplaceholder.typicode.com/posts/1"
# Send a GET request
response = requests.get(url)
# The response object contains server's response
print(response.status_code) # e.g., 200
print(response.json()) # The data in JSON format
HTTP Status Codes:
| 200 OK | The request was successful |
| 404 Not Found | Resource could not be found |
| 401 Unauthorized | Authentication required |
| 403 Forbidden | No permission to access |
| 500 Server Error | Problem on the server |
Response Body: The actual data, often in JSON format (key-value pairs).
Goal: Fetch a list of users from a public API
API Endpoint: https://jsonplaceholder.typicode.com/users
requestsGET request to the endpointpip install requests
Part of the URL path to identify a specific resource:
/users/1
Gets user with ID 1
Key-value pairs to filter or sort:
/posts?userId=1
Gets posts by user 1
Passing query parameters with requests:
params = {'userId': 1}
response = requests.get('.../posts', params=params)
print(response.url) # See the final URL
Goal: Fetch comments for a specific post
API Endpoint: https://jsonplaceholder.typicode.com/comments
postId=1Why? To identify the client, for security, and for rate limiting.
Common methods:
Sending a key in the headers:
headers = {'Authorization': 'Bearer YOUR_API_KEY'}
response = requests.get(url, headers=headers)
Goal: Write robust code that handles potential API errors
/posts/99999)status_code to see if it's 200try...except block to catch network errorstry:
response = requests.get(url, timeout=5)
response.raise_for_status() # Raises exception for bad status
except requests.RequestException as e:
print(f"Request failed: {e}")
Goal: Convert API data into an Orange Data Table
from Orange.data import Table, Domain, StringVariable
import requests
# 1. Fetch data from API
users_data = requests.get(
'https://jsonplaceholder.typicode.com/users'
).json()
# 2. Define the domain (the columns)
domain = Domain([
StringVariable('name'),
StringVariable('email'),
StringVariable('city')
])
# 3. Create the data table
data = [[u['name'], u['email'], u['address']['city']]
for u in users_data]
out_data = Table.from_list(domain, data)
API: RestCountries - https://restcountries.com/v3.1/all
Working with REST APIs
Next: Web Scraping Techniques
© 2026 WayUp - way-up.io