Skip to content
Services and APIs

Keycloak machine-to-machine authentication

Background jobs, microservices and partner integrations need to prove who they are too. Keycloak issues them tokens with the client credentials grant, scopes them with service account roles, and swaps them with standard token exchange when one service calls another for a user.

Point here to play
A simplified drawing. Services and APIs are examples.
The short answer

Register each calling service as a confidential client with service accounts enabled. It asks the realm’s token endpoint for an access token with the client credentials grant, authenticating with a secret or a signed JWT. Give its service account only the roles it needs, put your API in the token’s audience with an audience mapper, and have each API validate tokens against the realm’s published keys. When a service must call another on a user’s behalf, use standard token exchange, supported since Keycloak 26.2.

How it works

Built from Keycloak’s own parts

Realms, clients, authentication flows and mappers, as the standard admin console has them. Nothing here needs a custom extension.

One client per calling service

Create an OpenID Connect client for each service, never one shared by several. Turn Client authentication on and Service accounts roles on, and turn Standard flow off: nobody signs in to this client through a browser. The service then asks for a token:

A client credentials request
curl -s https://auth.yourcompany.com/realms/services/protocol/openid-connect/token \
  -d grant_type=client_credentials \
  -d client_id=orders-worker \
  -d client_secret="$CLIENT_SECRET"

The answer is a signed JWT access token with no refresh token. Cache it and ask again shortly before it expires, rather than asking for every call. The token request builder writes the request for each grant.

Roles, scopes and audience

A client with service accounts has a service account user. Give it roles on its Service accounts roles tab: client roles of the API it calls, such as orders:read, rather than broad realm roles.

APIs should refuse tokens meant for someone else. Add an Audience mapper to a client scope, pointing at the API’s client, so the token’s aud names that API. Keep access tokens short, a few minutes, so a leaked token is soon useless.

Validating tokens in the API

Each API checks the signature with the realm’s keys, published at /realms/services/protocol/openid-connect/certs, and caches them. Then it checks iss, aud, exp and the scope or role the call needs. Most JWT libraries do this from the realm’s discovery document. When an API needs to know a token is still active, it calls the introspection endpoint instead.

The JWT verifier and the JWKS viewer let you check a token and the keys by hand.

Calling another service for a user

When the Orders API must call the Billing API for the user who called it, it should not forward that user’s token as is. Standard token exchange (RFC 8693), supported since Keycloak 26.2, swaps it for a token meant for the next service:

The form the Orders API posts to the token endpoint, with its own client credentials
grant_type=urn:ietf:params:oauth:grant-type:token-exchange
subject_token=<the user’s access token>
subject_token_type=urn:ietf:params:oauth:token-type:access_token
audience=billing-api

Turn on Standard token exchange in the requesting client’s settings. The audience parameter narrows the new token to the service named. Standard token exchange covers exchanges within one realm. The older token exchange (version 1), which also did impersonation and exchanges with external providers, is a deprecated preview feature, off by default, and our clusters run Keycloak’s defaults.

Keeping client credentials safe

  • Prefer a signed JWT (private_key_jwt) to a shared secret where the service can keep a private key. Upload its public key or certificate to the client, so Keycloak needs no outbound call to check it.
  • Rotate secrets without downtime: client secret rotation, supported since Keycloak 26.8, keeps up to two secrets active while services move to the new one.
  • Bind tokens to a key with DPoP, supported since Keycloak 26.4, when a stolen token must be useless without the key.

Mutual TLS client authentication needs the client certificate to reach Keycloak, and TLS ends at our gateway, so ask us before you plan on it.

Who does what

We run the server. You shape the realm.

RealmDeploy runs
  • The token endpoint on your domain, over HTTPS, on a cluster of your own
  • Two or more replicas on Premium and Enterprise, so tokens keep coming during patch upgrades
  • Signing keys kept in your own database, included in every backup
  • Monitoring of request rates and errors, with alerts to our on-call
You configure, in the admin console
  • One client per service, with service accounts on and browser flows off
  • Service account roles and audience mappers
  • Token lifetimes and the standard token exchange switch
  • Client authentication: secrets, signed JWTs and their rotation
  • Token checks in each API
Which plan

Usually Premium

The plans are sized by realms and by active users a month: people who sign in at least once in the month. These are sizing guides, not hard caps.

Starter$199 a month
  • Dedicated Keycloak, 1 replica
  • Up to 2 realms
  • Sized for about 5,000 active users a month
  • Best effort, no uptime SLA
Fits this jobPremium$999 a month
  • High-availability cluster, 2+ replicas
  • Up to 20 realms
  • Sized for about 100,000 active users a month
  • 99.9% monthly uptime SLA
Enterprise$2,499 a month
  • Larger high-availability cluster
  • Up to 100 realms
  • Sized for about 500,000 active users a month
  • 99.95% monthly uptime SLA

When services cannot get tokens, they stop working, so run machine-to-machine traffic where the token endpoint stays up: Premium has two or more replicas and a 99.9% SLA. The plans are sized by people who sign in; a heavy rate of machine token requests is a load question, so tell us your expected rate when you order. Enterprise’s larger cluster and 99.95% SLA fit high rates. Starter suits development and staging.

Every plan, feature by feature

Questions

Frequently asked questions

Can a service get a token without a user?

Yes. That is the client credentials grant: the service authenticates as its own client and gets a token for its service account, with the roles you gave it.

Should several services share one client?

No. One client per service lets you give each only the roles it needs, see which one made a call, and revoke one without touching the others.

How long should machine tokens live?

Minutes, not hours. Services cache the token and request a new one just before it expires, so short lifetimes cost little and limit what a leaked token can do.

Can an API check tokens without calling Keycloak?

Yes. Access tokens are signed JWTs, so an API validates them with the realm’s public keys, which it fetches once and caches. Call the introspection endpoint only when you need to know a token has not been revoked.

Is token exchange supported?

Standard token exchange, for tokens within one realm, is supported since Keycloak 26.2 and on by default. The older version 1, with impersonation and external exchanges, is a deprecated preview that our clusters do not turn on.

Can I automate creating clients?

Yes, through Keycloak’s own admin REST API on your domain, for example with kcadm.sh. RealmDeploy has no API or Terraform provider of its own.

Can I use mutual TLS for client authentication?

Not as things stand: TLS ends at our gateway, so the client certificate does not reach Keycloak. Use a signed JWT or a client secret, and write to us if you need mTLS.

Run it on a cluster of your own

Order a realm in the dashboard, or write to us first about what you are building.