Legislation.gov.uk data reuse documentation

Legislation API

What is the Legislation API?

The legislation.gov.uk website is also an API (short for “application programming interface”), which means it is designed for software applications to use as well as human readers. Applications can use the API to search for and download legislation and information about it.

The API serves the pages you can view on legislation.gov.uk. This means that (almost) every page or resource on the website is also available via the API, often in multiple formats, and the content available via both routes is updated at the same time.

Features of the API

The API has the following features:

Access to legislation document content

You can get the content of a legislation document (or part of it) through the API.

For example, you can get the content of the Pension Schemes Act 2021 through the following URLs:

The CLML Reference Guide has guidance on how to interpret CLML.

Some documents are only available as PDF. For example, the Metropolis Water Act 1902 is (as of writing) only available as a PDF at https://www.legislation.gov.uk/ukpga/Edw7/2/41/pdfs/ukpga_19020041_en.pdf.

Access to legislation metadata

You can get metadata (information about an item of legislation) through the API.

Embedded document metadata

The <ukm:Metadata> element in CLML (see the section on namespaces in our introduction to XML for an explanation of what ukm: means) contains information about the item of legislation and the version being viewed, including but not limited to:

The CLML Reference Guide has guidance on how to interpret metadata in CLML.

If you only want the metadata for a document, the /resources sub-resource contains the metadata for the specified version of an item. For example:

Legislation Linked Data via the SPARQL endpoint

We now have a SPARQL endpoint that allows for querying of core legislation data, including a near-complete list of all UK law, as well as of EU origin law up to the end of the implementation period. For more information, see the SPARQL section of this documentation.

Changes to legislation

The API provides lists of changes made to legislation, also known as “effects”. You can request a list of changes to legislation by the type, year and number of the affected and/or affecting document, including information on:

To get a list of effects, see the changes to legislation sub-section of the “Search, lists and feeds” section.

For more information on how to interpret effects, see the Effects section of this documentation.

Search, lists and feeds

You can search for legislation through the API using any of the features available on the search interface on the website. You can also retrieve the same lists of legislation through the API as through the website.

The API returns the search results as an Atom feed. The list is paginated, which means that the API only returns a fixed number of items in each response (the default is 20, which you can change by adding results-count=N to the query string). Where there are more pages left in the feed, the response will contain a <link rel="next"> element with a link to the next results page.

By default the API returns items in order of their last modification date (the date on which an enacted or revised version was most recently published), most recent first.

For example:

For more information, see the section on search, lists and feeds.

How can I use the API?

You can connect to our API using any HTTP client (such as a web browser, or an HTTP request library for your programming language). This page contains addresses for some example resources available through the API.

We encourage users to use our API in their own applications, including crawling and bulk access, and whether commercial or non-commercial. The data in the API are available under open licences. For more information on licensing, see the licence section of this guide.

Fair Use Policy

We do have a Fair Use Policy for the API. To use the API, you must follow the requirements of the policy:

URI scheme

Our API follows a consistent URI scheme.

CORS

The API has CORS (cross-origin resource sharing) enabled, which means you can make requests to the API from client-side Javascript applications running in a web browser.

Note that only endpoints where the path ends in /data.[ext] (e.g. /data.xml or /data.feed) have CORS enabled. Endpoints ending in /data.htm (which correspond to web pages as returned by the website) currently do not have CORS enabled.

Caching

We cache the output of the API for performance reasons, so sometimes there may be a delay between an update to our data and its appearance on the website or API.

Representations and content negotiation

Formats

The API offers multiple representations of some types of content:

This means that any legislation available as HTML is also available as XML and in other formats, and any search results or list page is also available as an Atom feed in XML.

For more information on the formats the API currently provides, see the Formats section of this guide.

We recommend you request responses in XML format as this is normally easier to parse. You can request XML for legislation (as CLML) by appending /data.xml to the end of the URI, and XML for search results and lists (as Atom) by appending /data.feed to the end of the URI.

If you are not familiar with XML, we strongly recommend you read our introduction to XML to learn about its main features.

Content negotiation

To request a different representation of a resource, you can specify a subresource at the end of the URI path. For example, appending /data.xml to the end of the URI for a version of legislation requests that version as CLML (e.g. https://www.legislation.gov.uk/anaw/2013/1/data.xml requests the CLML version of 2013 anaw 1).

Supported HTTP methods

The API currently only supports the GET HTTP method, which is the default for most applications that use HTTP, except for the SPARQL endpoint at https://www.legislation.gov.uk/sparql, which also supports the POST HTTP method in accordance with the support for POST in the SPARQL protocol.

Response codes

The API uses HTTP response codes as specified in RFC 9110 (HTTP Semantics).

Most requests to the API should return 200 OK. Some other common response codes the API returns are: