# Open an API to agents

> Let your organization's agents call an API through the gateway, read-only, and see a stopped agent refused within seconds.

Your invoices API should be open to your AI agents, but only to read, and an agent that misbehaves should lose access
at once, not when its token runs out. With the gateway in front of the API, a policy in the realm says what agents may
do, the gateway refuses everything else, and quarantining an agent stops it at the API within one revocation poll.
This guide is for the team that owns the API and the realm administrator who runs the agents.

**Before you start:** the gateway runs in front of the API as in [Get started with Gateway](/gateway/get-started/),
enforcing the `default` policy set, with the audience `https://api.example.com`. Its image isn't published yet: ask
your Cvyūh contact for it. The agent `invoice-agent` is registered, as in
[Register an agent](/workforce-identity/guides/agents/register-an-agent/). You're a realm administrator in a group
holding `RealmOwner`, with your session in `$CVYUH_SESSION` ([Call the API](/develop/api/)).

## Write the policy

<Steps>

1. **Allow agents to read invoices.** The subject is any token whose `principal_type` is `agent`, whichever agent it
   is:

   ```sh
   curl -i "https://foobar.cvyuh.com/api/v0/am/objects/v1/policy?id=agents-read-invoices" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "active": true,
           "resources": ["*://*:*/invoices/*"],
           "actionValues": { "GET": true },
           "subject": { "type": "JwtClaim", "claimName": "principal_type", "claimValue": "agent" } }'
   ```

2. **Link it to the `default` set and the `url` type**, each with the revision the last answer's `ETag` gave:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/agents-read-invoices/relationships/policySet \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d '{ "targetId": "default" }'

   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/agents-read-invoices/relationships/resourceType \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d '{ "targetId": "url" }'
   ```

3. **Let the agent ask for tokens for the API.** Add `https://api.example.com` to the `allowedResources` of the
   agent's client. A list is replaced whole, so send the resources it already names with it.

</Steps>

The gateway picks the policy up at its next poll, within a minute.

## Check it worked

Call through the gateway with the agent's token for `https://api.example.com`:

| You send | You get |
|---|---|
| `GET /invoices/1` | Your API's own answer. Your API sees `x-agent-id` set to the agent's id |
| `DELETE /invoices/1` | `403`, reason `no_matching_policy` |
| A user's token, `GET /invoices/1` | `403`: the policy names agents only |

Then quarantine the agent:

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

Call again with the same token: within ten seconds, the gateway's next revocation poll, the answer is
`401 agent_revoked`. The agent can't get a new token either. The quarantine is on the audit trail as a `PATCHED`
record for the agent; the gateway's refusal is a warning line in its own log.

<Aside type="tip" title="Bind the agent's tokens">
  An agent's tokens are bound to its key by default, so a token copied out of its environment is refused at the
  gateway too: see [Bound tokens at the gateway](/gateway/protect-apis/bound-tokens/).
</Aside>

## Undo it

Set the agent's `status` back to `active` to let it in again, and delete the `agents-read-invoices` policy to close the
API to agents.

## Related

- [Run the gateway](/gateway/protect-apis/run-the-gateway/): every check a request goes through.
- [Requests and refusals](/gateway/protect-apis/requests-and-refusals/): what callers see.
- [Stop an agent](/workforce-identity/guides/agents/stop-an-agent/): quarantine and kill, and every agent beneath.
- [Policies](/workforce-identity/authorization/policies/): writing more rules.
