Stage 6 · Lesson 4 of 8
Calling a public API with requests
is the library most Python code uses for HTTP, the way programs talk to . requests.get(url, params={"q": "london"}, timeout=10) sends a . Always pass a timeout, because by default it will wait forever.
Check response.status_code, or call response.raise_for_status(), before response.json(). 200 means success, 401 a bad key, 404 a wrong URL and 429 too many requests. Read the API documentation for , and cache responses to a file while developing.
Wrap the call in a function that returns plain data or raises a clear error. Catch requests.exceptions.RequestException for network failures, so the rest of the program never deals with HTTP details.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- GitHub repoFreeProject Based Learning
Community maintained · Beginner
Follow one guided build in your language, then build your own without a guide
- BookFreeAutomate the Boring Stuff with Python
Al Sweigart · Beginner
Free book of practical scripts: files, spreadsheets, web scraping and email
Keep handy
Also worth your time
How to present each project: the problem, stack, challenges and results
- Build Your Own XGo deeper
When you want a harder build: a shell, a key-value store or a tiny Git, from scratch