# Add your own attribute to users

> Keep an employee number on every user, checked for shape and uniqueness, and find users by it.

Your HR system names every employee by a number like `E004211`, and your apps want it on the user. In this guide you
add `employeeId` to your realm's user type, make the realm refuse a malformed or duplicate one, set it on a user, and
find them by it.

**Before you start:** you're a realm administrator holding `RealmOwner`, with your session in `$CVYUH_SESSION` (see
[Call the API](../../../develop/api/)).

## Add the attribute

<Steps>

1. Read your realm's user type into a file:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/types/v1/user \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -o user-type.json
   ```

   Note its `rev`.

2. In the file's `schema.properties`, add the attribute, marked for search since you'll look users up by it:

   ```json
   "employeeId": { "type": "string", "x-searchable": true }
   ```

   In its `policies`, add the rules, keeping the ones already there:

   ```json
   "employeeId": ["unique", { "id": "regex-match", "pattern": "^E[0-9]{6}$" }]
   ```

3. Save the edited definition back:

   ```sh
   curl -X PUT https://foobar.cvyuh.com/api/v0/am/types/v1/user \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d @user-type.json
   ```

   The realm answers with the saved type and a new `rev`. If someone changed the type since you read it, it answers
   `412`: read it again and repeat.

4. Set it on a user:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "employeeId": "E004211" }'
   ```

</Steps>

<Aside type="caution" title="Keep what was there">
  The body you save replaces the type. Start from a fresh read, and change only what you add: the realm refuses a save
  that removes or changes a shipped attribute, but an attribute you added earlier and leave out is gone.
</Aside>

## Check it worked

Find Maya by her number:

```sh
curl -G https://foobar.cvyuh.com/api/v0/am/objects/v1/user \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  --data-urlencode 'filter={"filter":{"field":"employeeId","op":"=","value":"E004211"}}'
```

Then try a bad one, `{ "employeeId": "4211" }`: the realm refuses it `400`, naming `employeeId` and the
`regex-match` policy. Give Ben `E004211` too, and it's refused as not unique.

## Undo it

Read the type again, take `employeeId` out of `schema.properties` and `policies`, and save it with `PUT` and the new
`rev`.

## Related

- [Attributes and validation](../../attributes/attributes-and-validation/): every rule you can set.
- [Sensitive attributes](../../attributes/sensitive-attributes/): keep a value out of every read.
- [Search and paging](../../users/search/): find users by any attribute.
