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.
The API has the following features:
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.
You can get metadata (information about an item of legislation) through the API.
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:
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.
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.
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.
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.
We do have a Fair Use Policy for the API. To use the API, you must follow the requirements of the policy:
Our API follows a consistent URI scheme.
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.
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.
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.
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).
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.
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:
202 Accepted
: The API will return this code if you make a request to a URI that identifies a resource that is dynamically generated, such as a dynamically generated PDF, to indicate that your request has been accepted but the resource is not yet available. You should repeat the request for the same URI after a short wait, until the API returns a 200 OK
response—we recommend you wait at least 10 seconds between each request to give the API sufficient time to generate the resource.300 Multiple Choices
: The API will return this code if you make a request to a URI that identifies more than one item or section of legislation (e.g. http://www.legislation.gov.uk/id/ukpga/1955/18 identifies both the Army Act 1955 and the Aliens’ Employment Act 1955). The response will contain an HTML list with links to the different options.400 Bad Request
: The API will return this code if you make a request for an item of legislation and specify a year or number that is out of the acceptable range for that type of legislation. The API may also return this code if your request is malformed.403 Forbidden
: The API will return this code if you exceed the rate limit (see the section on our Fair Use Policy above). Your application should slow down or stop making requests until it is under the rate limit again. The API also returns 403 Forbidden
if we have blocked requests from your IP address. If you continue to receive 403 Forbidden
responses to your requests, please contact us.500 Internal Server Error
: The API may return this code if there is a temporary error within the service, or if it cannot process the requested document due to an error in the data. If you get this response more than once for a request to a specific resource, please contact us with details of the resource.503 Service Unavailable
or 504 Gateway Timeout
: The API returns these codes if the legislation.gov.uk service is over capacity or is experiencing a temporary interruption of service. We recommend you wait at least 5 minutes before trying your request again. If you continue to receive this response for a request to a specific resource, please contact us with details of the resource.