# `client.KeyLogs()`

Logs (CSV)

Returns a CSV of the paid lookups made on a key, with the information recorded against each one.

This method requires your Management Key, which can be found on your [accounts page](https://account.ideal-postcodes.co.uk/account).

You can request a maximum interval of 90 days. Without a start or end date, the interval defaults to the last 21 days.

The `Content-Type` returned is CSV (text/csv). For a non-200 response it reverts to JSON, with the error code and message in the body.

## CSV Format

The CSV has no header row. Columns, in order:

1. Timestamp (ISO 8601)
2. IP address the request was received from
3. Search term
4. URL the request originated from
5. Lookup type
6. Tags
7. Lookups consumed
8. Licensee name (sublicensing keys only)
9. Source IP address

The source IP column carries the address forwarded in the `IDPC-Source-IP` header. It is only recorded for keys with IP address forwarding enabled, and only when the header holds a valid IP address. It is empty otherwise.

## Data Redaction

We redact Personally Identifiable Information (PII) in your usage log (including IP, source IP, search term and URL data) weekly.

By default we redact PII older than 28 days. You can change this period from your dashboard.

Set the interval to `0` days to prevent PII collection altogether.

## Endpoint

`GET /keys/{key}/lookups`

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

This operation needs a Management Key. Pass it with `idealpostcodes.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) KeyLogs(ctx context.Context, key string, params *KeyLogsParams) (string, error)
```

## Arguments

| Name     | Type              | Required | Description                                                                            |
| -------- | ----------------- | -------- | -------------------------------------------------------------------------------------- |
| `ctx`    | `context.Context` | yes      | Cancels the request and sets its deadline.                                             |
| `key`    | `string`          | yes      | API Key                                                                                |
| `params` | `*KeyLogsParams`  | 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   |
| `licensee` | `string`    | no       | Licensee Key    |

## Response

The CSV text, as a `string`.

## Errors

An HTTP error returns `*idealpostcodes.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-logs/main.gogo

```go
package main

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

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

func main() {
	client := idealpostcodes.NewClient("ak_test", idealpostcodes.WithManagementKey("uk_test"))
	now := time.Now()
	logs, err := client.KeyLogs(context.Background(), "ak_test", &idealpostcodes.KeyLogsParams{
		Start: now.AddDate(0, 0, -7),
		End:   now,
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(logs)
}
```
