http and apis — making the call1 / 9
get, status, json — the call ai makes 100 times a day
The shape of an API call
Every AI-generated script that talks to a service does roughly this:
import httpx
response = httpx.get("https://api.example.com/users/7")
response.raise_for_status() # crash on 4xx/5xx
data = response.json() # parse the body as JSON
print(data["name"])
Four lines. AI writes them on autopilot. Your job is to read them and catch the two places it breaks: forgetting the status check, and indexing into the JSON with the wrong key.
A note on this chapter: the browser can't make real network calls. So instead of hitting a real URL, we'll work with hardcoded JSON shapes that match what an API would actually return. The pattern is the same; we just skip the wire.
Run the editor. We'll start by reading from a fake response.
⌘↵ runs the editor.
Booting Python…
Output
[promptdojo:~]$ _