2016-05-11 11:26:03 +00:00
== Admin REST API
2016-04-18 21:02:18 +00:00
2017-08-28 12:50:14 +00:00
{project_name} comes with a fully functional Admin REST API with all features provided by the Admin Console.
2016-04-18 21:02:18 +00:00
2022-03-23 14:34:07 +00:00
To invoke the API you need to obtain an access token with the appropriate permissions. The required permissions are described in the Server Administration Guide.
2016-04-18 21:02:18 +00:00
2022-03-23 14:34:07 +00:00
You can obtain a token by enabling authentication for your application using {project_name}; see the Securing Applications and Services Guide. You can also use direct access grant to obtain an access token.
2016-05-11 11:26:03 +00:00
2021-06-23 18:10:41 +00:00
=== Examples of using CURL
2019-11-13 18:25:30 +00:00
2022-03-23 14:34:07 +00:00
==== Authenticating with a username and password
2016-05-11 11:26:03 +00:00
2021-06-23 18:10:41 +00:00
.Procedure
2022-03-23 14:34:07 +00:00
. Obtain an access token for the user in the realm `master` with username `admin` and password `password`:
2021-06-23 18:10:41 +00:00
+
2022-03-23 14:34:07 +00:00
[source,bash,subs=+attributes]
2016-05-13 08:28:41 +00:00
----
curl \
-d "client_id=admin-cli" \
-d "username=admin" \
2016-05-31 18:39:12 +00:00
-d "password=password" \
2016-05-13 08:28:41 +00:00
-d "grant_type=password" \
2022-02-08 13:07:16 +00:00
"http://localhost:8080{kc_realms_path}/master/protocol/openid-connect/token"
2016-05-13 08:28:41 +00:00
----
2021-06-23 18:10:41 +00:00
+
2016-05-13 08:28:41 +00:00
NOTE: By default this token expires in 1 minute
2021-06-23 18:10:41 +00:00
+
The result will be a JSON document.
2016-05-11 11:26:03 +00:00
2021-06-23 18:10:41 +00:00
. Invoke the API you need by extracting the value of the `access_token` property.
2016-04-18 21:02:18 +00:00
2021-06-23 18:10:41 +00:00
. Invoke the API by including the value in the `Authorization` header of requests to the API.
+
2016-05-13 08:28:41 +00:00
The following example shows how to get the details of the master realm:
2021-06-23 18:10:41 +00:00
+
2022-02-08 13:07:16 +00:00
[source,bash,subs="attributes+"]
2016-05-13 08:28:41 +00:00
----
curl \
-H "Authorization: bearer eyJhbGciOiJSUz..." \
2022-02-08 13:07:16 +00:00
"http://localhost:8080{kc_admins_path}/realms/master"
2016-05-13 08:28:41 +00:00
----
2016-05-11 11:26:03 +00:00
2022-03-23 14:34:07 +00:00
==== Authenticating with a service account
To authenticate against the Admin REST API using a `client_id` and a `client_secret`, perform this procedure.
2019-11-13 18:25:30 +00:00
2022-03-23 14:34:07 +00:00
.Procedure
. Make sure the client is configured as follows:
2019-11-13 18:25:30 +00:00
* `client_id` is a **confidential** client that belongs to the realm *master*
* `client_id` has `Service Accounts Enabled` option enabled
* `client_id` has a custom "Audience" mapper
** Included Client Audience: `security-admin-console`
2022-03-23 14:34:07 +00:00
. Check that `client_id` has the role 'admin' assigned in the "Service Account Roles" tab.
2019-11-13 18:25:30 +00:00
2022-02-08 13:07:16 +00:00
[source,bash,subs="attributes+"]
2019-11-13 18:25:30 +00:00
----
curl \
-d "client_id=<YOUR_CLIENT_ID>" \
-d "client_secret=<YOUR_CLIENT_SECRET>" \
-d "grant_type=client_credentials" \
2022-02-08 13:07:16 +00:00
"http://localhost:8080{kc_realms_path}/master/protocol/openid-connect/token"
2019-11-13 18:25:30 +00:00
----
2017-08-28 12:50:14 +00:00
ifeval::[{project_community}==true]
2016-05-11 11:26:03 +00:00
=== Example using Java
There's a Java client library for the Admin REST API that makes it easy to use from Java. To use it from your application add a dependency on the
2016-05-13 08:28:41 +00:00
`keycloak-admin-client` library.
2016-05-11 11:26:03 +00:00
The following example shows how to use the Java client library to get the details of the master realm:
2022-02-08 13:07:16 +00:00
[source,java,subs="attributes+"]
2016-05-13 08:28:41 +00:00
----
2021-06-23 18:10:41 +00:00
2016-05-11 11:26:03 +00:00
import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.RealmRepresentation;
...
Keycloak keycloak = Keycloak.getInstance(
2022-02-08 13:07:16 +00:00
"http://localhost:8080{kc_base_path}",
2016-05-31 18:39:12 +00:00
"master",
"admin",
"password",
2016-05-11 11:26:03 +00:00
"admin-cli");
RealmRepresentation realm = keycloak.realm("master").toRepresentation();
2016-05-13 08:28:41 +00:00
----
2016-05-11 11:26:03 +00:00
2017-08-28 12:50:14 +00:00
Complete Javadoc for the admin client is available at {apidocs_link}[{apidocs_name}].
2019-11-13 18:25:30 +00:00
endif::[]
2021-06-23 18:10:41 +00:00
2022-03-23 14:34:07 +00:00
=== Additional resources
[role="_additional-resources"]
* {adminguide_link}[{adminguide_name}]
* {adapterguide_link}[{adapterguide_name}]
* {apidocs_link}[{apidocs_name}]