Adding non-visible User Custom Fields to the API

This is a quick walkthrough on how to use the public user custom fields or staff user custom fields admin setting to expose User Custom Field data through the API without displaying it on a user card.

First, create your User Custom Field. You can find out more detail about that in Create and configure custom user fields

Here’s mine:

Once you have that set up and saved, you can now visit the user fields json at /admin/customize/user_fields.json to find out the ID number:

user_fields json

This can now be entered into either the public user custom fields or staff user custom fields admin setting. The first would be accessible to all, and the second only to a staff API key. The format you need is user_field_X - in my case user_field_7:

As mine is in staff user custom fields it will only be visible to a staff API key.

This will now be included in the /u/[USER].json under ‘User Fields’:

user fields - user json

And there you go. :slight_smile:

5 Likes

This is great, thanks! Is there a way to find a user by a value in a custom field via API?

1 Like

Yes, using query parameters:

For example,
https://unicyclist.com/directory_items.json?period=weekly&order=likes_received&name=Flansberrium+G29&user_field_ids=1|2

Restrict the user custom fields to search by modifying the last parameter user_field_ids=1|2.
For example, user_field_ids=5 to search only in the custom field ID 5.

There are also a couple of other available parameters you can find in the API documentation (docs that don’t mention the user fields, but you can easily reverse-engineer the API.

The custom field(s) must have the searchable option checked for the query to work:

image

Also, note that the query will not search only in custom fields, but also in usernames and maybe full names. I think there’s no built-in feature to filter only by custom fields.

3 Likes