Ingesting Data from APIs

Pull data reliably from REST APIs: authentication, pagination, rate limits, retries, incremental loads and landing raw responses safely.

Intermediate · about 8 hours · difficulty 3/5

Learn first: Working with Files and DataFrames in Python

Many sources (payment gateways, CRMs, ad platforms, government open data) only expose data through HTTP APIs. A good API extractor is boring, restartable and never loses or duplicates records.

The building blocks

  • HTTP basics: methods, status codes (200, 401, 404, 429, 5xx), headers, JSON bodies.
  • Authentication: API keys, bearer tokens, OAuth 2.0 client credentials. Keep secrets in environment variables or a secrets manager.
  • Pagination: page numbers, offsets, or cursors. Cursor-based pagination is safest when data changes while you read.
  • Rate limits: respect 429 Too Many Requests and Retry-After headers.
  • Retries with backoff: retry transient errors (timeouts, 5xx) with exponential backoff and a cap; never retry 4xx validation errors forever.

Incremental extraction

Do not re-download everything daily. Store a watermark such as the max updated_at you have seen, and request only newer records next run. Overlap the window slightly and deduplicate downstream to handle late updates.

Landing the data

Write the raw response (JSON Lines, gzipped) to object storage with the extraction timestamp in the path, for example raw/razorpay/payments/dt=2026-10-24/. Transform later. Keeping raw data lets you reprocess when parsing logic changes.

Pitfalls

  • Using requests.get without a timeout; one hung call can block a pipeline for hours.
  • Overwriting yesterday's file on rerun instead of writing idempotently.
  • Assuming the API schema never changes. Log and alert on unexpected fields.

You are ready to move on when your extractor can be killed halfway, restarted, and still produce exactly one copy of every record.

Key concepts

  • HTTP methods and status codes
  • API keys, tokens and OAuth
  • Pagination styles
  • Rate limits and backoff
  • Incremental loads and watermarks
  • Landing raw JSON
  • Webhooks: Push-based ingestion
  • Ingestion frameworks like dlt

Mini project

Build a Python extractor for a free public API (for example GitHub's REST API for a popular repo's issues). Support pagination, retries with backoff, an incremental watermark stored in a file, and write gzipped JSON Lines partitioned by date.

Check yourself

Your extractor fetches records with updated_at greater than the last watermark. Describe a scenario where a record is missed and how you would prevent it.

Free resources