Filter and export datasets
Create custom cuts of ONS datasets and export them as CSV or XLS — including how to poll for completion and handle the async filter lifecycle.
The ONS filter API lets you specify exactly which dimension values you want from a dataset and generates a downloadable file (CSV or XLS) containing only that slice. This is the right approach when you need structured data for a spreadsheet, ETL pipeline, or data warehouse load — rather than individual observations fetched one at a time.
The filter lifecycle
Creating a filter is an asynchronous operation:
Create the filter — specify the dataset version and which dimension options you want. The API returns a filter_id and a filter_output_id.
Submit the filter — if you didn't pass submitted: true on creation, submit separately. This triggers processing.
Poll for completion — call getFilterOutput(filterOutputId) until state === 'completed' (or 'failed').
Download the file — access output.downloads.csv.href or output.downloads.xls.href. The URL is ready to fetch directly.
Filter outputs are not guaranteed to persist indefinitely. Download the file promptly after the output reaches 'completed' state. Do not cache the href URL across sessions.
Step 1: Discover valid dimension option values
Before creating a filter, confirm which option values exist for each dimension. Passing an invalid option code will result in an empty or failed output.
Do the same for every dimension you're filtering on. The getDatasetDimensions call will tell you the dimension names; getDatasetDimensionOptions gives you the valid values for each.
Step 2: Create and submit the filter
Pass the dataset version coordinates and an array of dimension objects, each with a name and options array. Set submitted: true to process immediately.
Two-step flow (create then submit)
If you need to inspect or modify the filter before processing, omit submitted (it defaults to false). The filter is created in a pending state, and you submit it explicitly when ready. This is rarely needed — use submitted: true for most cases.
Step 3: Poll for completion
getFilterOutput returns the current state of the processing job. Poll it with exponential backoff until state is 'completed' or 'failed'.
Step 4: Access the download URL
Once state === 'completed', the downloads field is populated. Both csv and xls may be present; prefer csv for programmatic use.
Complete production example
Filter CPIH data for UK overall and England separately, submit immediately, poll with exponential backoff, and return the CSV download URL.
Alternative: getObservations for smaller queries
For small, synchronous queries where you don't need a file download, getObservations returns data directly without the async filter lifecycle.
Wildcard constraints: Only one dimension may use '*' as a wildcard at a time. All other dimensions must specify exact option values. If you need to cross multiple dimensions freely (e.g. all geographies × all time periods), use the filter API instead.
Use getObservations when:
- You need data immediately without polling
- You're querying one dimension with
'*'and all others fixed - The result set is small enough to handle in-memory
Use the filter API when:
- You need a file for external consumption (Excel, data pipeline ingestion)
- The cross-product of your dimension selections is large
- You need the complete dataset in a structured format