# Let one service call another

> Let a back-end service call another's API with a short-lived token from the realm, with no user involved.

A report builder needs to call your reports API on a schedule, with no user signed in. With the client-credentials
grant, the calling service gets a short-lived token from the realm and presents it to the API, which knows exactly
which service is calling and what it may do. No shared passwords between services, and nothing that lasts longer than
a few minutes travels with each call. This guide is for back-end services, scheduled jobs and integrations.

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings but
  not change them.
- **Where:** today this is done through the API; [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`.
- **First:** know the API's audience, such as `https://reports.example.com/api`, and the scopes it checks.

## Set it up

<Steps>

1. Offer the API's scopes in the realm: add them to the realm's `scopes`, as in step 1 of
   [Sign users in to a web app](../../sign-in-to-apps/sign-in-web-app/).

2. Register the calling service:

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

   For a stronger credential than a secret, see [Choose how a client proves itself](../choose-client-authentication/).

3. Give the service its client id, its secret and the token address, `https://foobar.cvyuh.com/oauth2/token`.

4. The service asks for a token whenever it needs one:

   ```sh
   curl https://foobar.cvyuh.com/oauth2/token \
     -u '<client-id>:<secret>' \
     -d grant_type=client_credentials \
     -d scope=reports:read \
     -d resource=https://reports.example.com/api
   ```

   It receives an access token for five minutes, and asks again when it expires. There's no refresh token.

</Steps>

## Check it worked

The API receives a signed token (`typ: at+jwt`) with `iss` the realm (`https://foobar.cvyuh.com/oauth2`), `aud` its own
address, `sub` and `client_id` the calling service, `scope` what it may do, and `principal_type` showing a client,
not a user. It checks the signature with the realm's published keys
(see [Check what clients discover](../../../../platform/guides/what-clients-discover/)), or asks the realm, as in
[Check a token at your API](../../../../gateway/guides/check-a-token-at-your-api/).

Each token the service gets is on the trail as a `TOKEN_ISSUED` record under the client, with `grantType`
`client_credentials` and what was granted. Find it in **Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `invalid_client` | Wrong ID or secret, the client is `inactive`, or it is locked out after wrong secrets | Check the credentials and `status` |
| `invalid_scope` | A scope isn't in the client's `scopes`, or the realm doesn't offer it | Add it to both (step 1 and 2) |
| `invalid_target` | The `resource` isn't one of the client's `allowedResources` | Add it, or ask for the default |
| `unauthorized_client` | The client lacks `client_credentials`, or is public | Add the grant; a public client can't use it |
| The API refuses the token | Its `aud` isn't the API's address, or it expired | Ask with `resource` for that API; ask again after five minutes |

## Undo it

Set the client's `status` to `inactive`. Its next token request is refused.

## Related

- [Client credentials](../../../applications/tokens/client-credentials/): the grant in full.
- [Give a client tokens for one API](../tokens-for-one-api/): keep each token to one API.
- [Choose how a client proves itself](../choose-client-authentication/): a key instead of a secret.
