# `client.KeyUsageAsync()`

Usage Stats

Reports the number of lookups a key consumed over a date range, as a total and a daily breakdown.

The range defaults to the last 21 days. `start` and `end` take UNIX timestamps in milliseconds, and `end` defaults to the current time. The maximum range is 90 days.

Query at most three tags at once.

## Endpoint

`GET /keys/{key}/usage`

See the [API reference](/docs/api/key-usage.md) for this endpoint.

This operation needs a Management Key. Pass it as the second argument to `new IdealPostcodesClient(apiKey, managementKey)`, or set `ManagementKey` in the options; the client sends it in the `Authorization` header. See [setup](/docs/sdks/dotnet/setup.md).

## Signature

```csharp
Task<ApiKeyUsageResponse> KeyUsageAsync(string key, KeyUsageParams? query = null, CancellationToken cancellationToken = default)
```

## Arguments

| Name                | Type                | Required | Description                                                           |
| ------------------- | ------------------- | -------- | --------------------------------------------------------------------- |
| `key`               | `string`            | yes      | API Key                                                               |
| `query`             | `KeyUsageParams`    | no       | Query parameters. Fields under [Query Parameters](#query-parameters). |
| `cancellationToken` | `CancellationToken` | no       | Cancels the request.                                                  |

## Query Parameters

Set these on the `query` argument. Each property is the name in PascalCase, such as `PostTown` for `post_town`; required fields are constructor arguments.

| Name       | Type     | Required | Description     |
| ---------- | -------- | -------- | --------------- |
| `start`    | `long`   | no       | Start Timestamp |
| `end`      | `long`   | no       | End Timestamp   |
| `tags`     | `string` | no       | Tags            |
| `licensee` | `string` | no       | Licensee Key    |

## Response

`ApiKeyUsageResponse`. Fields are properties in PascalCase, such as `response.Result`; fields this SDK version does not know are kept in `AdditionalProperties`. See [responses](/docs/sdks/dotnet/responses.md).

## Errors

An HTTP error throws `ApiException` with the `StatusCode`, the API error `Code` and the raw `Body`. A success response whose body cannot be read throws `UnserializableResponseException`. Network failures and cancellation throw `HttpClient`'s own `HttpRequestException` and `OperationCanceledException`. See [error handling](/docs/sdks/dotnet/errors.md).

## Example

key-usage.cscsharp

```csharp
using IdealPostcodes;

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

var response = await client.KeyUsageAsync("ak_test", new KeyUsageParams
{
    Start = DateTimeOffset.UtcNow.AddDays(-7).ToUnixTimeMilliseconds(),
    End = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
});

foreach (var day in response.Result.DailyCount)
{
    Console.WriteLine($"{day.Date}: {day.Count}");
}

Console.WriteLine($"Total: {response.Result.Total}");
```
