Working With APIs in Python
Part of the Data Analyst Roadmap
In short
Some data isn't sitting in a file. It lives behind an API: exchange rates, weather, your company's own app. A few lines of Python can ask for it whenever you need fresh numbers.
Using the requests library to pull data directly from a web API — useful when the data you need isn't sitting in a file or database, but has to be fetched live.

Think of it like this
The railway enquiry counter
At the enquiry window you ask in a set format, train number and date, and get back a printed slip in a set format too. Ask wrongly and you get 'invalid'. An API is that counter: a fixed way to ask, a fixed shape of answer.
Watch it happen
Fetching data from an API
GET /rates?base=USD
1/5Your script asks for exchange rates, with the base currency as a query parameter.
The big ideas
What you need to know
requests.get()
The requests library sends the call. Pass params= for query values and timeout= so it never hangs forever.
Check the status
200 is fine. 401 means bad key, 404 wrong address, 429 too many calls. raise_for_status() stops on errors.
Keep keys secret
Read API keys from an environment variable or Colab secrets. Never commit them to GitHub.
Read the docs
Every API documents its endpoints, parameters, limits and pagination. Five minutes there saves an hour.
See it in code
Walk through the code
1import os2import requests3import pandas as pd4 5resp = requests.get(6 "https://api.example.com/v1/orders",7 params={"from": "2026-10-01", "page": 1},8 headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},9 timeout=10,10)11resp.raise_for_status()12data = resp.json()13orders = pd.json_normalize(data["results"])14print(len(orders), "orders fetched")1/4The endpoint and its query parameters. requests builds the ?from=...&page=1 part for you.
A safe pattern for any JSON API: params, a secret key from the environment, a timeout and a status check. The URL is a placeholder.
Save yourself the pain
Common mistakes
Oops
Pasting your API key directly into a notebook you then share.
Do this instead
Store it as an environment variable or Colab secret and read it with os.environ.
Oops
Assuming one call returns everything.
Do this instead
Most APIs paginate. Loop through pages until there's no next page, and sleep briefly between calls.
Your mission
Your first API call
Quick check
Your API call returns status 401. What's the likely cause?
Go deeper
- 1Use the requests library to pull data from a free public API.
- 2Practice parsing a JSON API response into a Pandas DataFrame.
- 3Learn basic API concepts: endpoints, parameters, and authentication keys.
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.