Skip to content

Refresh token request builder for Keycloak

Build the request that exchanges a refresh token for new tokens at the token endpoint, as curl or raw HTTP.

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.

A placeholder is filled in. Paste yours; it stays in your browser.

Only to narrow the scopes. It cannot add scopes the original grant did not have.

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

Refresh request

curl command
curl 'https://auth.example.com/realms/acme/protocol/openid-connect/token' \
  -H 'Authorization: Basic d2ViLWFwcDpzYW1wbGUtc2VjcmV0LW5vdC1yZWFs' \
  --data-urlencode 'grant_type=refresh_token' \
  --data-urlencode 'refresh_token=SAMPLE_REFRESH_TOKEN'
Use the same client
A refresh token belongs to the client it was issued to. A confidential client must authenticate again; a public client sends its client_id.
Store the new refresh token
Keycloak returns a new refresh token with each refresh. Keep the newest one. With the realm setting Revoke refresh token on, each refresh token works only once, unless Refresh token max reuse allows more.
invalid_grant on refresh
The refresh token expired, the user’s session ended, or the token was already used while rotation is on. Send the user through sign-in again.

How to use it

  1. Enter your Keycloak server URL and realm, or a custom token endpoint.
  2. Paste the refresh token.
  3. Set the client authentication the same way as for the original grant.
  4. Copy the request, and store the new refresh token from the response.

What to know

A refresh request is a POST with grant_type=refresh_token and the refresh token. It must come from the client the token was issued to.

Keycloak returns a new refresh token with each refresh; keep the newest one. With the realm setting Revoke refresh token on, each refresh token can be used only once, unless Refresh token max reuse allows more.

A refresh token stops working when it expires or when the user’s session ends. An offline token, from the offline_access scope, keeps working after the session ends.

What a refresh token is for

Access tokens are meant to be short-lived. A stolen access token is useful to an attacker only until it expires, so servers keep that window small. The catch is that the user should not have to sign in every few minutes.

A refresh token solves this. It is a longer-lived credential that the client keeps and sends only to the token endpoint, never to an API. When the access token runs out, the client trades the refresh token for a new one. RFC 6749 defines the refresh grant in section 6.

The refresh token is not shown to resource servers, and the user does not see the exchange. To the user, the session simply keeps working.

How the refresh grant works

The client sends a POST to the token endpoint with grant_type=refresh_token and the token. A confidential client authenticates the same way it did for the original grant. A public client sends its client_id.

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 d2ViLWFwcDpzYW1wbGUtc2VjcmV0LW5vdC1yZWFs

grant_type=refresh_token&refresh_token=SAMPLE_REFRESH_TOKEN

The response has the same shape as the first token response: a new access_token, its expires_in, and often a new refresh_token. If the original request had the openid scope, a new ID token may come back as well.

Sample response (made-up values)
{
  "access_token": "SAMPLE_NEW_ACCESS_TOKEN",
  "token_type": "Bearer",
  "expires_in": 300,
  "refresh_token": "SAMPLE_NEW_REFRESH_TOKEN",
  "scope": "openid profile email"
}

The optional scope parameter

You may send scope to ask for less than the original grant allowed. RFC 6749 says the requested scope must not include anything the user did not grant before. It can narrow a token for one API call; it cannot widen it.

What this tool does and does not do

The builder writes the refresh request for you as a curl command and as raw HTTP. It handles the endpoint, the grant parameters and client authentication.

  • It builds the Keycloak token endpoint from a server URL and realm, or takes a custom URL.
  • It supports public clients, client_secret_basic, client_secret_post and a pasted signed JWT.
  • It adds scope only when you fill it in.

It does not send the request and cannot tell whether your refresh token is still valid. It does not decode the token either. Keycloak refresh tokens are JWTs you can read with the JWT decoder, but other servers may issue opaque strings.

How to use the builder

  1. Enter your Keycloak server URL and realm, or a custom token endpoint.
  2. Paste the refresh token in place of the sample.
  3. Narrow the scope if you need to; leave it empty otherwise.
  4. Pick the same client authentication your app used for the original grant.
  5. Copy the curl command, run it, and keep the new refresh token from the response.

Where refresh tokens fit

Server-side web apps

A confidential client keeps the refresh token on the server, next to the user’s session. When an API call fails because the access token expired, the server refreshes and retries. The browser never sees either token.

Single-page apps

A browser app is a public client. RFC 9700 says refresh tokens for public clients must be sender-constrained or rotated, so a stolen one cannot be reused quietly. Keep the token in memory, and consider a back end for front end that holds tokens on a server instead.

Mobile and desktop apps

Native apps keep the refresh token in the platform’s secure storage, such as the iOS Keychain or Android Keystore-backed storage. Refreshing lets the app stay signed in across launches without asking for the password again.

Offline access

Some apps need to act for the user while the user is away, such as a sync job. The offline_access scope asks for a refresh token that keeps working after the user’s session ends. Request it only when you truly need it.

How Keycloak handles refresh tokens

In Keycloak the refresh request goes to https://<host>/realms/<realm>/protocol/openid-connect/token, the same endpoint as the first token request.

  • Keycloak returns a new refresh token with each refresh. Keep the newest one.
  • A refresh token is tied to the user’s session. When the session ends, by idle timeout, maximum lifetime or logout, the refresh token stops working.
  • With the realm setting Revoke refresh token on, each refresh token can be used only once, unless Refresh token max reuse allows more.
  • An offline token, from the offline_access scope, survives the end of the session. The user needs the offline_access role, which new users get by default.
  • Keycloak refresh tokens are JWTs with typ set to Refresh. Treat them as secrets all the same.

When to refresh

There are two common strategies, and many apps use both.

Before the token expires

The token response says how long the access token lasts in expires_in. Record the time you received it and refresh a little before it runs out, for example when less than a tenth of the lifetime is left. This avoids failed API calls, but it refreshes even when the user is idle.

When an API says the token expired

An API that rejects an expired token answers 401 with error="invalid_token" in the WWW-Authenticate header (RFC 6750). Refresh once, then retry the call once. If the retry also fails, the problem is not expiry, so stop and surface the error.

Clock differences

Do not compare the token’s exp claim with your own clock alone. A few seconds of drift between machines is normal. Working from expires_in and a margin is more reliable.

Rotation and reuse

Refresh token rotation means every refresh returns a new refresh token and the old one stops working. If an attacker steals a token and uses it, the real client’s next refresh fails, or the other way around. Either way the theft shows up.

With rotation on, two refreshes in parallel can break each other: both send the same old token, and one of them gets invalid_grant. Serialize refreshes in your app, for example behind a single in-flight promise or a lock.

Common errors and how to fix them

ErrorUsual causeFix
invalid_grant, Token is not activeThe refresh token expired.Send the user through sign-in again.
invalid_grant, Session not activeThe session behind the token ended: idle timeout, maximum lifetime or logout.Sign in again. Raise session timeouts if users hit this too often.
invalid_grant after a retryRotation is on and the token was already used.Store the new token from every response and avoid parallel refreshes.
invalid_clientWrong client id or secret, or no client authentication sent.Refresh with the client the token was issued to, using its current secret.
invalid_scopeThe scope asks for more than the original grant.Leave scope out or ask only for a subset.

The OAuth error lookup covers the rest of the codes.

Security notes

RFC 6749 section 6 says a refresh token belongs to the client it was issued to, and the server must check that. A confidential client authenticates on every refresh.

RFC 9700 asks servers to protect refresh tokens for public clients with sender-constraining, such as DPoP, or with rotation. It also suggests that refresh tokens can expire when they have not been used for some time.

Never send a refresh token to an API, never log it, and never put it in a URL.

Best practices

  • Refresh shortly before the access token expires, or when an API answers 401.
  • Replace the stored refresh token with each new one.
  • Make one refresh at a time per session.
  • Ask for offline_access only for real background work.
  • On invalid_grant, stop retrying and start a fresh sign-in.
Questions

Frequently asked questions

How do I refresh an access token?

POST `grant_type=refresh_token` and the `refresh_token` to the token endpoint, with the same client authentication as the original grant. The response holds a new access token and usually a new refresh token.

Why does my refresh fail with invalid_grant?

The refresh token expired, the session behind it ended, the user logged out, or the token was already used while rotation is on. Stop retrying and send the user through sign-in again.

Does Keycloak return a new refresh token on refresh?

Yes. Keycloak returns a new refresh token with each refresh. Keep the newest one, especially when Revoke refresh token is on in the realm.

Can I add scopes when refreshing a token?

No. The `scope` parameter on a refresh can only narrow what the original grant allowed. To get more, run a new authorization request.

What is the difference between a refresh token and an offline token?

A regular refresh token stops working when the user’s session ends. An offline token, requested with `offline_access`, keeps working after that. Use offline tokens only for background work.

Can a single-page app use refresh tokens?

Yes, as a public client, if the server rotates them or binds them to a key. Keep them in memory, not in localStorage. A back end for front end that keeps tokens on a server is safer still.

Should I store a refresh token in localStorage?

No. Any script on the page can read localStorage, so a single cross-site scripting bug leaks the token. Keep it in memory in a browser app, or keep it on a server and give the browser only a session cookie.

How long does a Keycloak refresh token last?

It lasts as long as the user’s session allows, set by the realm’s session idle and maximum timeouts. The `exp` claim in the decoded refresh token shows its current expiry.

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.