Skip to main content

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 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.

Signature​

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

Arguments​

NameTypeRequiredDescription
ctxcontext.ContextyesCancels the request and sets its deadline.
keystringyesAPI Key
params*KeyUsageParamsnoOptional query parameters, or nil. Fields under 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.

NameTypeRequiredDescription
starttime.TimenoStart Timestamp
endtime.TimenoEnd Timestamp
tagsstringnoTags
licenseestringnoLicensee Key

Response​

*addresszen.APIKeyUsageResponse. Fields the API may leave out or send as null are pointers. RawJSON() returns the body as received. See responses.

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.

Example​

key-usage/main.gogo
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)
}