# Read a user's or agent's trail

> Every sign-in, token, exchange and decision that involved one user or one agent, newest first, read in one call, by the user themselves or by anyone holding read access to the realm.

You read the trail one principal at a time, so two questions have a direct answer: *what did this agent do?* and
*what was done in this user's name?* One call returns everything that involved them, newest first, a page at a time.
Users can read their own trail; anyone else needs read access to the realm.

This is **limited** today: an agent's trail comes back without the changes to its record (read those from its
[change history](/audit-reports/audit-trail/change-history/)), and on a realm served from more than one site, each
read shows one site's records.

## Before you begin

- **Privilege:** a user reads their own trail with no privilege. Reading anyone else's needs `READ` on the realm,
  which `RealmViewer` and `RealmOwner` both give. To let an auditor read without letting them change anything, see
  [Give an auditor read-only access](#give-an-auditor-read-only-access).
- **Where:** the API.
- **What you need first:** the principal's `id`, the id the realm gave it when it was made, not the name you address
  it by. Read it from the object, for example `https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya`.

## How it works

Name exactly one principal by its id: `person` for a user, `agent` for an agent. The answer has two parts:

```json
{
  "principal": "<agent's id>",
  "kind": "agent",
  "events": { "data": [ … ], "page": 1, "pageSize": 100, "totalCount": 214, "totalPages": 3 },
  "changes": { "data": [], "page": 1, "pageSize": 100, "totalCount": 0, "totalPages": 0 }
}
```

- `events` are the records about the principal, and those in which it acted: see
  [What is recorded](/audit-reports/audit-trail/what-is-recorded/), and every field in
  [Record fields and examples](/audit-reports/audit-trail/record-fields/).
- `changes` is meant for the changes made to an agent's own record, such as a quarantine. It comes back empty today,
  for users and agents alike; read an object's changes in its
  [Change history](/audit-reports/audit-trail/change-history/).

Records are **newest first by `eventTime`**. A record appears within moments of
the work it records, so a read made just after a sign-in may not show it yet; read again.

## Set it up

There's nothing to set up. Read the agent `invoice-agent`'s trail, 100 records to a page
([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`):

```sh
curl "https://foobar.cvyuh.com/api/v0/am/audit/v1/trail?agent=<invoice-agent's id>&page=1&pageSize=100" \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

For a user, send `person=<user's id>` in place of `agent`.

### Your own trail

A signed-in user reads their own trail with no id to know:

```sh
curl https://foobar.cvyuh.com/api/v0/am/audit/v1/trail/me \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

It answers as `?person=` with their own id does: what they did, and what apps and agents did in their name.

### Give an auditor read-only access

Put the auditors in a group, then grant the group `RealmViewer`, which reads every record and setting in the realm,
and the realms beneath it, and changes nothing. Here the group is `auditors`:

```sh
curl https://foobar.cvyuh.com/api/v0/am/privilege/v1 \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "group": "auditors", "privilege": "RealmViewer" }'
```

Granting needs `DELEGATE` on the realm, which `RealmOwner` holds. Always send `privilege`: left out, the grant is
`RealmOwner`. See [Delegated administration](/platform/realms/delegated-administration/).

### Read every site's records

On a realm served from more than one site, each site keeps the records of the work it did, and a read answers from the
site that serves it. To read what each site recorded, make the same call on the host name that reaches each site (see
[Host names](/platform/realms/hostnames/)), and merge the answers by `eventTime`. A host name shared by every site
answers from whichever site serves you.

## What happens when it fails

| Answer | Why | What to do |
|---|---|---|
| `400` | Neither `agent` nor `person` named, or both | Name exactly one |
| `400` | `agent` isn't an id | Send the agent's `id`, not its name |
| `400` on `trail/me` | The session didn't sign in a user of this realm, such as an agent's or a client's | Read with `?person=` or `?agent=` and the privilege |
| `401` | No session, or it has ended | Sign in again |
| `403` | Someone else's trail, without `READ` on the realm | Ask for `RealmViewer`, or read your own trail |

## Limits and defaults

| What | Default | Range |
|---|---|---|
| `pageSize` | 20 | 1 to 100 |
| `page` | 1 | From 1 |
| Order | Newest first by `eventTime` | Fixed |
| Principals per read | One | |
| Sites per read | The site that serves the call | |

`events` and `changes` are paged separately, each with its own `totalCount` and `totalPages`. A realm set to cursor
paging answers `nextCursor` and `hasMore` instead; send `cursor` back for the next page.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The trail is empty for a user you know signed in | You named the user by name, not by `id` | Read the user and send their `id` |
| A sign-in just made isn't there | Records appear within moments | Read again |
| A user's lockout isn't there | Lockout records follow the name typed, in lower case, not the user's id | Read `?person=maya`, the name in lower case: see [Look into a locked account](/audit-reports/guides/look-into-a-locked-account/) |
| `changes` is empty for an agent | A known gap | Read `https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/history` |
| A refused token request isn't on the user's trail | No user was known yet, so it is under the client that asked | Read `?person=<the client's id>` |
| Records you expect from another site are missing | Each site keeps its own | [Read every site's records](#read-every-sites-records) |

## Audit record

Reading a trail adds nothing to the trails you read.

## Related

- [Get started with Audit & Reports](/audit-reports/get-started/): one user's and one agent's trail, step by step.
- [Follow what an agent did](/workforce-identity/guides/agents/follow-what-an-agent-did/): the task, start to finish.
- [Look into a locked account](/audit-reports/guides/look-into-a-locked-account/): a user's failed sign-ins.
