# Audit log

URL: https://lyeve.com/docs/features/audit-log/

> **Requires** a license with the `audit` feature. Streaming the log to an outside sink also needs `audit-pro`, which a license with `audit` does not include. See [pricing](https://lyeve.com/pricing/).

The audit log records the actions admins and the instance take: who acted,
what they changed, when, and from which address. You can search it, export it
as JSON or CSV, send an export to an S3 bucket, and decide how long entries are
kept.

:::tip[Best for]
- **Answering "who changed this, and when".** Filter by actor, action,
  resource or time range.
- **Handing evidence to an auditor.** Export a range as CSV, or upload it to
  an encrypted S3 object.
- **Keeping a copy in your SIEM.** With `audit-pro`, each new entry is
  [streamed](https://lyeve.com/docs/features/audit-log-streaming/) to Splunk, Datadog or your own HTTPS
  endpoint 10 to 30 seconds after it is written.
- **Keeping records exactly as long as a policy says.** Retention policies
  delete or archive by action and resource type, and legal holds keep what a
  case needs.
:::

:::note[Use something else when]
- You want what the instance logged while handling requests, such as errors
  and timings. [Logs](https://lyeve.com/docs/features/logs/) hold those.
- You want the exact request and response of a recent call.
  [Request capture](https://lyeve.com/docs/features/request-capture/) keeps them for 24 hours.
:::

## How it works

- Nothing in the API edits an entry, except a privacy erasure, which
  anonymizes the subject's personal data in their entries. Entries leave the
  log only through a super admin prune, a retention policy or the deletion of
  their tenant.
- Each entry carries a `sequence` and a `chain_hash`, a keyed hash of the
  entry and the hash stored on the entry before it.
- A tenant admin sees only their own tenant, with IP addresses and user agents
  masked. A super admin sees full values and every tenant.
- A super admin's work on a tenant other than their own, such as creating,
  cloning, restoring, moving or deleting it, is filed under no tenant. Only a
  super admin who is not scoped to a tenant sees those entries.
- Actions are named by area and verb, such as `mfa.admin.reset` or
  `ai.settings.update`. Resource types are plain names, such as `user`.

A prune, a retention pass and a tenant delete each leave a signed seal for
every entry they remove, and an erasure seals each entry it anonymizes. The
chain stays verifiable through all four, and a super admin checks it with
[`GET /api/admin/audit-log/verify`](#verify-the-chain).

## In the console

Open **Insight > Audit log**. The **Entries** tab lists the log, newest first,
with the time, action, resource, user and address of each entry. Type an
action, resource type or user id in the search box, or pick an action under
**All actions** and a **From** and **To** date, then choose **Search**. A
super admin also sees **Prune**.

**Export JSON**, and **Export all** for a super admin, sit beside **Prune**. Each
downloads the log as JSON, under the filter in force.

(Image: The Entries tab of the audit log listing recent actions, newest first, each with its time, action, resource and the user who acted.)

The **Retention and holds** tab lists the retention **Policies** and the
**Legal holds**. **New policy** and **New hold** add one, the release button
on a hold removes it, and **Enforce now** runs enforcement at once. The
**Streaming** tab holds [audit log streaming](https://lyeve.com/docs/features/audit-log-streaming/).

(Image: The Retention and holds tab with a policy that keeps every event on user for six months and archives it to default, and a legal hold named Case 2026-114 that protects entries on user.)

Checking the chain, rehashing it, sending an export to S3 and the compliance
export of a hold are done with the API. LyEve Console has no control for them on
this release.

## Turn it on

1. Install a license that includes `audit`. See
   [licensing and tiers](https://lyeve.com/docs/concepts/licensing-and-tiers/).
2. Set `LYEVE_AUDIT_HMAC_KEY` to 64 hex characters. Generate one with
   `openssl rand -hex 32`. A production instance refuses to start without it.
   Outside production, a missing key logs a warning and the chain is not
   protected by a secret.

Keep `LYEVE_AUDIT_HMAC_KEY` stable. Changing it breaks the chain for entries
written before the change, until a [rehash](#verify-the-chain) given the old
key re-signs it. Without the license, the routes are not served and
answer `404`.

## Try it

This needs a license with `audit`. Sign in as an admin. The steps read the
`schema.create` entry that creating the `post` type in the
[quickstart](https://lyeve.com/docs/getting-started/quickstart/) wrote.

Read the newest entries:

<div class="paths">
<div data-path="console">

Open **Insight > Audit log**. The **Entries** tab shows the newest entry on
top, with its time, action, resource, user and address. The count of entries
is under the title.

</div>
<div data-path="api">

You need a token in `TOKEN`. The quickstart shows how to get one.

```bash
curl "http://localhost:3001/api/admin/audit-log?limit=1" \
  -H "Authorization: Bearer $TOKEN"
```

```json
{
  "data": [
    {
      "id": "ae2e0796-abb9-4fe9-a5c7-4d60e0f7966e",
      "sequence": 1,
      "tenant_id": "default",
      "user_id": "caeb8ec2-5c16-4d9d-96dd-a5b1b19f8fd8",
      "action": "schema.create",
      "resource_type": "schema",
      "resource_id": "post",
      "ip": "172.31.184.5:38982",
      "user_agent": "curl/8.18.0",
      "created_at": "2026-10-23T16:53:34.819104Z",
      "chain_hash": "8fabd4295ac7fadc4520f577fbbb0731f6da13f69c3e24a9bcd0777807ac10ee"
    }
  ],
  "total": 1,
  "limit": 1,
  "offset": 0
}
```

`total` holds the number of entries.

</div>
</div>

Narrow it to actions on content types:

<div class="paths">
<div data-path="console">

Type `schema` in the search box and choose **Search**. The list shows the
**schema.create** entry with **schema/post** as its resource. To narrow by one
action instead, pick it under **All actions** and choose **Search**.

</div>
<div data-path="api">

```bash
curl "http://localhost:3001/api/admin/audit-log?resource_type=schema&limit=10" \
  -H "Authorization: Bearer $TOKEN"
```

The answer holds the `schema.create` entry with `"resource_id": "post"`.

</div>
</div>

Download the newest rows as CSV, two at most. The console exports JSON only, so this step
is an API call. If you work in the console, sign in over the API to get a token, as
[Turn on drafts and publishing](https://lyeve.com/docs/getting-started/first-content-type/#5-turn-on-drafts-and-publishing)
shows, and keep it in `TOKEN`:

```bash
curl -X POST http://localhost:3001/api/admin/audit-log/export \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"format": "csv", "limit": 2}'
```

```text
id,user_id,action,resource_type,resource_id,ip,user_agent,ts
ae2e0796-abb9-4fe9-a5c7-4d60e0f7966e,caeb8ec2-5c16-4d9d-96dd-a5b1b19f8fd8,schema.create,schema,post,***.***.***.5:38982,curl/8.18.0,2026-10-23T16:53:34.819104Z
```

On an install where creating `post` is the only entry so far, as here, the file
holds that one row. The address is masked in the export.

List the retention policies. A new install has none:

<div class="paths">
<div data-path="console">

Open the **Retention and holds** tab. **Policies** reads **No retention
policies yet** and **Legal holds** reads **No legal holds yet**.

</div>
<div data-path="api">

```bash
curl http://localhost:3001/api/admin/retention-policies \
  -H "Authorization: Bearer $TOKEN"
```

```json
{ "data": [], "limit": 50, "offset": 0, "total_count": 0 }
```

</div>
</div>

## Search the log

`GET /api/admin/audit-log` needs an admin. An
[admin token](https://lyeve.com/docs/how-to/admin-tokens/) can call it when it holds the
`audit:read` grant.

| Parameter | Meaning | Default |
|---|---|---|
| `actor` | User id of the actor. | all |
| `action` | Exact action name. | all |
| `resource_type` | Exact resource type. | all |
| `resource_id` | Exact resource id. | all |
| `from` | RFC 3339, inclusive. | none |
| `to` | RFC 3339, exclusive. Must be after `from`. | none |
| `tenant_id` | One tenant. Super admin only, ignored for anyone else. | your tenant, or every tenant for a super admin |
| `limit` | Page size, at most 500. | `50` |
| `offset` | Rows to skip. | `0` |

The list writes out pending entries before it reads, so an action you just
took is in the result. If some were still queued, the answer carries
`"pending_writes": true` and `total` is short.

## Export the log

| Request | Who |
|---|---|
| `POST /api/admin/audit-log/export` | An admin. Exports their own tenant. |
| `POST /api/admin/audit-log/export-all` | A super admin. Exports every tenant. |

Both take the same body. Every field is optional except `format`.

| Field | Meaning | Default |
|---|---|---|
| `format` | `json` or `csv`. CSV has a header row. | |
| `from` | RFC 3339, inclusive. | none |
| `to` | RFC 3339, exclusive. Cannot be before `from`. | none |
| `action`, `resource_type` | Exact filters. | all |
| `limit` | Rows. `0` means the default. Values above 1,000,000 are capped. | `100000` |
| `s3` | `{"bucket", "key", "region"}`. Uploads the export instead of returning it. | none |

IP addresses are masked in both exports, and user agents are cut to the
browser or client name and version. A compliance export of a legal hold
(below) keeps full values.

A download starts streaming before every row is read. If a row fails midway,
the file ends early and the status is still `200`. Compare the row count with
what you expect.

### Send an export to S3

Add `"s3": {"bucket": "my-audit", "region": "us-east-1"}` to the body. The
answer is:

```json
{ "rows_written": 5210, "object_key": "audit-exports/acme/2026/10/01/audit-export.csv", "bucket": "my-audit", "elapsed": "1.84s" }
```

- Objects are encrypted at rest with S3 server-side encryption (`AES256`).
- On the tenant export the object key is always under
  `audit-exports/<tenant>/<YYYY/MM/DD>/`, and a `key` you send is ignored. On
  `export-all` the `key` you send is used. It is required and cannot contain
  `..`. The format extension is added when the key lacks it.
- Credentials come from the standard AWS sources: environment variables, the
  shared config file, or the instance role. `region` in the request wins over
  `AWS_REGION`.

## Prune old entries

A prune deletes entries older than a cutoff in the caller's tenant. It needs a
super admin.

<div class="paths">
<div data-path="console">

On **Insight > Audit log**, choose **Prune**. Pick a date under **Delete
entries older than** and choose **Delete**. Pruned entries cannot be recovered
or exported afterwards.

</div>
<div data-path="api">

```bash
curl -X POST http://localhost:3001/api/admin/audit-log/prune \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"older_than": "2025-01-01T00:00:00Z"}'
```

```json
{ "deleted": 3812 }
```

A cutoff later than now answers `400`.

</div>
</div>

A prune skips every entry a legal hold covers. Each prune records itself as an
`audit.prune` entry with the cutoff, the count and the first and last sequence
it removed.

## Verify the chain

`GET /api/admin/audit-log/verify` walks the whole chain, every tenant's
entries and the seals left by deletions and erasures. It needs a super admin.

```bash
curl http://localhost:3001/api/admin/audit-log/verify \
  -H "Authorization: Bearer $TOKEN"
```

```json
{ "valid": true, "entries": 3812, "sealed": 140, "broken": 0, "broken_sequences": [] }
```

`entries` counts the entries read and `sealed` the positions that verified
through a seal. A broken chain still answers `200`, with `valid` false,
`broken` holding the number of broken positions and `broken_sequences` the
first 100 of them. A changed entry breaks every position after it up to the
next seal, so the first broken sequence is where to look.

On an install that pruned, enforced retention, erased a subject or deleted a
user before 0.55.0, verify reports those positions as broken.
`POST /api/admin/audit-log/rehash` accepts every entry as it stands and
re-signs the seals that still verify. A seal that does not verify stays broken
and is listed in `left_broken`. After you change `LYEVE_AUDIT_HMAC_KEY`, send
the old key so the seals signed under it are re-signed under the new one:

```bash
curl -X POST http://localhost:3001/api/admin/audit-log/rehash \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"previous_key": "<the old 64 hex character key>"}'
```

The key serves that one request and is never stored, logged or returned. A
rehash records itself as `audit.chain.rehash`, naming the positions that were
broken before it ran. A body over 1 KiB answers `413`.

## Retention policies and legal holds

A retention policy says how many days entries of an action and resource type
are kept, and whether they are archived to your storage backend before they
are deleted. A legal hold keeps matching entries past their retention window
until the hold is removed.

A license with `audit` includes these routes. If the license lapses while the
instance runs, they answer `402` and nothing is enforced. A license change
applies without a restart. A privacy erasure of a subject that an active hold
names in `filter_resource_id` or `filter_actor` erases nothing and reports the
hold instead, whatever the license says. A hold set in any tenant counts.

Enforcement runs once at start and then every 24 hours. Set
`RETENTION_ENFORCE_INTERVAL` to a duration such as `6h` to change that.

Create a policy that keeps user entries for 180 days and archives them first.
A super admin creates policies and holds:

<div class="paths">
<div data-path="console">

1. On **Insight > Audit log**, open **Retention and holds** and choose
   **New policy**.
2. Leave **Event type** empty, enter `user` in **Resource type** and `180` in
   **Keep for, in days**.
3. Leave **Archive before deleting** on, which the form turns on for you.
   Enter `default` in **Archive to** and `audit-archive/` in **Path prefix**.
4. Choose **Create**. The page says **Policy created**, and the policy reads
   **Every event on user**, kept for **6 months**, then **Archived to
   default**.

</div>
<div data-path="api">

```bash
curl -X POST http://localhost:3001/api/admin/retention-policies \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"event_type": "", "resource_type": "user", "retention_days": 180, "archival_enabled": true, "archival_path_prefix": "audit-archive/"}'
```

The answer is `201` with the policy, and `archival_storage` is `default`.

</div>
</div>

`event_type` is the action. An empty `event_type` or `resource_type` matches
everything. `retention_days` defaults to `365` when you leave it out. An
explicit `0` deletes matching entries on the next run.

Each archive is a CSV object under
`<archival_path_prefix>/<event_type>/<time>/policy-<id>-<run id>.csv`, where
`<time>` reads like `20261020-030000` in UTC. The run id keeps two runs in the
same second from writing one object.

Create a hold:

<div class="paths">
<div data-path="console">

On **Retention and holds**, choose **New hold**. Enter `Case 2026-114` in
**Name** and `user` in **Resource type**, then choose **Create**. The form
also filters on **Action**, **Resource id** and **Tenant**. A date range or an
actor is set with the API.

</div>
<div data-path="api">

```bash
curl -X POST http://localhost:3001/api/admin/legal-holds \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name": "Case 2026-114", "filter_resource_type": "user", "filter_from": "2026-01-01T00:00:00Z"}'
```

</div>
</div>

Holds filter on `filter_action`, `filter_resource_type`, `filter_resource_id`,
`filter_actor`, `filter_from`, `filter_to` and `filter_tenant_id`. `name` is
required. A removed hold keeps protecting its entries for 30 days. A
`filter_resource_id` that is a UUID is stored in lower case and matches the id
written in lower or upper case.

Run enforcement now:

<div class="paths">
<div data-path="console">

On **Retention and holds**, choose **Enforce now**. The dialog asks **Enforce
retention now?** and warns that it is not a dry run. Choose **Delete them**.
The page reports how many policies ran and how many entries were deleted.

</div>
<div data-path="api">

```bash
curl -X POST http://localhost:3001/api/admin/retention/enforce \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

Send `{"cleanup_only": true}` to delete without archiving. The answer lists
each policy with `entries_archived`, `entries_deleted` and `elapsed`.

</div>
</div>

Export what a hold preserves. Only the API does this on this release, with a
super admin token in `TOKEN`. A hold made in the console works the same way.

1. List the holds and copy the hold's `id` into `HOLD_ID`:

   ```bash
   HOLD_ID=$(curl -s http://localhost:3001/api/admin/legal-holds \
     -H "Authorization: Bearer $TOKEN" \
     | jq -r '.data[] | select(.name == "Case 2026-114") | .id')
   ```

2. Export it:

   ```bash
   curl -X POST http://localhost:3001/api/admin/retention/compliance-export \
     -H "Authorization: Bearer $TOKEN" \
     -H "Content-Type: application/json" \
     -d "{\"legal_hold_id\": \"$HOLD_ID\", \"format\": \"csv\"}" -o hold.csv
   ```

The answer is `200` with a CSV file of the entries the hold matches. A hold
that matches no entry yet gives a file with the header line only.

<details>
<summary>Retention and hold routes</summary>

| Method | Path | Who | Purpose |
|---|---|---|---|
| `GET` | `/api/admin/retention-policies` | Admin | List policies. |
| `POST` | `/api/admin/retention-policies` | Super admin | Create a policy. |
| `PUT` | `/api/admin/retention-policies/{id}` | Super admin | Update a policy. |
| `DELETE` | `/api/admin/retention-policies/{id}` | Super admin | Delete a policy. |
| `GET` | `/api/admin/legal-holds` | Admin | List holds. |
| `POST` | `/api/admin/legal-holds` | Super admin | Create a hold. |
| `DELETE` | `/api/admin/legal-holds/{id}` | Super admin | Remove a hold. |
| `POST` | `/api/admin/retention/enforce` | Super admin | Run enforcement now. |
| `POST` | `/api/admin/retention/compliance-export` | Super admin | Export what a hold preserves. |

</details>

## Stream the log to an outside sink

With `audit-pro` on the same license as `audit`, a tenant streams each new
entry to Splunk, Datadog or an HTTPS endpoint of its own, in order and about 10
to 30 seconds after it is written. Streams already set up keep shipping after
`audit-pro` lapses. See [Audit log streaming](https://lyeve.com/docs/features/audit-log-streaming/).

## Record and read from a flow

With `flow-pro`, flows can use two audit nodes:

- `audit.record` writes one entry to the tenant's log. The actor is the flow
  unless you give a user id. A test run writes nothing.
- `audit.query` reads a page of the tenant's log, newest first, with the same
  filters as `GET /api/admin/audit-log` and the same masking.

## Settings

| Variable | What it does | Default |
|---|---|---|
| `LYEVE_AUDIT_HMAC_KEY` | Key for the hash chain, exactly 64 hex characters. | Required in production |
| `RETENTION_ENFORCE_INTERVAL` | How often retention policies are enforced. | `24h` |

If you set `LYEVE_PLUGINS` to choose which features start, include `audit` in
it.

## Routes

| Method | Path | Who | Purpose |
|---|---|---|---|
| `GET` | `/api/admin/audit-log` | Admin | Search the log. |
| `POST` | `/api/admin/audit-log/export` | Admin | Export your tenant. |
| `POST` | `/api/admin/audit-log/export-all` | Super admin | Export every tenant. |
| `POST` | `/api/admin/audit-log/prune` | Super admin | Delete entries before a cutoff. |
| `GET` | `/api/admin/audit-log/verify` | Super admin | Check the hash chain. |
| `POST` | `/api/admin/audit-log/rehash` | Super admin | Re-sign the chain after a deliberate reset. |

## Errors

| Status | Message | Cause |
|---|---|---|
| `400` | `invalid filter parameters` | A list parameter is malformed, or `to` is not after `from`. |
| `400` | `format must be 'json' or 'csv'` | Export or compliance export with another format. |
| `400` | `'to' must be after 'from'` | Export range is reversed. |
| `400` | `limit must be >= 0` | Export with a negative limit. |
| `400` | `older_than must be RFC 3339 (e.g. 2024-01-01T00:00:00Z)` | Prune cutoff is not a timestamp. |
| `400` | `older_than must not be later than now` | Prune cutoff is in the future. |
| `400` | `previous_key must be 64 hexadecimal characters` | Rehash with a malformed old key. |
| `400` | `name is required` | Legal hold without a name. |
| `400` | `legal_hold_id is required` | Compliance export without a hold. |
| `402` | `payment_required` | The license lapsed while the instance ran. |
| `403` | `tenant context required for export` | Tenant export from a token with no tenant. |
| `403` | `insufficient permissions` | A tenant admin called a super admin route, such as prune or `export-all`. |
| `422` | `s3 bucket is not a valid bucket name` | The bucket name breaks S3 naming rules. |
| `503` | `failed to load audit log`, `s3: upload failed` | The database or S3 did not answer. Retry. |

## Related

- [Logs](https://lyeve.com/docs/features/logs/): what the instance logged while serving requests.
- [Admin tokens](https://lyeve.com/docs/how-to/admin-tokens/): an `audit:read` token for a
  script or a SIEM.
- [Analytics](https://lyeve.com/docs/features/analytics/): daily active users, counted from this log.
- [Secure your instance](https://lyeve.com/docs/how-to/secure-your-instance/): the hardening
  steps, including the audit key.
