# Many devices: enrolment by token

A token lets each device enrol itself, holding no session or access code, and no device secret crosses the network. Calls follow [enrolment](/guides/device-enrol.md).

## Needs

- These routes answer `503` `enrol_tokens_off` until the operator opens them ([limits](/guides/limits.md)).
- An account signed in that signed the text in force ([enrolment](/guides/device-enrol.md), steps 1 to 3), and the yes of the devices' owner.

## Steps

1. Mint a token with the access code as proof; `expiresInHours` and `label` are optional, and `uses` above what the account may still enrol is refused with `most`.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
jq -c '{consent: {version, sha256}, agreed: true, uses: 1, label: "rack_a"}' coronium-consent.json > coronium-mint.json
jq -c --slurpfile m coronium-mint.json '$m[0] + {proof: {code: .accessCode}}' coronium-account.json | curl -sS -b coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/enrol-tokens" -o minted.json
if jq -e '.tokenId | strings' minted.json > /dev/null; then
  jq -r '"Authorization: Bearer \(.token)"' minted.json > token.header
  jq -c .consent minted.json > consent.json
else
  jq -r '"\(.code): \(.cause) Fix: \(.fix)"' minted.json; false
fi
```

Expect: `token.header` and `consent.json` for each device; the token is shown once. A refusal prints its code, cause and fix and writes neither: `enrol_tokens_off` (enrol as in [enrolment](/guides/device-enrol.md)), `token_limit_reached` (revoke one), `device_cap_reached`, `agreement_unsigned`. Safe to repeat: no.

2. On each device, make the id and secret and write `agent.env` before any call, so a lost answer is sent again with the same pair.

```sh
umask 077
[ -e agent.env ] || {
  head -c 32 /dev/urandom >secret.bin
  id=$(od -An -tx1 -N16 /dev/urandom | tr -d ' \n' | sed -E 's/^(.{8})(.{4})(.{4})(.{4})(.{12})$/\1-\2-\3-\4-\5/')
  printf 'CORONIUM_DEVICE=%s\nCORONIUM_DEVICE_SECRET=%s\n' "$id" "$(od -An -tx1 -v secret.bin | tr -d ' \n')" >agent.env
}
```

3. Exchange the token. The body is `secretSha256`, the SHA-256 of the raw secret bytes, never of their hex text, and `consent`.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
id=$(sed -n 's/^CORONIUM_DEVICE=//p' agent.env)
printf '{"secretSha256":"%s","consent":%s}' "$(sha256sum secret.bin | cut -d' ' -f1)" "$(cat consent.json)" >exchange.json
code=$(curl -sS -o answer.json -w '%{http_code}' -X PUT -H @token.header -H 'Content-Type: application/json' \
  --data-binary @exchange.json "$SUPPLY/app/api/devices/$id")
case "$code" in 200|201) printf 'CORONIUM_RELAY=%s\n' "$(jq -r '.relay // empty' answer.json)" >>agent.env && rm -f token.header secret.bin exchange.json ;; esac
echo "$code"
```

Expect: `201` (`applied`), or `200` (`already_applied`, no use spent) for the same id and secret. If refused: `bad_enrol_token`, `enrol_token_expired`, `enrol_token_used` or `enrol_token_revoked` (mint another), `consent_outdated` (sign the text in force, then mint again), `device_id_taken` (delete `agent.env` and `secret.bin`, start at step 2). A 429 means wait: until `retryAt` when given, else a second. A null `relay` means ask the operator. Safe to repeat: yes.

Then run the agent with `agent.env`; [`/kit/SIGNING.md`](/kit/SIGNING.md) has a worked digest.

## Many devices in one call

`PUT /app/api/devices` takes `consent` and `devices`, pairs of `deviceId` and `secretSha256`, and skips entries the account already holds; [the API](/openapi.json) has the rest.

**Never bake `agent.env` into an image**: copies of one id and secret are one device, each replacing the last at the relay. Bake `token.header` and `consent.json`; make the pair at first boot.

## Read, revoke and withdraw

`GET /app/api/enrol-tokens` lists each token's `state`, `used` and `app`, never the token. `POST /app/api/enrol-tokens/{tokenId}/revoke` with `{}` revokes one; `{"withdrawDevices": true, "confirm": true}` also withdraws its devices. Replacing the access code revokes all.
