# Metadata

> A secret's versions, its own settings and your labels, read and set without opening its values.

Metadata is everything the store keeps about a secret except its values: which versions it holds and when each was
written or deleted, the secret's own settings for history and check-and-set, and labels you add, such as which team it
belongs to. Reading it never opens the values, so it is how you check a secret's history and settings.

## Before you begin

- **Who:** reading needs `view`: anyone with every right in the vault, or a `view` grantee. Setting it needs every right
  in the vault: the user in their own vault, a member in a group's, a realm administrator in the realm's.
- **Where:** the API. There are no screens yet.
- **First:** the store is on.

## How it works

| Field | What it is |
|---|---|
| `current_version`, `oldest_version` | The latest version, and the oldest still kept |
| `created_time`, `updated_time` | When the path was first and last written |
| `versions` | Each version kept, with its `created_time`, its `deletion_time` if it is deleted, and whether it is `destroyed` |
| `max_versions` | How many versions this path keeps, at most 100; `0` takes the realm's |
| `cas_required` | Whether every write of this path must use [check-and-set](/secrets-store/secrets/check-and-set/) |
| `delete_version_after` | How long after its write a version reads as deleted; `0s` takes the realm's |
| `custom_metadata` | Your own labels, such as `{ "team": "engineering" }` |

Each call is on `https://foobar.cvyuh.com/api/v0/vault/managed/v1/<vault>/metadata/<path>`:

| To | Method |
|---|---|
| Read it | `GET` |
| Set settings and replace every label | `POST` |
| Set settings and merge labels | `PATCH`; a label sent as `null` is removed |

Setting metadata answers `204`. Durations are whole seconds, or text such as `"1h30m"`.

## Set it up

Keep five versions of `web/github` and label it ([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/metadata/web/github \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "max_versions": 5, "custom_metadata": { "team": "engineering" } }'
```

## When it fails

| Answer | When | What to do |
|---|---|---|
| `400`, "'custom_metadata' is not a valid value" | A label isn't text, or `custom_metadata` isn't an object | Send labels as strings; `null` only with `PATCH`, to remove one |
| `400` on `max_versions` | Not a whole number, or over 100 | Send 0 to 100 |
| `400` on `delete_version_after` | Not seconds or a duration such as `"720h"`, or over ten years | Send a valid duration |
| `404` reading | The path isn't there, or, in a personal vault, you have no access | Check the path |
| `403` | A `use` grant, or no right in a group's or the realm's vault | Ask for `view`, or for membership |

## Limits and defaults

| Limit | Default | Range |
|---|---|---|
| `max_versions` | `0`, the realm's (10 unless changed) | 0 to 100 |
| `delete_version_after` | `0s`, the realm's (kept) | 0 to ten years |
| `cas_required` | `false` | |
| Labels | none | text values only; no fixed count, within the 2 MiB request |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Lowering `max_versions` left old versions in place | A new limit applies at the next write | Write the secret once; the oldest past the limit are dropped then |
| A new `delete_version_after` didn't touch existing versions | It applies to versions written after it | Delete older versions yourself if you need them gone |
| Labels you set disappeared | `POST` replaces every label | Use `PATCH` to add or change one |

## Audit record

Setting metadata is a `CREATED` change record on a new path, or `UPDATED` on an existing one, under who set it.
Labels are sealed and never recorded. See [Secrets audit trail](/secrets-store/keys-and-protection/audit-trail/).

## Good to know

- **Labels are text.** `custom_metadata` is an object whose values are strings, sealed like the secret's values.
- **A new limit applies at the next write.** Lowering `max_versions` drops the oldest versions past it when the path
  is next written.
- **A new deletion time applies to versions written after it.** Versions already written keep the time they were
  given.
- **Setting metadata creates the path** if it doesn't exist yet, with no versions.
- **Reading it needs `view`.** Someone with only a `use` grant can't read a secret's metadata.

## Related

- [Limit a secret's history](/secrets-store/guides/limit-a-secrets-history/): set the limits and labels.
- [Realm settings](/secrets-store/realm-settings/): the defaults a path takes when it sets none.
- [Subkeys](/secrets-store/secrets/subkeys/): the names inside a secret.
