Programming · Grade 11 · Chapter 10
Working with APIs
Pulling live data from the internet into our own code.
Here's a problem
Data That Changes Every Second
A student wants their program to show today's weather, live stock prices, or the latest news — data that changes constantly and can't just be typed into the code by hand. How can a program get up-to-date information?
Discuss before revealing: What if our code could send a request to a service and get back live, current data?
Today's tool
Requests and Responses
Using the requests library, our Python code can send a request to a web API and receive live data back — usually as JSON — which we can then parse and use.
How it looks
Fetching Live Data
# fetching data from an API
import requests
response = requests.get("https://api.example.com/data")
data = response.json()
print(data)
Let's build it together
Fetch and Parse Real Data
We'll fetch live data from a real, free API and parse the response.
Activity: Live-code a request to a free public API (weather, jokes, trivia, etc). Parse the JSON response and print specific fields. Students try modifying the request.
Quick check
What do you typically get back from an API call?
AStructured data, often in JSON format
BA printed physical document
BNothing at all
DA new programming language
Click to reveal answer
Let's discuss
What live data could power your Innovation Challenge project?
Now that we can fetch real-time data — what kind of live information could make your capstone project more powerful?
Before you go
Today we learned...
APIs let our programs use live, real-world data. Next week: planning our AI Innovation Challenge project!