Programming foundations

Stage 6 · Lesson 4 of 8

Calling a public API with requests

Build and ship your first projects

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

Keep handy

  • Free APIs that give a small project real data to work with

Also worth your time

Mark this stage as not done?