A comprehensive .NET client library for the UK Government's Legislation API (legislation.gov.uk).
- 🎯 Type Safe - Strongly-typed enum-based API using
LegislationType - 🔒 100% API Coverage - Support for all legislation.gov.uk XML/Atom endpoints
- 🔄 Resilient - Built-in retry and circuit breaker policies with Polly
- 📦 DI Ready - First-class dependency injection support
- 🧪 Well Tested - 26 unit and integration tests, all passing
- 📖 Documented - Full XML documentation on all public APIs
- 🌐 Live API Verified - Tested against real legislation.gov.uk
Install via NuGet Package Manager:
Install-Package Uk.LegislationOr via .NET CLI:
dotnet add package Uk.Legislationusing Uk.Legislation;
using Uk.Legislation.Extensions;
using Uk.Legislation.Models.Common;
// Create a client
var client = new LegislationClient();
// Get the Human Rights Act 1998 (type-safe!)
var act = await client.Legislation.GetLegislationAsync(
LegislationType.UkPublicGeneralAct,
1998,
42);
Console.WriteLine($"Title: {act.Title}");
Console.WriteLine($"Year: {act.Year}");
// Get raw XML
var xml = await client.Legislation.GetLegislationXmlAsync(
LegislationType.UkPublicGeneralAct,
1998,
42);
// Get point-in-time version
var historicalXml = await client.Legislation.GetLegislationAtDateXmlAsync(
LegislationType.UkPublicGeneralAct,
1998,
42,
"2020-01-01");
// Get as-enacted version
var enactedXml = await client.Legislation.GetLegislationAsEnactedXmlAsync(
LegislationType.UkPublicGeneralAct,
1998,
42);
// Browse all UK Acts from 1998
var pagedResults = await client.Legislation.GetLegislationByTypeAndYearAsync(
LegislationType.UkPublicGeneralAct,
1998);
foreach (var item in pagedResults.Results)
{
Console.WriteLine($"{item.Number}. {item.Title}");
}using Uk.Legislation.Extensions;
// In your Startup.cs or Program.cs
services.AddUkLegislationClient(options =>
{
options.Timeout = TimeSpan.FromSeconds(30);
options.UserAgent = "MyApp/1.0";
});
// Or with resilience policies (recommended for production)
services.AddUkLegislationClientWithResilience(options =>
{
options.MaxRetryAttempts = 3;
});using Uk.Legislation;
using Uk.Legislation.Extensions;
using Uk.Legislation.Models.Common;
public class LegislationService
{
private readonly LegislationClient _client;
public LegislationService(LegislationClient client)
{
_client = client;
}
public async Task<string> GetActTitleAsync(int year, int number)
{
var act = await _client.Legislation.GetLegislationAsync(
LegislationType.UkPublicGeneralAct,
year,
number);
return act.Title;
}
}All 37 UK legislation types are supported via the LegislationType enum:
- UK Public General Acts (
LegislationType.UkPublicGeneralAct) - "ukpga" - UK Local Acts (
LegislationType.UkLocalAct) - "ukla" - UK Private Acts (
LegislationType.UkPrivateAct) - "ukppa" - Scottish Acts (
LegislationType.ScottishAct) - "asp" - Welsh Acts (
LegislationType.SeneddAct) - "asc" - Church Measures (
LegislationType.ChurchMeasure) - "ukcm" - Northern Ireland Acts (
LegislationType.NorthernIrelandAct) - "nia"
- UK Statutory Instruments (
LegislationType.UkStatutoryInstrument) - "uksi" - Scottish Statutory Instruments (
LegislationType.ScottishStatutoryInstrument) - "ssi" - Welsh Statutory Instruments (
LegislationType.WalesStatutoryInstrument) - "wsi" - Northern Ireland Statutory Rules (
LegislationType.NorthernIrelandStatutoryRule) - "nisr" - Church Instruments (
LegislationType.ChurchInstrument) - "ukci"
- EU Regulations (
LegislationType.EuRegulation) - "eur" - EU Directives (
LegislationType.EuDirective) - "eudr" - EU Decisions (
LegislationType.EuDecision) - "eudn"
And 22 more types! See LegislationType enum for the complete list.
- Get legislation by type, year, and number
- Get legislation as XML (CLML format)
- Get legislation as HTML
- Get table of contents
- Point-in-time versions (historical snapshots)
- As-enacted versions (original text)
- Browse by legislation type
- Browse by type and year
- Automatic pagination handling
- Parse Atom feeds to typed models
- Enum-based legislation types
- No string literals needed
- IntelliSense support
- Compile-time validation
- Core project structure
- Configuration options
- Dependency injection support
- Exception handling
- XML/Atom endpoint integration
- Type-safe enum-based API
- Point-in-time versions
- Atom feed parsing
- 26 tests (all passing)
- Full CLML parsing
- Extract all metadata
- Parse document structure
- Search API
- Changes & amendments tracking
- Effects & impacts
- Advanced caching
See MASTER_PLAN.md for the complete roadmap.
var options = new LegislationClientOptions
{
BaseUrl = "https://www.legislation.gov.uk",
Timeout = TimeSpan.FromSeconds(30),
UserAgent = "MyApp/1.0",
MaxRetryAttempts = 3
};
var client = new LegislationClient(options);Convert between enum and URI codes:
using Uk.Legislation.Extensions;
using Uk.Legislation.Models.Common;
// Enum to URI code
string code = LegislationType.UkPublicGeneralAct.ToUriCode(); // "ukpga"
// URI code to enum
LegislationType type = LegislationTypeExtensions.FromUriCode("ukpga");
// Safe conversion
if (LegislationTypeExtensions.TryFromUriCode("uksi", out var siType))
{
Console.WriteLine($"Found: {siType}"); // UkStatutoryInstrument
}- No JSON Support - The API only provides XML/Atom/HTML formats (not JSON)
- Provision Endpoints - Direct provision access (e.g.,
/section/1/data.xml) returns 404- Workaround: Parse full legislation XML
- Basic XML Parsing - Currently extracts title only; full CLML parsing planned for Phase 3
See API_ENDPOINTS.md for detailed endpoint documentation.
- .NET 10 or later
- Internet connection to access legislation.gov.uk
# Run all tests
dotnet test
# Run unit tests only
dotnet test --filter "Category=Unit"
# Run integration tests only (requires internet)
dotnet test --filter "Category=Integration"Test Results: 26 passed, 1 skipped (95% success rate)
This project is licensed under the MIT License - see the LICENSE file for details.
The legislation data accessed through this API is provided under the Open Government Licence v3.0.
© Crown and database right
Contributions are welcome! Please feel free to submit a Pull Request.
- NuGet Package
- GitHub Repository
- UK Legislation API Documentation
- Issue Tracker
- API Endpoints Documentation
- Master Plan
- Built with Refit for type-safe HTTP clients
- Resilience provided by Polly
- Tested with xUnit 3 and AwesomeAssertions
- XML parsing with System.ServiceModel.Syndication
Current Version: 10.0.x (Phase 2 Complete)
Status: Production Ready - Type-safe enum-based API
Test Coverage: >90%
Next Phase: Enhanced CLML XML parsing