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.
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.
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
- Enter your public Keycloak address and the realm you want to manage.
- Choose Service account or Admin user (master). For scripts, prefer the service account.
- Pick the action and fill in the username, client id, role or group it needs.
- Read the secret into your shell with
read -rs CLIENT_SECRET, so it stays out of your history. - 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.
- Create a confidential OpenID Connect client in the realm, for example
admin-api. - Turn on Service accounts roles and turn off the flows it does not need.
- On its Service account roles tab, assign roles from the
realm-managementclient, such asview-usersormanage-users. - 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
| Response | Usual cause | Fix |
|---|---|---|
| 401 Unauthorized | No 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 Forbidden | The token is valid but lacks the role for this call. | Assign the needed realm-management role to the service account or user. |
| 404 Not Found | Wrong realm name, wrong id, or an old /auth path. | Check the realm and id. Drop /auth unless your server still uses it. |
| 409 Conflict | Something with the same name already exists. | Look it up first, then update instead of create. |
invalid_client on the token call | Wrong client id or secret. | Check the secret and that client authentication is on. |
unauthorized_client on the token call | Service 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=truewhen you search for a username, or a search forannalso matchesjoanne. - Page through large lists with
firstandmaxinstead 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.