DEV Community

rounakcodes
rounakcodes

Posted on • Edited on

1 1

Keycloak: REST API for managing users

While Keycloak is well documented, I found it really hard to get started. Hence, these notes.
admin-cli is a default client in a realm. We use that as the client_id to get an access token.

Get access token

curl --request POST 'http://localhost:8080/auth/realms/<realm-name>/protocol/openid-connect/token' \
    --header 'Content-Type: application/x-www-form-urlencoded' \
    --data-urlencode 'username=<username>' \
    --data-urlencode 'password=<password>' \
    --data-urlencode 'grant_type=password' \
    --data-urlencode 'client_id=admin-cli'
Enter fullscreen mode Exit fullscreen mode

Get users

 curl --request GET 'http://localhost:8080/auth/admin/realms/<realm-name>/users' \
        --header 'Authorization: Bearer <access-token>'
Enter fullscreen mode Exit fullscreen mode

Create a user

 curl --request POST 'http://localhost:8080/auth/admin/realms/<realm-name>/users' \
      --header 'Content-Type: application/json' \
      --header 'Authorization: Bearer <access-token>' \
      --data-raw '{"username":"<username>"}'
Enter fullscreen mode Exit fullscreen mode

Set user password (use the Get users API to get user id)

 curl --request PUT 'http://localhost:8080/auth/admin/realms/<realm-name>/users/<user-id>/reset-password'
       --header "Content-Type: application/json"
       --header "Authorization: Bearer <access-token>"
       --data-raw '{"type":"password", "value":"<password>", "temporary": false}'
Enter fullscreen mode Exit fullscreen mode

Send verify email

Pre-requisite: Configure Realm Email Settings)

curl --request PUT 'http://localhost:8080/auth/admin/realms/<realm-name>/users/<user-id>/send-verify-email' \
--header 'Authorization: Bearer <access-token>'
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more