@varve/ons-api
TypeScript client for the UK Office for National Statistics beta API — timeseries indicators, multidimensional datasets, and custom exports for UK economic and social statistics.
The UK Office for National Statistics publishes the country's most authoritative economic, demographic, and social statistics: inflation, unemployment, GDP, population estimates, trade, earnings, and more. @varve/ons-api wraps the ONS beta REST API (api.beta.ons.gov.uk/v1) into a typed, validated TypeScript client that handles rate limiting transparently and works in Node.js, browsers, and Cloudflare Workers.
Two data access paths
The ONS API exposes its data through two parallel structures. Understanding which to use will determine every method call downstream.
Timeseries (CDIDs)
A CDID (Coded Data IDentifier) is a four-character code that uniquely identifies a single statistical indicator series — similar to a ticker symbol. CDIDs are stable over time and are the canonical way ONS publishes and cross-references indicators.
Each CDID gives you a complete time series: monthly, quarterly, and annual data going back decades, plus metadata including units, next release date, and source dataset. This is the fastest path when you know what you want.
Datasets (multidimensional)
Datasets are structured tables with multiple dimensions — geography, age group, industry, product category — allowing you to query specific slices. For example, the cpih01 dataset contains CPIH broken down by aggregate type and ONS geography code, letting you query England vs Wales vs Scotland separately.
Datasets follow a hierarchy: dataset → edition → version → observations. You navigate this hierarchy to reach actual data points or export files.
Rate limiting
The ONS API throttles aggressively. The client automatically retries on HTTP 429 responses, respecting the Retry-After header returned by the server. All requests also send an appropriate User-Agent header as required by the ONS API terms. The default retry limit is 2; raise it for batch workloads:
Configuration
Error handling
All HTTP errors throw OnsApiError, which includes the HTTP status code, request URL, and raw response body for debugging:
Guides
Track timeseries by CDID
Fetch CPIH, CPI, unemployment, and other indicators by CDID — the fastest path to UK economic data.
Explore topics and datasets
Navigate the ONS topic hierarchy to discover dataset IDs, inspect dimensions, and find the right version to query.
Filter and export
Create custom cuts of multidimensional datasets and export them as CSV or XLS for pipelines and spreadsheets.