Using the API

Pagination

Long lists, like every ward in England and Wales, come back a page at a time. Fetching the next page is a single parameter.

How it works

  • Set limit to choose the page size. Most endpoints return 100 records by default and up to 500 at a time.
  • Each response's meta.nextCursor holds a cursor for the next page, or null on the last page.
  • Pass that value back as cursor, with the rest of your query unchanged, to get the next page.

A cursor only works with the query that issued it. Change another parameter and it's refused with invalid_cursor, so start again from the first page instead.

Fetching every page

This loop collects every ward's population for 2022:

const url = new URL(
  "https://api.ukdataatlas.com/v1/data/population?period=2022&geography=ward&boundaryYear=2023&limit=500",
);
const records = [];

while (true) {
  const response = await fetch(url);
  const { data, meta } = await response.json();
  records.push(...data.records);

  if (meta.nextCursor === null) break;
  url.searchParams.set("cursor", meta.nextCursor);
}

Want a whole dataset? Download it in one go instead of paging through it.

Pages of CSV

A CSV or NDJSON page has nowhere to put a cursor, so the next page's URL comes in the Link response header instead, marked rel="next". The last page has no Link header.