# `client.KeyUsage()`

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 with `addresszen.WithManagementKey(...)` when you create the client, which sends it in the `Authorization` header. See [setup](/docs/sdks/go/setup.md).

## Signature

```go
func (c *Client) KeyUsage(ctx context.Context, key string, params *KeyUsageParams) (*APIKeyUsageResponse, error)
```

## Arguments

| Name     | Type              | Required | Description                                                                            |
| -------- | ----------------- | -------- | -------------------------------------------------------------------------------------- |
| `ctx`    | `context.Context` | yes      | Cancels the request and sets its deadline.                                             |
| `key`    | `string`          | yes      | API Key                                                                                |
| `params` | `*KeyUsageParams` | no       | Optional query parameters, or nil. Fields under [Query Parameters](#query-parameters). |

## Query Parameters

A required parameter is a method argument. Set the rest as fields of `params`, named in Studly case (`post_town` is `PostTown`); a field left at its zero value is not sent.

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

## Response

`*addresszen.APIKeyUsageResponse`. Fields the API may leave out or send as null are pointers. `RawJSON()` returns the body as received. See [responses](/docs/sdks/go/responses.md).

## Errors

An HTTP error returns `*addresszen.APIError` with the HTTP status, the API error code and the body; match it with `errors.AsType`. A transport failure returns the `net/http` error, wrapped. See [error handling](/docs/sdks/go/errors.md).

## Example

key-usage/main.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	addresszen "go.addresszen.com/sdk"
)

func main() {
	client := addresszen.NewClient("ak_test", addresszen.WithManagementKey("uk_test"))
	now := time.Now()
	res, err := client.KeyUsage(context.Background(), "ak_test", &addresszen.KeyUsageParams{
		Start: now.AddDate(0, 0, -7),
		End:   now,
	})
	if err != nil {
		log.Fatal(err)
	}
	for _, day := range res.Result.DailyCount {
		fmt.Printf("%s: %d\n", day.Date, day.Count)
	}
	fmt.Println("Total:", res.Result.Total)
}
```
