# Change history

> Every change made to one user, group, agent or setting, newest first, with who made it and what changed, secrets left out.

Every object in the realm keeps its own history: each time a user, a group, an agent, a client or a setting is
created, changed or deleted, the realm records who did it and what changed, including the links added or removed, such
as a group's members. Reading one object's history answers *who changed this, and how?*

This is **limited** today: a link change shows on the history of the object it was made through, not on both ends,
and a deleted object's history can't be read.

## Before you begin

- **Privilege:** the same access as reading the object: `READ` on the realm, which `RealmViewer` and `RealmOwner`
  both give.
- **Where:** the API.
- **What you need first:** the object's type and name, as you address it: `group/engineering`, `user/maya`,
  `agent/invoice-agent`, or a setting such as `authorizationSettings/authorizationSettings`.

## How it works

The history is read at the object's own address, with `/history` added:
`https://foobar.cvyuh.com/api/v0/am/objects/v1/<type>/<id>/history`. It lists every change, newest first, and leaves
out every value no read of the object would show, such as a password. A link added or removed, such as a member
joining a group, is an `UPDATED` entry on the object whose link changed, listing the link in `edgeChanges`.

Each entry says what was done and by whom:

```json
{
  "id": "<record id>",
  "eventType": "UPDATED",
  "eventTime": "<event time>",
  "objectId": "<the group's id>",
  "changedBy": "<the id of who made the change>",
  "changedFields": ["member"],
  "before": { "…": "the group as it was" },
  "after": { "…": "the group as it became" },
  "edgeChanges": { "member": { "added": [ { "…": "the link to ben" } ] } },
  "subjectRev": "5",
  "component": "entity",
  "traceId": "<trace id>",
  "transactionId": "<transaction id>"
}
```

| Field | What it holds |
|---|---|
| `eventType` | `CREATED`, `UPDATED`, `PATCHED` or `DELETED` |
| `changedBy` | The id of the user or agent who made the change |
| `changedFields` | The fields and links that changed, secret ones named but never shown |
| `before`, `after` | The object before and after, secret values left out |
| `edgeChanges` | For each link field, the links `added`, `changed` and `removed`, and `removedAll` when every link went |
| `subjectRev` | The object's revision after the change |

## Set it up

There's nothing to set up. Read the history of the `engineering` group ([Call the API](/develop/api/) shows how to get
`$CVYUH_SESSION`):

```sh
curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/group/engineering/history?page=1&pageSize=50" \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

The same works for any type: `user/maya`, `agent/invoice-agent`, or a setting such as
`authorizationSettings/authorizationSettings`.

## What happens when it fails

| Answer | Why | What to do |
|---|---|---|
| `200` with no entries | The object has no changes yet, or it no longer exists | Check the name; a deleted object's history can't be read here |
| `400` | A `page` so large it is past any history | Page forward from 1 |
| `401` | No session, or it has ended | Sign in again |
| `403` | No read access to the object | Ask for `RealmViewer` |

## Limits and defaults

| What | Default | Range |
|---|---|---|
| `pageSize` | 50 | 1 to 500 |
| `page` | 1 | From 1 |
| Order | Newest first | Fixed |
| Total count | Not given | Page until a page comes back short |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A member you know was added isn't in the group's history | The link was made through the user (their `memberOf`), so it is recorded on the user | Read the user's history too |
| The history is empty for an object you deleted | A deleted object's history can't be read here | Its changes are kept; see [Retention and sites](/audit-reports/audit-trail/retention/) |
| You can't tell how many entries there are | The history isn't counted | Ask for the next page until one comes back with fewer than `pageSize` entries |

## Audit record

Reading a history adds nothing to it. Every change it lists is a record on the realm's trail, kept for the life of the
realm.

## Good to know

- **A link change is recorded once,** on the object it was made through. A member added through the group shows on
  the group's history; one added through the user's `memberOf` shows on the user's.
- **An agent's changes are here, not on its trail.** An agent's trail comes back without them today: read the agent's
  history.
- **Names in a history are ids.** `changedBy` and `objectId` are ids; read the object to see its name.

## Related

- [See who changed a group](/audit-reports/guides/see-who-changed-a-group/): the task, start to finish.
- [What is recorded](/audit-reports/audit-trail/what-is-recorded/): every kind of record.
