How can I get userId after call create user api in keycloak?

Viewed 1144

I implemented keycloak in my node.js project and call following API for add user in keycloak:

{{keycloak_url}}/admin/realms/{{realm}}/users

This API works and I can add user in keycloak but I need userId in response to this API how can I get this, any alternative way for this

Thanks in advance

2 Answers

I belive there is another way to do this which will save you an extra request. If you take a look at the respoonse headers when you create a user, you should find a field named "Location". It looks like this:

Location: http://keycloak_address/auth/admin/realms/realm/users/3a11cc77-9871-4f6e-805b-bf17ea79fa3a

In this case the value "3a11cc77-9871-4f6e-805b-bf17ea79fa3a" would be the new user's id.

I know it's been a while sice you asked the question but I hope it helps someone.

If I understand correctly, you just need to use Keycloak Admin REST API, namely the endpoint:

GET /{realm}/users

with the query parameter username. For instance:

GET "{{keycloak_url}}/admin/realms/{{realm}}/users/?username={{username}}"

NOTE: It will return all the users with a username that matches {{username*}}. Therefore, additional filtering of the list might be necessary. For those using bash script I have uploaded to my repo one example on how to do filter currently.

Related