Skip to content
Guide

Machine-to-machine with client credentials

A confidential service app gets tokens for itself with the client_credentials grant; your API verifies them like any other token, within their ceilings.

You get
A backend service that calls your API with a token of its own, within the ceilings you set.
For
Backend developer
Time
About 10 min
Products
Machines and AI
Sign-in
How it works: Backend service, no userNo user: signs in as itself. 1. Your service → Anchr.ing: Client ID + secret. 2. Anchr.ing → Your service: Access token. 3. Your service → Your API: Calls with access token. 4. Your API → Your service: Response.No user: signs in as itselfNo userYour serviceAnchr.ingYour API1Client ID + secret2Access token3Calls with access token4ResponseHow it works: Backend service, no userNo user: signs in as itself. 1. Your service → Anchr.ing: Client ID + secret. 2. Anchr.ing → Your service: Access token. 3. Your service → Your API: Calls with access token. 4. Your API → Your service: Response.No user: signs in as itselfNo userYour serviceAnchr.ingYour API1Client ID + secret2Access token3Calls with access token4Response

A backend job or another service calls your API as itself, with no user. Register an app of kind service (machine to machine, client credentials) with your API's audience. It is confidential: copy its client secret into the service's environment, never into code.

worker/token.ts
// A machine token (client_credentials): the app calls your API as itself.
const res = await fetch('https://acme.test.anchr.ing/token', {
  method: 'POST',
  headers: { 'content-type': 'application/x-www-form-urlencoded' },
  body: new URLSearchParams({ grant_type: 'client_credentials', client_id: 'test_…', client_secret: process.env.CLIENT_SECRET! }),
});
const { access_token, expires_in } = await res.json();

await fetch('https://api.acme.example/api/reports', { headers: { authorization: `Bearer ${access_token}` } });

Tokens from the client_credentials grant have the app's client id as sub and no session. Request a new one when expires_in runs out.

In your API

// In the API: the same middleware verifies it (typed 'apiKey', sub = the client id).
app.use('/api/*', auth.middleware());
app.get('/api/reports', (c) => {
  const machine = c.var.user.claims.client_id === c.var.user.sub;
  return c.json(listReports(machine ? undefined : c.var.user.sub));
});

The middleware types machine tokens apiKey, like a personal access token, so tokens.apiKey permission ceilings and the token's scopes limit them. maxSessionAge and requireMfaWithin do not apply (there is no sign-in); limit machine callers with scopes, ceilings and allowIps. /userinfo refuses these tokens: key your data by sub.

Full example (Backend service): Node

Full example (API): Hono · Express

Every option, error and token type: the SDK README.

Try it in your own test environment

Every feature works in test on the free plan, so the code above runs against your real sign-in.