Skip to content

OAuth token exchange request builder

Build an OAuth 2.0 token exchange request (RFC 8693) with subject token, token types, audience, resource and scope.

Runs in your browser. Nothing you paste is sent to us.

Request

The base URL, without /realms/…. Add a path if your server has one, such as /auth on old versions.

The token you have: usually the access token your service received. A placeholder is filled in.

Who the new token is for. In Keycloak, the client id of the target service.

The URL of the target API, as an alternative to audience.

For delegation: a token for the party acting on behalf of the subject.

A sample value is filled in. Whatever you type stays in this page.

Token exchange request

curl command
curl 'https://auth.example.com/realms/acme/protocol/openid-connect/token' \
  -H 'Authorization: Basic d2ViLWFwcDpzYW1wbGUtc2VjcmV0LW5vdC1yZWFs' \
  --data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange' \
  --data-urlencode 'subject_token=SAMPLE_ACCESS_TOKEN' \
  --data-urlencode 'subject_token_type=urn:ietf:params:oauth:token-type:access_token' \
  --data-urlencode 'audience=orders-api'
What comes back
A JSON object with access_token, issued_token_type (the type of the new token), token_type and expires_in (RFC 8693 section 2.2).
In Keycloak
Since Keycloak 26.2, standard token exchange is turned on per client, with Standard token exchange in the client’s capability settings. The older token exchange is still a preview feature.

How to use it

  1. Enter your Keycloak server URL and realm, or a custom token endpoint.
  2. Paste the subject token: usually the access token your service received.
  3. Set the audience or resource of the API you need to call.
  4. Enter the calling client’s credentials and copy the request.

What to know

Token exchange lets a service trade a token it holds for a new one, for example one meant for another API. The request uses grant_type=urn:ietf:params:oauth:grant-type:token-exchange.

subject_token_type and requested_token_type are URNs such as urn:ietf:params:oauth:token-type:access_token. The response says which type was issued in issued_token_type.

Since Keycloak 26.2, standard token exchange is turned on per client in its capability settings. The older token exchange remains a preview feature.

What token exchange solves

Picture a request that passes through several services. A user calls the orders API. The orders API needs to call the billing API on the same user’s behalf. What token should it send?

Forwarding the user’s original access token is tempting but risky. That token was issued for the orders API. If billing accepts it, any service holding it can call billing too, and a leak in one place opens every API the token works for.

OAuth 2.0 Token Exchange, RFC 8693, gives a cleaner answer. The orders API sends the user’s token to the authorization server and asks for a new token meant for billing. The server checks that this exchange is allowed and issues a token with the right audience and scope.

How a token exchange request works

Token exchange is a grant at the normal token endpoint. The calling service authenticates as itself and sends the token it holds as the subject token.

Sample request (made-up values)
POST /realms/acme/protocol/openid-connect/token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic b3JkZXJzLWFwaTpzYW1wbGUtc2VjcmV0

grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange
&subject_token=SAMPLE_ACCESS_TOKEN
&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token
&audience=billing-api
Sample response (made-up values)
{
  "access_token": "SAMPLE_EXCHANGED_TOKEN",
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
  "token_type": "Bearer",
  "expires_in": 300
}

The response is a normal token response plus issued_token_type, which RFC 8693 section 2.2.1 requires. It tells the caller what kind of token it received.

The parameters, one by one

subject_token and subject_token_type

The subject token represents the party the new token is about, usually the signed-in user. Its type is a URN. The common ones are urn:ietf:params:oauth:token-type:access_token, refresh_token, id_token and jwt, all under the same prefix. Both parameters are required.

audience and resource

audience names the target service by a logical name, such as a client id. resource names it by URL, as in RFC 8707. Both are optional and may be repeated. A server may support one or both.

requested_token_type and scope

requested_token_type asks for a particular kind of token back. Leave it out and the server picks, usually an access token. scope narrows what the new token may do.

actor_token and actor_token_type

These are for delegation: the actor token represents the party acting on the subject’s behalf. The issued token may then carry an act claim naming the actor. Without them, the exchange is closer to impersonation, where the new token simply represents the subject.

Delegation and the act claim

RFC 8693 separates two ideas. In impersonation, the new token represents the subject and nobody else; a service holding it looks exactly like the user to the next API. In delegation, the new token represents the subject and also names who is acting for them.

Delegation shows up in the token as the act claim, defined in RFC 8693 section 4.1. It is a JSON object that identifies the actor, usually with a sub. Chains are nested: an actor inside an actor shows each hop.

Sample claims of a delegated token (made-up values)
{
  "aud": "billing-api",
  "sub": "1f8f4b3a-sample",
  "act": { "sub": "orders-api" }
}

Section 4.4 also defines may_act, a claim in the subject token that says who is allowed to act for the subject. Whether your server issues or checks these claims depends on the server; read the issued token to find out.

Checking the exchanged token

After an exchange, check the result before you rely on it.

  • issued_token_type in the response matches what you asked for.
  • aud names the target service, and only it.
  • scope is no wider than the original token’s scope.
  • sub is still the user, not the calling service.
  • exp is short, since the token serves one call chain.

What this tool does and does not do

The builder writes the whole exchange request as curl and as raw HTTP, with every RFC 8693 parameter available.

  • It offers the standard token type URNs in lists, so you do not have to type them.
  • It sends optional parameters only when you fill them in.
  • It requires client authentication, since the caller must prove who it is.
  • It builds the Keycloak token endpoint from a server URL and realm, or takes a custom URL.

It does not send the request, check your server’s exchange policy or decode the tokens. Read the result with the JWT decoder to see the new audience and scope.

How to use the builder

  1. Enter your Keycloak server URL and realm, or a custom token endpoint.
  2. Paste the subject token: usually the access token your service received.
  3. Pick its type; access_token is the usual choice.
  4. Set the audience of the service you need to call.
  5. Enter the calling client’s id and credentials.
  6. Copy the curl command, run it and check issued_token_type and the new token’s aud.

Where token exchange fits

Service chains in a back end

This is the main use. Each hop exchanges the token it received for one scoped to the next service. Every token is valid for one audience, so a leak in one service does not unlock the others.

Narrowing a token

A gateway can trade a broad token for a narrower one before it calls an internal API. The internal API then sees only the scope it needs.

Where it does not fit

Token exchange is not a way for a browser or mobile app to get extra tokens. The caller authenticates as a confidential client, so it belongs on a server. A service acting as itself, with no user, should use client credentials instead.

How Keycloak handles token exchange

Keycloak serves token exchange at its normal token endpoint, https://<host>/realms/<realm>/protocol/openid-connect/token.

  • Since Keycloak 26.2, standard token exchange is a supported feature. It is turned on per client with Standard token exchange in the client’s capability settings.
  • The calling client authenticates, so it needs Client authentication on.
  • The older token exchange, which also covers cases such as exchanging with external identity providers, is still a preview feature.

The exact rules for which audiences a client may request depend on your Keycloak version and setup. Test the exchange in your realm and read the token you get back.

Common errors and how to fix them

ErrorUsual causeFix
unsupported_grant_typeToken exchange is not enabled, or the grant URN has a typo.Turn on Standard token exchange for the client and copy the URN exactly.
invalid_clientThe calling client did not authenticate or used a wrong secret.Send the client’s credentials.
invalid_targetThe audience or resource is unknown or not allowed.Check the target’s client id and the exchange permissions.
invalid_requestA required parameter is missing, often subject_token_type.Send both subject_token and subject_token_type.
invalid_grantThe subject token expired or is not valid for this exchange.Use a fresh token from the current request.

The OAuth error lookup has more detail on each code.

Security notes

An exchange can create powerful tokens, so treat exchange rights like any other permission. Allow each client to exchange only for the audiences it needs.

Prefer narrowing over widening. A token for a downstream service should carry less scope than the one the user gave the front service.

RFC 9700 asks for audience-restricted access tokens, so a token is accepted only by the service it was meant for. Exchanging per hop is one way to get there.

Best practices

  • Exchange once per downstream call chain and cache the result until it expires.
  • Always set audience or resource so the new token has a clear target.
  • Check aud in the receiving service and reject tokens meant for others.
  • Keep the calling client’s credentials on the server, ideally as a signed JWT instead of a shared secret.
Questions

Frequently asked questions

What is OAuth 2.0 token exchange?

It is the grant from RFC 8693. A client sends a token it holds and gets back a new one, for example a token meant for another API. The grant type is `urn:ietf:params:oauth:grant-type:token-exchange`.

What is the difference between audience and resource in token exchange?

`audience` names the target service logically, such as by client id. `resource` names it by URL, following RFC 8707. Servers support one or both.

How do I enable token exchange in Keycloak?

Since Keycloak 26.2, turn on Standard token exchange in the calling client’s capability settings. The client must authenticate. The older token exchange remains a preview feature.

What is subject_token_type?

It is a URN that says what kind of token `subject_token` is. For an access token it is `urn:ietf:params:oauth:token-type:access_token`. It is required.

Is token exchange the same as impersonation?

Not always. Without an actor token, the new token represents the subject, which is close to impersonation. With an actor token, the new token can name both the subject and the acting party, which is delegation.

Can a public client use token exchange?

The caller should be a confidential client that authenticates, which means a server. A browser or mobile app should not exchange tokens.

What does issued_token_type mean in the response?

It says what kind of token the server issued, as a token type URN. RFC 8693 requires it in every successful exchange response, because the caller may not get exactly what it requested.

Does token exchange return a refresh token?

It may. RFC 8693 makes `refresh_token` optional in the response, and the server decides. When an exchanged access token expires, the simplest path is to exchange a fresh subject token again.

Why should I not just forward the user’s access token?

A forwarded token works at every service that accepts it, so one leak exposes all of them. An exchanged token is limited to one audience and can carry less scope.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.