Collecting Data from APIs
To test whether hot days really sell more cold coffee, Nisha needs daily temperatures for her city. She could copy them from a weather website by hand, one day at a time. Or the analyst can ask a weather service's API, and get a year of clean, structured data in one request.
The rule
An API is a menu for programs: send a request, get structured data back.
An API (application programming interface) is a doorway a service offers to other programs. Instead of a page designed for eyes, it returns data designed for code. Most web APIs follow the same shape:
- Endpoint: the address you call, such as
https://api.example-weather.com/v1/daily. - Parameters: what you are asking for, such as city and date range.
- Authentication: usually an API key, a secret string that identifies you. Keep it out of shared files.
- Response: a status code (200 means success, 401 means not authorised, 429 means too many requests) plus a body, usually JSON.
A request and part of its reply:
GET https://api.example-weather.com/v1/daily?city=Pune&start=2025-06-02&end=2025-06-08{
"city": "Pune",
"units": "celsius",
"days": [
{ "date": "2025-06-02", "max_temp": 31 },
{ "date": "2025-06-03", "max_temp": 29 }
]
}JSON is semi-structured: named fields, nested lists. To analyse it, you flatten it into a table with one row per day, then join it to sales on date.
Services protect themselves with rate limits (a maximum number of requests per minute or day) and pagination (long results split into pages you fetch one by one).
The shortcut
For any API, find four things in its documentation before writing a line: where (endpoint), what (parameters), who (authentication) and how much (rate limit and pagination). Then check the status code before you trust the body.
Where and what
Endpoint
/v1/daily, withcity=Pune,start=2025-06-02andend=2025-06-08.
The trap: reading an error as data. If the body says {"error": "invalid key"} and your script does not check the status, you may save an empty or wrong table without noticing.
Drills
Answer fast — trust the rule.
What is an API key for?
Your request returns status 429. What does it mean?
The reply holds 365 days but you received only the first 100. What is the likely reason?
In the sample JSON, how would you describe the temperature data before flattening?
Remember
- An API is a menu for programs: send a request, get structured data back.
- Shortcut: where, what, who, how much; then check the status code.
- Trap: treating an error reply as data; keep API keys out of shared files.
