How to Modify the User Profile Section in Realm Settings via Script (Keycloak 26.0.7)?

Hello everyone,

I’m working on automating Keycloak configuration (version 26.0.7), and I need to add custom attributes in the User Profile section under realm settings.

The goal is to ensure that when a user is created, they automatically have these attributes available.

  • Is there an API endpoint or configuration file where I can define these attributes programmatically?
  • Has anyone successfully automated this step in a script?

Any guidance, API references, or example scripts would be greatly appreciated!

Thanks in advance! :blush:

how you are importing a realm?
If you are using JSON file there is a part of it:

"org.keycloak.userprofile.UserProfileProvider": [
  {
    "id": "4acb908b-be01-4261-99cb-b400d347ae5b",
    "providerId": "declarative-user-profile",
    "subComponents": {},
    "config": {
      "kc.user.profile.config": [
        "{\"attributes\":[{\"name\":\"username\",\"displayName\":\"${username}\",\"validations\":{\"length\":{\"min\":3,\"max\":255},\"username-prohibited-characters\":{},\"up-username-not-idn-homograph\":{}},\"permissions\":{\"view\":[\"admin\",\"user\"],\"edit\":[\"admin\",\"user\"]},\"multivalued\":false},{\"name\":\"email\",\"displayName\":\"${email}\",\"validations\":{\"email\":{},\"length\":{\"max\":255}},\"required\":{\"roles\":[\"user\"]},\"permissions\":{\"view\":[\"admin\",\"user\"],\"edit\":[\"admin\",\"user\"]},\"multivalued\":false},{\"name\":\"firstName\",\"displayName\":\"${firstName}\",\"validations\":{\"length\":{\"max\":255},\"person-name-prohibited-characters\":{}},\"required\":{\"roles\":[\"user\"]},\"permissions\":{\"view\":[\"admin\",\"user\"],\"edit\":[\"admin\",\"user\"]},\"multivalued\":false},{\"name\":\"lastName\",\"displayName\":\"${lastName}\",\"validations\":{\"length\":{\"max\":255},\"person-name-prohibited-characters\":{}},\"required\":{\"roles\":[\"user\"]},\"permissions\":{\"view\":[\"admin\",\"user\"],\"edit\":[\"admin\",\"user\"]},\"multivalued\":false}],\"groups\":[{\"name\":\"user-metadata\",\"displayHeader\":\"User metadata\",\"displayDescription\":\"Attributes, which refer to user metadata\"}],\"unmanagedAttributePolicy\":\"ENABLED\"}"
      ]
    }
  }
]

or you can try partial import and just paste the code but maybe you will need to add whole object “components”:

and there may be a endpoint for it: