Setup
Install IdealPostcodes, then create a client with your API key. The SDK runs on .NET 8 or later, and on .NET Framework 4.6.2 or later through netstandard2.0.
Install
dotnet add package IdealPostcodes
Create a client
Pass your API key to the constructor. Create one client and reuse it: it is safe to share across threads, and it owns and disposes its HttpClient.
using IdealPostcodes;
using var client = new IdealPostcodesClient("ak_test");
Management Key
Key details, usage and logs, licensees and configurations need your Management Key, which begins uk_. Pass it as the second argument. It grants control of your account, so keep it on your server and out of version control.
using var client = new IdealPostcodesClient("ak_test", Environment.GetEnvironmentVariable("IDPC_MANAGEMENT_KEY"));
var details = await client.KeyDetailsAsync("ak_test");
Console.WriteLine(details.Result.LookupsRemaining);
A client without a Management Key throws MissingManagementKeyException before sending a request that needs one.
Client options
For anything beyond the keys, pass an IdealPostcodesOptions.
using var client = new IdealPostcodesClient(new IdealPostcodesOptions
{
ApiKey = "ak_test",
ManagementKey = Environment.GetEnvironmentVariable("IDPC_MANAGEMENT_KEY"),
UserAgent = "my-app/1.0",
});
| Property | Purpose |
|---|---|
ApiKey | Required. The API key, or a licensee key beginning sl_ |
ManagementKey | The Management Key for account operations |
BaseUrl | Defaults to https://api.ideal-postcodes.co.uk/v1 |
UserAgent | Sent ahead of the SDK and .NET versions, for example my-app/1.0 ideal-postcodes-dotnet-sdk/0.1.0 dotnet/10.0.0 |
Your own HttpClient
Pass an HttpClient and the options. The SDK never disposes an HttpClient it did not create, and a User-Agent already on the client is kept ahead of the SDK's.
var http = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
var client = new IdealPostcodesClient(http, new IdealPostcodesOptions { ApiKey = "ak_test" });
Dependency injection
With IHttpClientFactory (Microsoft.Extensions.Http), register the options and a typed client. The factory calls the (HttpClient, IdealPostcodesOptions) constructor.
builder.Services.AddSingleton(new IdealPostcodesOptions
{
ApiKey = builder.Configuration["IdealPostcodes:ApiKey"]!,
});
builder.Services.AddHttpClient<IIdealPostcodesClient, IdealPostcodesClient>(http => http.Timeout = TimeSpan.FromSeconds(5));
Then inject IIdealPostcodesClient. It declares every method on the client, so it is also what to mock in tests.
Request timeout and cancellation
Set Timeout on the HttpClient. The SDK has no timeout of its own. Every method also takes a CancellationToken as its last argument, such as the request's HttpContext.RequestAborted in ASP.NET Core. A timeout throws TaskCanceledException and a cancellation throws OperationCanceledException, as described in error handling.
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
var response = await client.PostcodesAsync("BR8 7RE", cancellationToken: cts.Token);
Credentials
The API key goes in the Authorization header of every address, place, email and phone request. The Management Key goes in the same header of every key, licensee and configuration request, and nowhere else. Public operations such as KeyAvailabilityAsync() send no credentials.
To bill requests to a licensee, create a client with the licensee's sl_ key.