Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Some Keycloak client examples
import static java.util.Arrays.asList;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
public class KeycloakAdminClientExample {
public static void main(String[] args) throws Exception {
Keycloak kc = KeycloakBuilder.builder() //
.serverUrl("http://localhost:8081/auth") //
.username("rest-user-admin") //
.password("password") //
.clientId("admin-cli") //
.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()) //
CredentialRepresentation credential = new CredentialRepresentation();
UserRepresentation user = new UserRepresentation();
// Create testuser
Response result = kc.realm("rest-example").users().create(user);
if (result.getStatus() != 201) {
System.err.println("Couldn't create user.");
System.out.println("Testuser created.... verify in keycloak!");
System.out.println("Press any key...");;
// Delete testuser
String locationHeader = result.getHeaderString("Location");
String userId = locationHeader.replaceAll(".*/(.*)$", "$1");
<project xmlns="" xmlns:xsi=""
Copy link

harshmahey commented Sep 2, 2016

When i trying this , i am getting the error ," Could not find artifact org.keycloak:keycloak-admin-client:jar:1.8.0.CR1-SNAPSHOT -> [Help 1]
I also tried other higher version and it starts failing on resteasy.

Copy link

rockydcoder commented Mar 16, 2017

@harshmahey you should not use a SNAPSHOT version in your pom. Use a release version like 2.5.4.Final

Copy link

thomasdarimont commented Sep 27, 2017

Note that the rest-user-admin user needs to have the "manage-user" (and potentially view-clients) role for the realm-management client.

Copy link

alvaroqv commented Dec 13, 2017

@thomasdarimont thanks for this client example. Do you now how can I send email when user is created into keycloak? Keycloak should send a automatic email to the new user, right?

Copy link

alvaroqv commented Dec 13, 2017

Hi @thomasdarimont thanks, I'm using userRessource.get(userId).executeActionsEmail to send the email. I don't know if it is the best solution but it is working.

Copy link

amit-tarento commented Feb 27, 2018

Hi @thomasdarimont thanks for client example. Do you know , how to get the exact validation message in failed user creation like email already exist, or username already exist or password pattern does not match.

Copy link

maherBoudhraa commented Mar 22, 2018

Hello @thomasdarimont thanks for this client example. When i try to run it i get a HTTP 400 Bad Request can you help me ??

Copy link

chittaa commented Apr 26, 2018

I was receiving jackson error while running this sample. It got resolved when i used - <artifactId/>resteasy-jackson2-provide</artifactId>
Kc version - 3.4.3.Final

Copy link

Stalluri1 commented May 8, 2019

hey this is giving me this error - (default task-1) Uncaught server error: java.lang.IllegalArgumentException: interface org.keycloak.admin.client.token.TokenService is not visible from class loader
any help ?

Copy link

charStarGoulash commented May 24, 2019

Is there anyway to use the Client API not the REST ADMIN API and still be able to use a JSON config to create a new realm as we would with a POST to the RestAdminAPI?

Copy link

VenkateshGuntuk commented Jul 12, 2019

Realm Role not getting created....i think user.setRealmRole("rolename") not working

Copy link

thomasdarimont commented Jul 12, 2019

Take a look at this example:
Roles need to be set explicitly.

Copy link

canattofilipe commented Jun 12, 2020

It runs outside server ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment