This repository provides a SDK for integrating with the NHS Digital API Platform, including:
-
Care Identity Service (CIS2) Client
- Authentication (Authorization Code Flow)
- Automatic refresh-token renewal
-
Personal Demographics Service (PDS) Client
(Future extensibility for additional NHS Digital APIs is planned.)
This solution is intentionally split into two focused packages:
NHSDigital.ApiPlatform.Sdk(Core)NHSDigital.ApiPlatform.Sdk.AspNetCore(Web Integration)
The SDK was deliberately designed to be host-agnostic.
The Core package contains:
- NHS API integration logic
- Authentication and token lifecycle management
- API client implementations
- No dependency on ASP.NET Core
It does not reference:
HttpContextIHttpContextAccessorISession- ASP.NET Core middleware
- Web-specific abstractions
This design ensures the Core SDK can be used in:
- Console applications
- Background services
- Azure Functions
- Worker services
- Integration pipelines
- Custom web frameworks
- ASP.NET Core (via the integration package)
NHSDigital.ApiPlatform.Sdk.AspNetCore provides:
- Session-based implementations of:
IApiPlatformStateBrokerIApiPlatformTokenBroker
IServiceCollectionextension methods- ASP.NET Core-specific wiring
- Access to
HttpContextand session safely
This package depends on ASP.NET Core abstractions, but the Core SDK does not.
If the Core SDK directly referenced ASP.NET Core types such as:
IHttpContextAccessorHttpContextISession
then:
- Console applications could not use it.
- Worker services would carry unnecessary web dependencies.
- Unit testing would become more complex.
- The SDK would violate separation-of-concerns principles.
By splitting responsibilities:
| Concern | Package |
|---|---|
| Authentication logic | Core |
| Token refresh logic | Core |
| HTTP calls to NHS APIs | Core |
| Web session integration | AspNetCore |
This keeps the architecture:
- Clean
- Modular
- Testable
- Replaceable
- Environment-agnostic
Host-agnostic .NET SDK that:
This package can be used in:
- Console applications
- Background services
- Azure Functions
- Custom web frameworks
- ASP.NET (with your own storage implementation)
👉 Full documentation:
NHSDigital.ApiPlatform.Sdk README
ASP.NET Core adapter for the core SDK.
Provides:
- Session-based storage implementation
- Cookie-based storage implementation (BFF-style)
- ASP.NET DI registration helpers
- Minimal setup for web applications
This is the recommended package for:
- ASP.NET Core MVC
- Web APIs
- BFF architectures
👉 Full documentation:
NHSDigital.ApiPlatform.Sdk.AspNetCore README
Internally, the SDK follows The Standard layering:
- Brokers
- Foundations
- Validations
- Orchestrations (workflow + auto-refresh logic)
- Exposers (
IApiPlatformClientsurface)
This ensures:
- Predictable structure
- High testability
- Clean separation of concerns
- Future extensibility for additional NHS APIs
The SDK automatically:
- Detects expired (or near-expiry) access tokens
- Uses the refresh token
- Requests new tokens from CIS2
- Stores updated tokens
- Continues execution seamlessly
No additional developer logic is required.
The solution includes:
- Unit test projects
- Acceptance test scaffolding
- Integration test scaffolding
- Refresh-token renewal tests
-
Install the ASP.NET Core integration package:
dotnet add package NHSDigital.ApiPlatform.Sdk.AspNetCore(This package automatically installs the Core SDK as a dependency.)
-
Configure
ApiPlatforminappsettings.json. -
Register the SDK:
services.AddApiPlatformSdkCore(config); services.AddApiPlatformSdkAspNetCore();
-
Inject and use
IApiPlatformClientvia DI.Example: initiating CIS2 login from a controller:
using Microsoft.AspNetCore.Mvc; using NHSDigital.ApiPlatform.Sdk.Clients.ApiPlatforms; public class AuthController : Controller { private readonly IApiPlatformClient apiPlatformClient; public AuthController(IApiPlatformClient apiPlatformClient) => this.apiPlatformClient = apiPlatformClient; [HttpGet("login")] public async Task<IActionResult> Login(CancellationToken cancellationToken) { string loginUrl = await this.apiPlatformClient .CareIdentityServiceClient .BuildLoginUrlAsync(cancellationToken); return Redirect(loginUrl); } }
👉 Full documentation:
NHSDigital.ApiPlatform.Sdk.AspNetCore README
Install the Core package directly:
`dotnet add package NHSDigital.ApiPlatform.Sdk`
You must implement:
IApiPlatformStateBrokerIApiPlatformTokenBroker
Then register:
`services.AddApiPlatformSdkCore(config);`
👉 Full documentation:
NHSDigital.ApiPlatform.Sdk README
© North East London ICB
