Is there a way to retrieve the number of members in a group using the Keycloak API?

4.6k views Asked by At

I am using Keycloak API and I have a use case where I need to get the number of members in a group, before fetching all users.

Unfortunately, both endpoints GET /{realm}/groups/{id}/members and GET /{realm}/groups/{id} do not return such information.

The API doc on https://www.keycloak.org/docs-api/5.0/rest-api/index.html does not seem to indicate how we can get the count.

So, how we can get this information using the Keycloak API ?

2

There are 2 answers

2
dreamcrash On BEST ANSWER

Unfortunately, one does not have an endpoint like GET /{realm}/groups/{id}/members/count like one has for the number of groups (i.e., GET /{realm}/groups/count).

What you would need to do, is to define max query parameter to -1 so that you are sure to get all the members using the endpoint GET /{realm}/groups/{id}/members and then just count the number of members on the json response. Of course, this is a sub-optimal approach, but the only one using the API as it is.


I have provided a complete script that automatizes this processes in the following repo.

0
Abdennour TOUMI On

As mentioned by @dreamcrash, you need to count that from the response of GET /{realm}/groups/{id}/members.

I am using jq.. then my script is:

curl  .... ${url}/${realm}/groups/${id}/members | jq '. | length'