# Get started with Audit & Reports

> Read everything that happened for one user and for one agent on the realm's audit trail, with every call.

In this walkthrough you read the audit trail of the user `maya` in the realm `foobar`: her sign-ins, the tokens issued
to her apps, and what agents did in her name. Then you read the trail of the agent `invoice-agent`: every token it was
issued or refused, every exchange it made, and every change to its record.

## Before you begin

- You hold `READ` on the realm `foobar`: `RealmOwner` includes it, and `RealmViewer` gives it with no right to change
  anything. To give an auditor that access, see
  [Give an auditor read-only access](/audit-reports/audit-trail/read-a-trail/#give-an-auditor-read-only-access).
- You are signed in with your session in `$CVYUH_SESSION`: [Call the API](/develop/api/) shows how. Everything here is
  done in the API.
- The trail names each principal by its id, the `id` the realm gave it when it was made, not the name you address it
  by. The first step of each part looks it up.

## Read what happened

<Steps>

1. **Look up the user's id.** Read `maya` and note her `id`:

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

   ```json
   {
     "id": "<maya's id>",
     "objectId": "maya",
     "typeName": "user",
     "rev": 4,
     "attributes": { "…": "…" }
   }
   ```

2. **Read her trail.** Name her with `person`:

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

   ```json
   {
     "principal": "<maya's id>",
     "kind": "person",
     "events": {
       "data": [
         { "eventType": "TOKEN_ISSUED", "result": "SUCCESSFUL", "…": "…" },
         { "eventType": "LOGIN", "result": "SUCCESSFUL", "…": "…" }
       ],
       "page": 1,
       "pageSize": 20,
       "totalCount": 37,
       "totalPages": 2
     },
     "changes": { "data": [], "…": "…" }
   }
   ```

   `events` are newest first by `eventTime`. Each says what happened (`eventType`), when, how it went (`result`), who
   acted (`principals`), its `context` and, for a sign-in, each step in `entries`. A full `LOGIN` record looks like
   this:

   ```json
   {
     "id": "<record id>",
     "eventType": "LOGIN",
     "eventTime": "<event time>",
     "result": "SUCCESSFUL",
     "userId": "<maya's id>",
     "principals": ["maya"],
     "context": {
       "authLevel": 0, "maxSessionSecs": 7200, "maxIdleSecs": 1800, "name": "maya"
     },
     "entries": [
       { "position": 0, "moduleId": "nativeStore", "result": "succeeded",
         "info": { "authControlFlag": "REQUIRED", "authLevel": 0,
                   "ipAddress": "203.0.113.24", "moduleClass": "NativeStore" } }
     ],
     "trackingIds": {
       "sessionName": "<session name>", "sid": "<sign-in id>",
       "authId": "<sign-in attempt id>", "ctxId": "<session context id>"
     },
     "traceId": "<trace id>",
     "spanId": "<span id>",
     "parentSpanId": null,
     "transactionId": "<transaction id>",
     "component": "authentication",
     "realm": "<your realm>"
   }
   ```

   Its `trackingIds.sid` is the same as the `sid` on every `TOKEN_ISSUED` record that sign-in led to: see
   [Record fields and examples](/audit-reports/audit-trail/record-fields/).

3. **Look up the agent's id.** Read `invoice-agent` and note its `id`:

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

4. **Read the agent's trail.** Name it with `agent`:

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

   The answer has the same shape, with `"kind": "agent"`. `events` holds every token the agent was issued or refused,
   every exchange it made with the chain of agents behind it, and every policy decision it was in.

5. **Read the changes to the agent's record.** A quarantine or a change of status is in its change history, with the
   values before and after:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/history \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   The trail's own `changes` part comes back empty today.

</Steps>

## Check it worked

- `events.totalCount` says how many records there are in all; ask for the next page with `page=2`.
- Sign in as `maya` and read your 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"
  ```

<Aside type="note" title="Refused, or asked twice">
  Naming neither `agent` nor `person`, or both, answers `400`. Reading someone else's trail without `READ` on the realm
  answers `403`. A record can take a moment to appear: if a sign-in you just made isn't there, read again.
</Aside>

## Next steps

<CardGrid>
  <LinkCard title="What is recorded" description="Every kind of record, and when the realm writes it." href="/audit-reports/audit-trail/what-is-recorded/" />
  <LinkCard title="Follow what an agent did" description="An agent's work, start to finish, from its trail." href="/workforce-identity/guides/agents/follow-what-an-agent-did/" />
  <LinkCard title="Change history" description="Every change to one user, group or agent." href="/audit-reports/audit-trail/change-history/" />
</CardGrid>
