A comprehensive .NET client for the UK Government's Legislation API (legislation.gov.uk). Provides type-safe access to all UK legislation including Acts, Statutory Instruments, and more. Supports search, point-in-time versions, changes tracking, and multiple output formats (XML, Atom feeds).
$ dotnet add package Uk.LegislationA comprehensive .NET client library for the UK Government's Legislation API (legislation.gov.uk).
LegislationTypeInstall via NuGet Package Manager:
Install-Package Uk.Legislation
Or via .NET CLI:
dotnet add package Uk.Legislation
using 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:
LegislationType.UkPublicGeneralAct) - "ukpga"LegislationType.UkLocalAct) - "ukla"LegislationType.UkPrivateAct) - "ukppa"LegislationType.ScottishAct) - "asp"LegislationType.SeneddAct) - "asc"LegislationType.ChurchMeasure) - "ukcm"LegislationType.NorthernIrelandAct) - "nia"LegislationType.UkStatutoryInstrument) - "uksi"LegislationType.ScottishStatutoryInstrument) - "ssi"LegislationType.WalesStatutoryInstrument) - "wsi"LegislationType.NorthernIrelandStatutoryRule) - "nisr"LegislationType.ChurchInstrument) - "ukci"LegislationType.EuRegulation) - "eur"LegislationType.EuDirective) - "eudr"LegislationType.EuDecision) - "eudn"And 22 more types! See LegislationType enum for the complete list.
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
}
/section/1/data.xml) returns 404
See API_ENDPOINTS.md for detailed endpoint documentation.
# 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.
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