Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Collection  ›  Lesson

Collecting Data from APIs

APIs 12 minAn API lets one program request data from another with a structured request, and it answers in a predictable format such as JSON.

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.

A week of temperaturesStep 1 of 4
  1. Where and what

    Endpoint /v1/daily, with city=Pune, start=2025-06-02 and end=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.

quick drill

What is an API key for?

quick drill

Your request returns status 429. What does it mean?

quick drill

The reply holds 365 days but you received only the first 100. What is the likely reason?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFetch the weather

You want daily temperature for the café's city to compare with sales. Describe the request you would make to a weather API, what the JSON reply might look like, and two things to handle before trusting the data.