# 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 `KeyAvailability()` need a [Management Key](/docs/sdks/go/setup.md#management-key), passed with `WithManagementKey()`.

## Key status

Call `KeyAvailability()`. It needs no credentials and costs nothing, so it suits a health check.

availability.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	res, err := client.KeyAvailability(context.Background(), "ak_test")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("available:", res.Result.Available)
	for _, country := range res.Result.Contexts {
		fmt.Println(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

`KeyDetails()` returns `LookupsRemaining`, the daily, monthly and per-IP limits, allowed URLs, notification settings and datasets. A limit that is not set is nil.

details.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test",
		idealpostcodes.WithManagementKey(os.Getenv("IDPC_MANAGEMENT_KEY")))
	res, err := client.KeyDetails(context.Background(), "ak_test")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Result.LookupsRemaining, "lookups left")
	if limit := res.Result.DailyLimit.Limit; limit != nil {
		fmt.Println("daily limit", *limit)
	}
}
```

## Limits

Call `UpdateKeyDetails()` with only the settings to change. Fields left nil are not sent, and the rest stay as they are.

update.gogo

```go
package main

import (
	"context"
	"log"
	"os"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test",
		idealpostcodes.WithManagementKey(os.Getenv("IDPC_MANAGEMENT_KEY")))
	_, err := client.UpdateKeyDetails(context.Background(), "ak_test", idealpostcodes.UpdateKeyDetailsBody{
		DailyLimit:  map[string]any{"limit": 1000},
		AllowedURLs: new([]string{"https://www.example.com"}),
	})
	if err != nil {
		log.Fatal(err)
	}
}
```

The nested settings (`DailyLimit`, `MonthlyLimit`, `IndividualLimit`, `Notifications`, `Datasets`) are maps keyed by the API's own field names, and a nil value in one is sent as JSON `null`. So `map[string]any{"limit": nil}` removes the daily limit, `new([]string{})` removes the URL restriction and `map[string]any{"enabled": false}` in `Notifications` stops notification emails. See [`UpdateKeyDetails()`](/docs/sdks/go/endpoints/update-key-details.md) for every field.

## Usage export

`KeyUsage()` returns totals and a daily breakdown. `KeyLogs()` returns each billable lookup as CSV text. Both take `Start` and `End` as `time.Time`.

usage.gogo

```go
package main

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

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	ctx := context.Background()
	client := idealpostcodes.NewClient("ak_test",
		idealpostcodes.WithManagementKey(os.Getenv("IDPC_MANAGEMENT_KEY")))
	now := time.Now()
	start, end := now.AddDate(0, 0, -7), now

	usage, err := client.KeyUsage(ctx, "ak_test", &idealpostcodes.KeyUsageParams{Start: start, End: end})
	if err != nil {
		log.Fatal(err)
	}
	for _, day := range usage.Result.DailyCount {
		fmt.Printf("%s: %d\n", day.Date, day.Count)
	}

	logs, err := client.KeyLogs(ctx, "ak_test", &idealpostcodes.KeyLogsParams{Start: start, End: end})
	if err != nil {
		log.Fatal(err)
	}
	if err := os.WriteFile("lookups.csv", []byte(logs), 0o644); err != nil {
		log.Fatal(err)
	}
}
```

Set `Licensee` in the same params to report one [licensee's](/docs/sdks/go/licensees.md) usage.
