Register user using Rest Api in keycloak

I want to know how can I register user using rest api.
I am calling api http://localhost:8080/auth/realms/test/users to register user.
but I am not able to get response.

You can create a new user using the REST API. Documentation is here: Keycloak Admin REST API

Your path looks wrong. Should be http://localhost:8080/auth/admin/realms/test/users

Thanks for the quick response.
However I tried running the api http://localhost:8080/auth/admin/realms/test/users in postman. I am getting error as below.
{

"error": "unknown_error"

}

Looks right. A few things:

  1. try with header accept: application/json
  2. try with a minimal user, as it may not like you setting fields that get set automatically. E.g. {"enabled":true,"attributes":{},"groups":[],"email":"jdoe@example.com","emailVerified":"","firstName":"John","lastName":"Doe"}
  3. check your server logs

Thank you xgp :star_struck:

it worked perfect. Included username in data and done :clap:
Thanks :slight_smile:

user got created:

1 Like

How do I register using password in this data . I need password to login, but when I create user mentioning password, but i am getting below error saying ‘password policy not met’.

I don’t know if it works in the same request as the user creation. I’ve never tried that. I use:
path:
/auth/admin/realms/<realm>/users/<user-id>/reset-password
payload:
{"type":"password","value":"johndoe","temporary":false}

Also, that’s a legitimate error if you have a password policy set on your realm. Check in the “Authentication”->“Password Policy” page of the admin.

It is working…the issue got resolved.
I have passed credentials this way
“credentials”: [{
“type”: “password”,
“value”: “Johndoe@1234”,
“temporary”: false
}]

It worked.
But I am facing cors error.
The resources that I have passed looks correct.
Not sure what might be causing the cors error.
Can you suggest anything on it?

Hi,

I have registered user using rest api /users.
But the saved data is invalid for login.
Can you suggest something about why it is Invalid when I try to login with the registered user ?

Hola Estoy creando usuario con keycloak desde la api en c# y este es mi metodo:

Primero me logeo con un usuario admin de mi reino y obtengo el token.

luego esto es para crear un usuario.

    private static ResponseBase<UsuarioModel> _responseBase = new();
    private static RestClient cliente = new RestClient();
    private List<UsuarioModel> entities = new List<UsuarioModel>();
    private UsuarioModel entity = new UsuarioModel();

    #region Contruct
    public UsuariosRepositorio()
    {
        cliente = new RestClient("http://localhost:8080/auth");
        cliente.Options.MaxTimeout = -1;
        entities = new List<UsuarioModel>();
        entity = new UsuarioModel();
    });

    #endregion

    public ResponseBase<UsuarioModel> Insert(string? token, UsuarioModel model)
    {
        _responseBase = new ResponseBase<UsuarioModel>();
        try
        {
            string jsonData = JsonConvert.SerializeObject(model, Formatting.Indented);

            var request = new RestRequest("/admin/realms/ircnl-psi-realm/users", Method.Post);
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Authorization", "Bearer " + token);
            request.AddParameter("application/json", jsonData, ParameterType.RequestBody);
            RestResponse response = cliente.Execute(request);

            _responseBase.Entities = new List<UsuarioModel>(); 
            _responseBase.IsSuccess = true;
            _responseBase.MsgType = AlertTypeEnum.success.ToString();
            _responseBase.Message = "Listo";
        }
        catch (Exception ex)
        {
            _responseBase.IsSuccess = false;
            _responseBase.MsgType = AlertTypeEnum.error.ToString();
            _responseBase.Message = ex.Message.ToString();
        }
        return _responseBase;
    }

y hasta aca unciona perfecto!!

esto es para crear un usuario con un token de permiso.
Mi pregunto como hacerle para que dentro mi aplicacion una persona publica pueda registrarse sin que hay que enviarle un token.
Alguien tiene la solución para esto?