# Register an agent

> Give an AI agent its own identity, with an owner, a risk class and a status, and a client it gets tokens through with no secret to leak.

An AI agent or automation calling your APIs should be an identity you can see, govern and stop, not a shared
password in a config file. Once registered, the agent has a named owner, a risk class and a status, and gets
short-lived tokens on its own account with no password or shared secret. Each token tells every API that an agent is
calling, which one, how risky it is, and who owns it. This guide is for the team putting an agent to work.

**Before you start:**

- **Who can do it:** a realm administrator (a group holding `RealmOwner`).
- **Where:** today this is done through the API, with your session in `$CVYUH_SESSION`
  ([Call the API](/develop/api/)).
- **What you need:** the user accountable for the agent, its business owner (here `maya`); and either the public half
  of a key the agent holds, or the intent to hand it a one-time bootstrap assertion instead. The client's scopes must
  be ones the realm offers (`scopes` in `oauth2Settings`): add your API's, such as `invoices:read`, first, as in
  [Sign users in to a web app](../../sign-in-to-apps/sign-in-web-app/).

## Register it

<Steps>

1. Create the agent:

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/agent?id=invoice-agent" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
           "uid": "invoice-agent",
           "displayName": "Invoice agent",
           "purpose": "Matches supplier invoices to purchase orders",
           "riskClass": "limited",
           "autonomyTier": 2,
           "status": "active",
           "expiresAt": "<RFC 3339 time>"
         }'
   ```

   To create it under another agent, add `"parent": [{ "id": "<parent's id>" }]`. The parent can never change later.

2. Name `maya` as its business owner:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/relationships/businessOwner \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "maya" }'
   ```

3. Register its client. With a key of its own (`registeredKey`):

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=invoice-agent-client" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
           "status": "active",
           "clientName": "Invoice agent",
           "tokenEndpointAuthMethod": "private_key_jwt",
           "assertionTrust": "registeredKey",
           "jwks": {
             "keys": [
               { "kty": "EC", "crv": "P-256", "kid": "agent-1", "x": "…", "y": "…",
                 "exp": <unix time, at most 90 days ahead> }
             ]
           },
           "grantTypes": ["client_credentials"],
           "scopes": ["invoices:read"],
           "defaultScopes": ["invoices:read"],
           "allowedResources": ["https://app.example.com/api"],
           "defaultAudience": "https://app.example.com/api"
         }'
   ```

   For a bootstrap instead, set `"assertionTrust": "amBootstrap"` and leave out `jwks`.

4. Link the client to the agent:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/invoice-agent-client/relationships/agent \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "invoice-agent" }'
   ```

   Once linked, a client that names no `securityProfile` is written `fapi2`: every token request it makes carries a
   DPoP proof, and its tokens come back DPoP-bound. Set `"securityProfile": "oauth2.1"` in step 3 only if the agent
   can't send proofs. An agent's client that authenticates by attestation (`attest_jwt_client_auth`) is written
   `oauth2.1` instead, since FAPI 2.0 takes no attestation.

5. With a bootstrap only, mint the agent's first assertion and hand its `assertion` to the agent:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/clients/v1/invoice-agent-client/bootstrap \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   It's signed by the realm, valid five minutes, and used once, for the agent's first token. Use it to hand an agent
   its first credential without handling a key yourself.

</Steps>

<Aside type="caution" title="Every key must end">
  Every key an agent's client registers carries `exp`, the time it stops working, in seconds since 1970, no further
  ahead than the realm's `agentKeyMaxLifetimeSecs`: 90 days by default, a year at most. A key with no end, or one
  ending further off, is refused when you write it.
</Aside>

## Check it worked

The agent asks for a token with an assertion signed by its key (or the bootstrap assertion), and a DPoP proof:

```sh
curl https://foobar.cvyuh.com/oauth2/token \
  -H "DPoP: <proof>" \
  -d grant_type=client_credentials \
  -d client_id=invoice-agent-client \
  -d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
  -d client_assertion=eyJ…
```

Its token carries:

| Claim | Value |
|---|---|
| `sub` | the agent's permanent id |
| `principal_type` | `agent` |
| `mode` | `autonomous`: no user behind this request |
| `risk_class`, `autonomy_tier` | from the agent |
| `business_owner` | the owner's name, `maya` |

If the request is refused `invalid_client`, see [AI agents](../../../agents/ai-agents/#troubleshooting). Each request,
issued or refused, is a `TOKEN_ISSUED` record on the agent's trail
([Follow what an agent did](../follow-what-an-agent-did/)).

## Rotate its key

Once a key is past its end it authenticates nothing. To rotate, add the next key with a later `exp` and a new `kid`,
then remove the old one: both work while both are current.

- **Keys are judged only when they change.** A write is checked against key ages only when it changes the keys, or
  `assertionTrust`, or links the client to its agent. Any other write stands whatever the keys' age, so you can
  always disable an agent's client, even one whose key has ended.
- **Keys at a `jwksUri` need an `exp` too.** An agent's client that publishes its keys at a `jwksUri` is checked at
  its first assertion, since nothing is fetched when it's written: a key there with no `exp` is refused then.
  Publish an `exp` on each key.

## Undo it

To stop an agent you registered by mistake, retire it (`{ "status": "retired" }`), as in
[Stop an agent](../stop-an-agent/): it gets no token again. To stop only its client, set the client's `status` to
`inactive`.

## Related

- [AI agents](../../../agents/ai-agents/): every field an agent has, and what the realm holds to.
- [Client authentication](../../../applications/client-security/client-authentication/) and
  [Client credentials](../../../applications/tokens/client-credentials/): how the client proves itself and gets tokens.
- [Stop an agent](../stop-an-agent/): pause it or stop it for good.
- [Let an agent act for a user](../agent-acts-for-a-person/): delegated tokens in a user's name.
