# Device limits

> Cap how many devices a realm may enroll, and how long an enrollment token may be spent.

Two settings keep enrollment in bounds. The device cap stops a realm from growing past the number of devices it is
meant to have, however many tokens are handed out. The token lifetime keeps a token that leaks from being useful
for long.

## Before you begin

- **Privilege:** reading the settings needs `READ` on the realm's endpoint settings, which `RealmViewer` and
  `RealmOwner` hold; changing the token lifetime needs `MODIFY`, which `RealmOwner` holds
  ([Who can call](../../api/#who-can-call)). Only an administrator of the root realm can change the device cap.
- **Where:** the API.

## How it works

| Setting | Field | Default | Range | Who sets it |
|---|---|---|---|---|
| Devices | `maxDevices` | 100 | 1 and up | An administrator of the root realm only |
| Enrollment token lifetime | `enrolTokenTtlHours` | 24 hours | 1 to 168 hours (a week) | The realm's administrators |

**The device cap** is checked before anything of an enrollment is made: an enrollment past the cap registers
nothing, records nothing, and is refused with `409`. The cap holds even when many devices enroll at once.

**The token lifetime** is the lifetime a token gets when its maker names none, and the longest one they may name. A
`ttlHours` of zero, below zero, or past the realm's setting is refused with `400`.

## Set it up

Both are fields of the realm's `endpointSettings` settings object
([Call the API](../../../develop/api/)). To keep tokens to eight hours:

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/endpointSettings/endpointSettings \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{"enrolTokenTtlHours": 8}'
```

Read the current values with a `GET` on the same address.

## What the person enrolling sees

At the cap, `enroll` on the device is refused, and prints:

```
enrol failed: enrol refused, 409 Conflict: {"code":"DISABLED","message":"the realm enrols its most devices, 100","detail":null}
```

The token isn't spent: once there's room, the same token enrolls the device while it lasts.

## If it fails

| Status | When | What to do |
|---|---|---|
| `400` on making a token | `ttlHours` is below 1 or past the realm's lifetime; the answer names the range | Ask for a lifetime within it, or leave `ttlHours` out |
| `400` on the settings | A value outside its range, such as a lifetime past 168 | Keep to the ranges above |
| `403` on the settings | You lack `MODIFY` here, or you changed `maxDevices` from a realm other than the root | Ask a root realm administrator to change the cap |
| `409` on enrolling | The realm is at its cap | Remove a device you no longer need, or have the cap raised |

## Limits and defaults

| What | Default | Range | Can it be raised |
|---|---|---|---|
| Devices per realm | 100 | 1 and up | Yes, by an administrator of the root realm |
| Enrollment token lifetime | 24 hours | 1 to 168 hours | Up to 168 hours, by the realm's administrators |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Enrollments are refused at the cap, but you removed devices | A removal frees its place only once it has finished | Read the removed device's record until it answers `404`, then enroll ([Device removal](../../revoke-and-remove/removal/)) |
| Enrollments are refused at the cap, with fewer devices in use than the cap | Old records from devices enrolled again are still counted | Remove the records of devices enrolled again ([Enroll a device again](../../guides/enroll-a-device-again/)) |
| A token request for a week is refused | The realm's lifetime is shorter | Raise `enrolTokenTtlHours`, or ask for less |

## Audit record

Each change to the settings is recorded on the realm's audit trail as a change to `endpointSettings`, with who made
it and the values before and after. Read it at
`https://foobar.cvyuh.com/api/v0/am/objects/v1/endpointSettings/endpointSettings/history`; see
[Change history](../../../audit-reports/audit-trail/change-history/).

## Good to know

- **The cap is the root realm's.** A realm's own administrators can read it but not raise it, so a deployment can
  allot devices per realm.
- **Changes apply to new enrollments only.** Lowering the cap below the devices you have removes no device; it stops
  new ones until the count is under it again. A shorter lifetime doesn't shorten tokens already made.
- **A retry doesn't count twice.** A device retrying its own enrollment over the same key is answered without asking
  the cap again.
- **Removing a device frees its place** once its removal has finished.
- **The field is spelled `enrolTokenTtlHours`**, as the realm names it.

## Related

- [Device enrollment](../device-enrollment/): how a token is made and spent.
- [Device removal](../../revoke-and-remove/removal/): taking a device out of the count.
