# `client.key_logs`

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 as `management_key:` to `IdealPostcodes::Client.new`, which sends it in the `Authorization` header. See [setup](/docs/sdks/ruby/setup.md).

## Signature

```ruby
def key_logs(key, start: nil, end: nil, licensee: nil)
```

## Arguments

| Name       | Type                | Required | Description                                                                                                         |
| ---------- | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
| `key`      | `String`            | yes      | API Key                                                                                                             |
| `keywords` | `keyword arguments` | no       | One per [query parameter](#query-parameters), named as the API names them. A keyword left out or `nil` is not sent. |

## Query Parameters

Pass these as keyword arguments.

| Name       | Type      | Required | Description     |
| ---------- | --------- | -------- | --------------- |
| `start`    | `Integer` | no       | Start Timestamp |
| `end`      | `Integer` | no       | End Timestamp   |
| `licensee` | `String`  | no       | Licensee Key    |

## Response

The CSV text, as a `String`.

## Errors

An HTTP error raises `IdealPostcodes::ApiError` with the HTTP `status`, the API error `code` and the decoded `body`. A transport failure raises `IdealPostcodes::TransportError`. See [error handling](/docs/sdks/ruby/errors.md).

## Example

key-logs.rbruby

```ruby
require "ideal_postcodes"

client = IdealPostcodes::Client.new(api_key: "ak_test", management_key: "uk_test")

now = (Time.now.to_f * 1000).to_i
week = 7 * 24 * 60 * 60 * 1000

puts client.key_logs("ak_test", start: now - week, end: now)
```
