Skip to main content

API keys

The key methods report whether a key can take requests, read or change its limits and export its usage. All of them except KeyAvailabilityAsync() need a Management Key, passed as the second constructor argument or as IdealPostcodesOptions.ManagementKey.

Key status​

Call KeyAvailabilityAsync(). It needs only the API key and costs nothing, so it suits a health check.

Availability.cscsharp
using IdealPostcodes;

using var client = new IdealPostcodesClient("ak_test");

var key = (await client.KeyAvailabilityAsync("ak_test")).Result;

Console.WriteLine(key.Available);
foreach (var country in key.Contexts)
{
Console.WriteLine(country.Iso3);
}

Available is false when the key has no lookups left, has hit a daily limit or is called from a URL it does not allow. Contexts lists the countries the key can search.

Remaining lookups​

KeyDetailsAsync() returns LookupsRemaining, the daily, monthly and per-IP limits, allowed URLs, notification settings and datasets.

Details.cscsharp
using IdealPostcodes;

using var client = new IdealPostcodesClient("ak_test", Environment.GetEnvironmentVariable("IDPC_MANAGEMENT_KEY"));

var details = (await client.KeyDetailsAsync("ak_test")).Result;

Console.WriteLine($"{details.LookupsRemaining} lookups left, daily limit {details.DailyLimit.Limit?.ToString() ?? "none"}");

Limits​

Call UpdateKeyDetailsAsync() with only the settings to change. A property you leave unset is not sent, so its setting stays as it is. Nested settings are classes of their own, such as UpdateKeyDetailsDailyLimit and UpdateKeyDetailsNotifications.

Update.cscsharp
using IdealPostcodes;

using var client = new IdealPostcodesClient("ak_test", Environment.GetEnvironmentVariable("IDPC_MANAGEMENT_KEY"));

await client.UpdateKeyDetailsAsync("ak_test", new UpdateKeyDetailsBody
{
DailyLimit = new UpdateKeyDetailsDailyLimit { Limit = 1000 },
AllowedUrls = ["https://www.example.com"],
});

A setting the API clears with null, such as a daily limit or a balance threshold, is a Scalar?. Leave it unset to keep the setting; set it to Scalar.Null to send null and clear it. So new UpdateKeyDetailsDailyLimit { Limit = Scalar.Null } removes the daily limit, AllowedUrls = [] removes the URL restriction and Notifications = new UpdateKeyDetailsNotifications { Enabled = false } stops notification emails. See UpdateKeyDetailsAsync() for every field.

Usage export​

KeyUsageAsync() returns totals and a daily breakdown. KeyLogsAsync() returns each billable lookup as a CSV string. Both take Start and End as UNIX timestamps in milliseconds.

Usage.cscsharp
using IdealPostcodes;

using var client = new IdealPostcodesClient("ak_test", Environment.GetEnvironmentVariable("IDPC_MANAGEMENT_KEY"));
var start = DateTimeOffset.UtcNow.AddDays(-7).ToUnixTimeMilliseconds();
var end = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

var usage = (await client.KeyUsageAsync("ak_test", new KeyUsageParams { Start = start, End = end })).Result;
foreach (var day in usage.DailyCount)
{
Console.WriteLine($"{day.Date}: {day.Count}");
}

var csv = await client.KeyLogsAsync("ak_test", new KeyLogsParams { Start = start, End = end });
await File.WriteAllTextAsync("lookups.csv", csv);

Set Licensee on the same parameters to report one licensee's usage.