# Check a token at your API

> Have your API accept only tokens that are genuine, meant for it and still standing, by asking the realm or by checking them itself.

When your API checks tokens itself rather than through the [gateway](/gateway/), it has two
ways to do it. It can ask the realm about each token by **introspection**, which catches a revoked token at once.
Or it can **check the signature itself** with the realm's published keys, which costs no call but keeps accepting a
revoked token until it expires. This guide is for API teams choosing between them and setting one up.

**Before you start:** you know the audience your API's tokens name, such as `https://erp.example.com/api`, and you're
a realm administrator in a group holding `RealmOwner`, with your session in `$CVYUH_SESSION`
([Call the API](/develop/api/)). This guide needs no gateway.

## Choose a way

| | Check it yourself | Ask the realm (introspection) |
|---|---|---|
| How | Verify the signature with the realm's keys; check `iss`, `aud`, `exp`, `typ: at+jwt` | Post the token to the realm; read `active` |
| Cost | No call per request | One call per request |
| A revoked token, an app a user took back, a stopped agent | Still accepted until it expires (five minutes by default), unless you also read [status lists](/workforce-identity/applications/revoke-access/status-lists/) | Refused at once |
| Refresh tokens | Can't be read | Can be checked by the client they belong to |

## Ask the realm

<Steps>

1. Give the API a client of its own, naming its audience in `servesResources`:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=erp-api" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "status": "active",
           "tokenEndpointAuthMethod": "client_secret_basic",
           "clientSecret": "<a long random secret>",
           "grantTypes": ["client_credentials"],
           "servesResources": ["https://erp.example.com/api"] }'
   ```

   For a key in place of a secret, see
   [Choose how a client proves itself](/workforce-identity/guides/services/choose-client-authentication/).

2. For each request, introspect the token, authenticated as `erp-api`:

   ```sh
   curl https://foobar.cvyuh.com/oauth2/introspect -u "erp-api:<the secret>" -d "token=<the token>"
   ```

3. Accept the request only when the answer's `active` is `true`, then read who is calling from it:

   ```json
   { "active": true,
     "sub": "…", "client_id": "…", "scope": "invoices:read",
     "aud": ["https://erp.example.com/api"], "exp": <unix time>,
     "principal_type": "agent", "act": { "...": "..." },
     "cnf": { "jkt": "…" } }
   ```

</Steps>

Anything not active is just `{ "active": false }`. The API may introspect only tokens for the audiences it serves, or
issued to itself, and gets `{ "active": false }` for anything else.

<Aside type="tip" title="Keep proof of the answer">
  Send `Accept: application/token-introspection+jwt` and the answer comes back as a JWT signed by the realm, with the
  same fields under `token_introspection`, typed so it can never pass as an access token. It's signed with the API
  client's `introspectionSignedResponseAlg`; unset, ES256 for a FAPI 2.0 client, else the realm's default (RS256).
</Aside>

## Or check it yourself

<Steps>

1. Read the realm's keys from `https://foobar.cvyuh.com/oauth2/jwks`, and refresh them when a token names a key you don't
   have.

2. Accept a token only when it's signed `ES256`, typed `at+jwt`, its `iss` is `https://foobar.cvyuh.com/oauth2`, its `aud`
   holds your API's audience, and it hasn't expired.

3. If the token carries `cnf.jkt`, require a DPoP proof made with that key: see
   [Bind tokens to a key](/workforce-identity/guides/services/bind-tokens-to-a-key/).

</Steps>

## Check it worked

Send the API a token for another audience: it's refused. Revoke a token and send it again: introspection refuses it
at once; a signature check accepts it until it expires.

## Related

- [Introspection](/workforce-identity/applications/revoke-access/introspection/): every field in the answer.
- [Status lists](/workforce-identity/applications/revoke-access/status-lists/): learn of revocations within a minute, offline.
- [How fast revocation takes effect](/gateway/revocation/how-fast/): each way side by side.
- [Open an API to agents](/gateway/guides/open-an-api-to-agents/): let the gateway do all of this.
