How to get first name and last name from Keycloak's REST API?

I use this curl command to get a token:

curl \
  -d "client_id=admin-cli" \
  -d "username=admin" \
  -d "password=admin_password" \
  -d "grant_type=password" \
  "https://example.com/auth/realms/master/protocol/openid-connect/token"

Then I use this curl command to get the list of users for my Development realm:

  curl \
  -H "Authorization: bearer token" \
  "https://example.com/auth/admin/realms/Development/users"

And it returns a list of users. This is the response JSON for a sample user:

{
    "id": "9ca4216f-d13e-49f9-8c35-36e60916c6a9",
    "createdTimestamp": 1631337976997,
    "username": "admin@example.com",
    "enabled": true,
    "totp": false,
    "emailVerified": false,
    "email": "admin@example.com",
    "disableableCredentialTypes": [],
    "requiredActions": [],
    "notBefore": 0,
    "access": {
        "manageGroupMembership": true,
        "view": true,
        "mapRoles": true,
        "impersonate": true,
        "manage": true
    }
}

Where are the first name and last name? How can I get that data?

Are you sure you have them set for that user? That method returns a firstName and lastName value if they are present.

  {
    "id": "3da4891d-a726-492b-bd5f-a6cce6af483b",
    "createdTimestamp": 1631728576931,
    "username": "admin@example.com",
    "enabled": true,
    "totp": false,
    "emailVerified": true,
    "firstName": "First",
    "lastName": "Last",
    "email": "admin@example.com",
    "disableableCredentialTypes": [],
    "requiredActions": [],
    "notBefore": 0,
    "access": {
      "manageGroupMembership": true,
      "view": true,
      "mapRoles": true,
      "impersonate": true,
      "manage": true
    }
  }
1 Like

My bad. You’re right. I have not specified first name and last name for users in the Development realm.

But this seems to be an odd design. I mean, why not returning firstName: null? That’s something that almost all APIs do, so that API clients can understand that data is null.

I don’t know what the rationale behind it is. If you wish to have it changed, you can file feature enhancements with the developers here: https://issues.redhat.com/projects/KEYCLOAK/issues/