# Versions

> Every write keeps the secret's earlier state as a numbered version, so you can read what it was and go back to it.

A secret is a set of named values, such as a user name and a password. Every write makes a new version of the whole
set, numbered from 1, and the earlier versions stay readable. When a change goes wrong, you can read what the secret
was and write it back.

## Before you begin

- **Who:** anyone with every right in the vault writes and reads: the user in their own vault, a member in a group's
  vault, a realm administrator in the realm's vault. A `view` grantee reads every version; a `use` grantee reads the
  latest only. No privilege is needed for your own vault.
- **Where:** the API. There are no screens yet.
- **First:** a realm administrator has turned the store on.

## How it works

Each call is on `https://foobar.cvyuh.com/api/v0/vault/managed/v1/<vault>/data/<path>`, where `<vault>` is the vault's
address ([Vaults](/secrets-store/vaults/)):

| To | Method | What happens |
|---|---|---|
| Write | `POST` with `{ "data": { … } }` | The values sent become the next version |
| Change | `PATCH` with `{ "data": { … } }` | The values sent are merged into the latest as a new version (JSON merge patch). A value sent as `null` is removed |
| Read the latest | `GET` | The latest version's values and metadata |
| Read an older one | `GET` with `?version=<n>` | That version |

The first write creates the path; there is no separate create. A write answers `200` with the new version's
metadata. Values are any JSON object, nested objects included.

A path keeps a set number of versions, 10 unless the realm or the path says otherwise. The write that goes past the
limit drops the oldest version for good.

## Set it up

Change one value of `web/github` in your own vault, keeping the rest ([Call the API](/develop/api/) shows how to get
`$CVYUH_SESSION`):

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/data/web/github \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "data": { "password": "<new password>" } }'
```

Then read the version before it:

```sh
curl "https://foobar.cvyuh.com/api/v0/vault/managed/v1/me/data/web/github?version=1" \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

The answer carries the values under `data.data` and the version's metadata (`version`, `created_time`,
`deletion_time`, `destroyed`, `custom_metadata`) under `data.metadata`.

## When it fails

| Answer | When | What to do |
|---|---|---|
| `400`, "no data provided" | The body has no `data` object | Send `{ "data": { … } }` |
| `400`, "is not a path" | The path is empty, or has an empty, `.` or `..` segment | Use a path such as `web/github` |
| `400`, check-and-set | `options.cas` doesn't match the latest version, or is required and missing | See [Check-and-set](/secrets-store/secrets/check-and-set/) |
| `404` with metadata | The version is deleted or destroyed | Undelete it, or read another version |
| `404` | The path or version isn't there, or, in a personal vault, you have no access | Check the path and the version |
| `404` on a `PATCH` | The latest version is deleted or destroyed | Write the whole secret with `POST` |
| `403` | No right in a group's or the realm's vault, or a `use` grant asking for an older version | Ask for membership or a `view` grant |
| `413` | The request body is larger than 2 MiB | Keep secrets small; store a large file elsewhere and keep its key here |

## Limits and defaults

| Limit | Default | Range | Can it be raised |
|---|---|---|---|
| Versions a path keeps | 10 | 1 to 100 | up to 100, per path or for the realm |
| One request, and so one version's values | 2 MiB | | no |
| Path length, values per secret, nesting depth | no fixed limit, within the 2 MiB request | | |
| Request rate | the store sets no limit of its own | | |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| An old version answers `404` with no metadata | It was dropped past the path's version limit | Raise `max_versions` before you need older history; dropped versions don't come back |
| Writes answer `400` that worked yesterday | The realm now requires check-and-set | Send `options.cas` with each write |
| A `PATCH` answers `404` | The latest version is deleted | Undelete it, or `POST` the whole secret |

## Audit record

The first write is a `CREATED` change record on the secret, and each later write an `UPDATED` one, under the writer.
Every read and write is also an `ACCESS` record. Values are never recorded. See
[Secrets audit trail](/secrets-store/keys-and-protection/audit-trail/).

## Good to know

- **A change needs a live latest version.** Patching a path whose latest version is deleted or destroyed answers
  `404`.
- **A deleted or destroyed version answers `404`** with its metadata and no values, so you can still see when it was
  deleted, or that it was destroyed. See [Delete, undelete and destroy](/secrets-store/secrets/delete-and-destroy/).
- **Reading an older version needs `view`.** Someone with a `use` grant reads the latest version only. See
  [Grants](/secrets-store/sharing/grants/).

## Related

- [Store a secret](/secrets-store/guides/store-a-secret/): write and read your first secret.
- [Change a secret safely](/secrets-store/guides/change-a-secret-safely/): when several writers share one.
- [Metadata](/secrets-store/secrets/metadata/): how many versions a path keeps.
- [Check-and-set](/secrets-store/secrets/check-and-set/): a write that names the version it replaces.
