Skip to content

Keycloak Admin REST API curl builder

Build curl commands for the Keycloak Admin REST API: get an admin token, then find, create and change users, clients, roles and groups.

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

Request

A made-up server and realm are filled in.

Sign in as

A confidential client in acme with service accounts on and roles from realm-management.

curl

1. Get an access token
TOKEN=$(curl -s -X POST https://auth.example.com/realms/acme/protocol/openid-connect/token \
  -d grant_type=client_credentials \
  -d client_id=admin-api \
  --data-urlencode "client_secret=$CLIENT_SECRET" | jq -r .access_token)
2. Call the admin API
curl -s -X GET 'https://auth.example.com/admin/realms/acme/users?username=alice&exact=true' \
  -H "Authorization: Bearer $TOKEN"
Endpoint
GET /admin/realms/acme/users
Role needed
realm-management › view-usersassign it to the service account
Read CLIENT_SECRET into your shell first (for example read -rs CLIENT_SECRET), so it stays out of your shell history. Admin tokens are short-lived (a few minutes by default); get a new one when calls start to answer 401. The commands use jq to read the token.

How to use it

  1. Enter your server and realm.
  2. Choose a service account or an admin user to sign in with.
  3. Pick the action and fill in the names or ids it needs.
  4. Run the token command, then the API call.

What to know

The Admin REST API lives under /admin/realms/<realm> and takes a bearer token. For scripts, a confidential client with a service account in the same realm is the cleaner choice: give it only the realm-management roles it needs.

Many calls take internal ids rather than names. Look the user, client or group up first, then use the id from the answer in the path.

Admin tokens are short-lived. A script that runs for long should get a new token when calls start to answer 401.

What the Keycloak Admin REST API is

Everything you can do in the Keycloak admin console goes through the Admin REST API. The console is a web app that calls it. That means any change you make by hand, such as creating a user, adding a client or giving someone a role, can also be scripted.

The API lives under /admin/realms/<realm> on your Keycloak server. Each call needs a bearer token in the Authorization header. The token decides what the caller may do, through roles of the realm-management client in that realm.

In production, the API is how teams keep realms in step across environments, onboard users in bulk, and connect Keycloak to their own systems. It is also how incident scripts sign a user out everywhere or reset a password without anyone clicking through the console.

How a call works in Keycloak 26

An admin API call is always two steps. First you get an access token from a realm’s token endpoint. Then you send that token with the request to the admin endpoint.

Step one: get a token

There are two common ways to get a token. A service account uses the client credentials grant with a confidential client in the realm you want to manage. An admin user uses the password grant with the admin-cli client in the master realm.

Client credentials token for the sample realm acme
TOKEN=$(curl -s -X POST https://auth.example.com/realms/acme/protocol/openid-connect/token \
  -d grant_type=client_credentials \
  -d client_id=admin-api \
  --data-urlencode "client_secret=$CLIENT_SECRET" | jq -r .access_token)

Step two: call the endpoint

With the token in a shell variable, each call is a plain HTTP request. Reads use GET. Creates use POST and answer 201 with the new object’s id at the end of the Location header. Updates use PUT, and deletes use DELETE.

Find a user by exact username
curl -s 'https://auth.example.com/admin/realms/acme/users?username=alice&exact=true' \
  -H "Authorization: Bearer $TOKEN"

Paths changed after Keycloak 17

Older WildFly-based versions served everything under /auth, so the admin API was at /auth/admin/realms/<realm>. Current versions drop that prefix by default. If your server still sets http-relative-path=/auth, keep the prefix in every URL.

What this tool does

The builder writes the two curl commands for you. You enter the server address and realm, choose how to sign in, and pick an action. It fills in the method, path and JSON body, and shows the realm-management role the call needs.

  • Users: find by username, search, count, create, set a temporary password, email required actions, list sessions, sign out everywhere, delete.
  • Clients: find by client id, create a public client with PKCE, read a client secret.
  • Roles and groups: list and create realm roles, give a user a realm role, list and create groups, add a user to a group.

It runs in your browser and never sends a request. It does not hold your secret either: the commands read it from a shell variable you set yourself.

What it does not do

It does not call your server, so it cannot look up ids for you. Paths that need an internal id, such as a user or group id, show a placeholder you replace after a lookup. It also covers the common calls only, not every endpoint the API has.

How to use it

  1. Enter your public Keycloak address and the realm you want to manage.
  2. Choose Service account or Admin user (master). For scripts, prefer the service account.
  3. Pick the action and fill in the username, client id, role or group it needs.
  4. Read the secret into your shell with read -rs CLIENT_SECRET, so it stays out of your history.
  5. Run the token command, then the API call. Repeat the token command when calls start to answer 401.

Setting up a service account for scripts

A service account is the cleaner way to automate. It is a confidential client with Service accounts turned on and the standard flow turned off. It signs in as itself, so there is no human password in your pipeline.

  1. Create a confidential OpenID Connect client in the realm, for example admin-api.
  2. Turn on Service accounts roles and turn off the flows it does not need.
  3. On its Service account roles tab, assign roles from the realm-management client, such as view-users or manage-users.
  4. Copy the secret from the Credentials tab into your secret store.

Grant the smallest set of roles that does the job. A sync script that only reads users needs view-users, not realm-admin.

Where it pays off

The admin API is most useful wherever a change has to happen the same way more than once.

First production deploy

A new realm usually needs the same roles, groups and a first set of users. A short script of admin API calls makes that repeatable, so staging and production start from the same shape.

Automation and CI

Pipelines can create test users, register clients for preview environments and clean them up afterwards. A service account with narrow roles keeps those jobs from being able to change anything else.

Support and incidents

When an account may be compromised, listing a user’s sessions and signing them out everywhere takes two calls. Setting a temporary password or emailing an UPDATE_PASSWORD action is one more.

Moving between environments

Realm exports move a whole realm at once. The admin API is better for small, targeted changes, such as adding one client to every environment without touching anything else.

Common errors and how to fix them

ResponseUsual causeFix
401 UnauthorizedNo token, an expired token, or the token is from another server address.Get a fresh token. Check the token’s iss matches the server you call.
403 ForbiddenThe token is valid but lacks the role for this call.Assign the needed realm-management role to the service account or user.
404 Not FoundWrong realm name, wrong id, or an old /auth path.Check the realm and id. Drop /auth unless your server still uses it.
409 ConflictSomething with the same name already exists.Look it up first, then update instead of create.
invalid_client on the token callWrong client id or secret.Check the secret and that client authentication is on.
unauthorized_client on the token callService accounts are off for that client.Turn on Service accounts roles for the client.

A token from the master realm can manage other realms when the user has the right roles there. A service account token from realm acme can only manage acme. Mixing these up is a common source of 403 errors.

Security notes

  • Treat an admin token like a password. Anyone holding it can act as the account until it expires.
  • Keep secrets in shell variables or a secret store, not in scripts or command history.
  • Prefer a service account per job over one shared admin user. You can then revoke one job without breaking the others.
  • Do not expose the admin API on a public address if no one outside your network needs it. A reverse proxy can pass only /realms/ and /resources/.
  • Turn on admin events so changes made through the API are recorded with who made them.

Best practices

  • Look up by name, then act by id. Names are what people know; ids are what the API needs.
  • Make scripts safe to run twice: check whether a user, role or client exists before creating it.
  • Use exact=true when you search for a username, or a search for ann also matches joanne.
  • Page through large lists with first and max instead of fetching everything at once.
  • Get a new token when one expires rather than raising the access token lifespan for every client in the realm.

The commands use jq to read the token from the JSON answer. Install it, or read access_token with any JSON tool you prefer.

Questions

Frequently asked questions

What is the Keycloak admin API base URL?

It is `https://<host>/admin/realms/<realm>`. Versions before 17, and servers that still set `http-relative-path=/auth`, use `https://<host>/auth/admin/realms/<realm>`.

How do I get an access token for the Keycloak admin API?

Use the client credentials grant with a confidential client that has a service account and `realm-management` roles. Or use the password grant with the `admin-cli` client in the master realm. Both call the realm’s `/protocol/openid-connect/token` endpoint.

Why does the Keycloak admin API return 403 Forbidden?

The token is valid but does not carry the role the call needs. Assign the matching role of the `realm-management` client, such as `view-users` for reads or `manage-users` for changes, then get a new token.

Why does the admin API return 401 after a few minutes?

Admin tokens are short-lived access tokens. Once one expires, every call answers 401. Request a new token and retry; long scripts should do this whenever they see a 401.

How do I get the id of a user I just created?

A successful create answers 201 with a `Location` header that ends in the new id. Run curl with `-i` to see the header. You can also look the user up by username with `exact=true`.

Which roles does a service account need to manage users?

`view-users` to read users and `manage-users` to create, change or delete them. Both are roles of the `realm-management` client in the same realm. Give the account only what its job needs.

Can one token manage several realms?

A user in the master realm can be given roles that cover other realms, so its token can manage them. A service account in a regular realm can only manage that realm.

Is it safe to build admin API commands in this tool?

The builder runs in your browser and sends nothing. It never asks for your secret: the commands read it from a shell variable you set. Treat the tokens you get as secrets all the same.

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.