Stop 01 · Programming & CS FoundationsTopics · 11/12

Working with APIs (requests)

Part of the AI Engineer Roadmap

In short

Weather data, stock prices, and LLMs like GPT or Claude are all reached through APIs. A few lines of Python with requests is how your code talks to them.

Using Python's `requests` library to call external APIs — how you'll pull data, call AI model APIs, and integrate with other services.

The purple blob waits at a table as a waiter brings a covered plate from the kitchen.

Think of it like this

The waiter at a restaurant

You don't walk into the kitchen. You tell the waiter 'one masala dosa, less oil', and he brings it back on a plate. If the dosa is finished, he tells you that instead. An API is the waiter: you send a clear request, it returns a response or an error.

Watch it happen

Getting weather data

Your script
Weather API

GET /forecast?city=Pune

1/5requests.get sends an HTTP GET with your query parameters attached to the URL.

The big ideas

What you need to know

URL + method + params

GET reads data, POST sends data. Query parameters like ?city=Pune go after the URL. requests builds that for you with params=.

Status codes tell the story

2xx is success, 4xx means you asked wrongly (401 bad key, 404 not found, 429 too many), 5xx means the server failed.

Keys go in headers, not code

Read API keys from an environment variable and send them in a header. Never hard-code them in a notebook you push.

Always set a timeout

Without timeout=, a stuck server can freeze your script forever.

See it in code

Walk through the code

python
1import os2import requests3 4API_KEY = os.environ["WEATHER_API_KEY"]5 6resp = requests.get(7    "https://api.example.com/v1/forecast",8    params={"city": "Pune", "days": 3},9    headers={"Authorization": f"Bearer {API_KEY}"},10    timeout=10,11)12resp.raise_for_status()          # error if 4xx or 5xx13 14data = resp.json()15for day in data["days"]:16    print(day["date"], day["max_temp"])

1/4The key comes from an environment variable, so it never sits in your code.

api.example.com is a placeholder. Swap in a real API's URL and field names from its docs. The pattern stays the same for almost every JSON API.

Save yourself the pain

Common mistakes

Oops

Calling r.json() without checking the status, then getting a confusing KeyError.

Do this instead

Call raise_for_status() first, or check r.status_code, so errors show up where they happen.

Oops

Hitting an API in a tight loop of thousands of calls.

Do this instead

Read the rate limit in the docs, add time.sleep between calls, and cache results you already have.

Your mission

Call a free public API

~20 min

Quick check

Your API call returns status 401. What's the most likely problem?

Go deeper

  1. 1Make GET and POST requests to a public API and parse the JSON response.
  2. 2Learn to handle API keys, headers and authentication safely.
  3. 3Practice handling errors, timeouts and rate limits gracefully.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge