Prompt
How do I export large datasets from an API in bulk?
Latest observation
Exporting large datasets from an API in bulk usually works best when you avoid pulling everything through a normal synchronous request. The right approach depends on the API, but these patterns are the most common and scalable:
1) Prefer a bulk export endpoint if available
Many APIs offer a dedicated endpoint like:
GET /exportsPOST /exportGET /reports/{id}/download
These often work like this:
- You request an export job.
- The API processes it asynchronously.
- You poll for completion or receive a webhook.
- You download a file from a URL or object storage location.
This is the best option for large datasets because it’s designed for scale.
2) Use pagination for incremental retrieval
If there’s no bulk export endpoint, use pagination:
- Offset-based pagination:
?limit=1000&offset=0 - Cursor-based pagination:
?limit=1000&cursor=abc123
For large datasets, cursor-based pagination is usually better because it handles inserts/deletes more reliably and is more efficient.
Good practices:
- Request the largest page size the API allows.
- Store the last seen cursor or ID so you can resume.
- Write each page to disk or a database as you go instead of keeping everything in memory.
3) Stream data instead of loading it all at once
If the API supports streaming responses or chunked transfer, use that to process records incrementally.
Examples:
- NDJSON (
application/x-ndjson) - CSV download streams
- Gzipped responses
This reduces memory usage and speeds up ingestion.
4) Use filters to split the export into chunks
For very large datasets, split by:
- date range
- ID range
- customer/account
- region/status/type
Example:
- Export January separately from February
- Export records
id 1–1,000,000, then1,000,001–2,000,000
This helps avoid timeouts and makes retries easier.
5) Handle rate limits and retries
Bulk exports can trigger throttling. Make sure your client:
- respects
Retry-After - uses exponential backoff
- retries transient failures
- can resume from the last successful page/chunk
Also consider concurrency carefully. More parallel requests isn’t always better if the API rate-limits aggressively.
6) Use async jobs for expensive exports
If you control the API, the best pattern is often:
POST /exports→ returnsjob_idGET /exports/{job_id}→ statusGET /exports/{job_id}/download→ file URL
This avoids request timeouts and lets the server generate files in the background.
7) Store output in a durable format
Common bulk export formats:
- CSV: simple, widely supported
- JSONL/NDJSON: good for record-by-record processing
- Parquet: best for analytics/large-scale processing
- Avro: good for schema evolution
If the dataset is very large, Parquet is often the best choice for downstream analysis.
8) Validate completeness
Large exports can fail partially, so verify:
- total record count
- checksums/hashes if available
- date ranges covered
- no missing cursors/pages
Keep logs of:
- request IDs
- page/cursor values
- retry attempts
- export job IDs
9) Example workflow
A robust bulk export pipeline might look like:
- Start export job or begin paginated pull.
- Save progress after each chunk.
- Write data to compressed files or object storage.
- Retry failed chunks only.
- Verify row counts and integrity.
- Load into warehouse or analytics system.
10) Example pseudocode for paginated export
cursor = None
with open("export.jsonl", "a") as f:
while True:
params = {"limit": 1000}
if cursor:
params["cursor"] = cursor
resp = api.get("/records", params=params)
data = resp.json()
for item in data["results"]:
f.write(json.dumps(item) + "\n")
cursor = data.get("next_cursor")
if not cursor:
break
If you’re designing the API
Best practices for bulk export APIs:
- asynchronous export jobs
- cursor-based pagination
- streaming downloads
- compression support (
gzip) - filters for incremental exports
- resumable downloads
- clear rate-limit headers
- webhook notification on completion
If you want, I can also show:
- a Python script for exporting from a paginated API,
- a Node.js example,
- or a recommended API design for bulk exports.