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.
The requests library sends HTTP requests to web APIs and gives back the response. You check the status code, read JSON with .json(), pass query parameters and headers, and set a timeout on every call.

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
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
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
Quick check
Your API call returns status 401. What's the most likely problem?
Go deeper
- 1Fetch data from a free public API and print a few fields.
- 2Always pass timeout= and call raise_for_status().
- 3Keep API keys in environment variables, not in the code.
Also part of these roadmaps
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.