Find user through API by user field?

(Burke) #1

Continuing the discussion from Discourse API Documentation:

We’re using a github user field for users to specify their GitHub username. I’d like to use Discourse’s API to make changes to a user, given their GitHub username. Is there an easy way to search for a user by a user field value?

Technically speaking, I can make a list of user field values:

  • GET /admin/users.json
  • Loop through each user to GET /users/:username.json
  • Collect user.user_fields[GITHUB_USER_FIELD_ID] from each one

but that’s prohibitively inefficient. Any chance there’s a method like

GET /users?user_fields[1]=foo&api_key=...&api_username=...

to get all users with user_fields[1] containing “foo” that exists or is in the works?

I’m a Ruby n00b, so haven’t been able to find my answer in the source. I also haven’t been able to find a feature in Discourse that lets you search by user field value from which to reverse engineer the proper call.

Thanks for any advice,


(Jeff Atwood) #2

Sure @techapj can you offer a suggestion here?

(Burke) #3

I looked around to see if a Ruby n00b like myself might be able to muster a pull request for this feature. It looks like the ability to search users by user_field value would first require adding support for user field searching into UserSearch – i.e., adding params like @user_field_id and @user_field_value. Am I on the right track?

(Burke) #4

As it turns out, not only is finding users by user field not available via the API, getting a list of all users isn’t available either. Found this workaround for now.

So, the only approach I’ve found thus far to find Discourse users by GitHub user field setting is:

  • GET /groups/trust_level_0/members.json?limit=n&offset=0, iterate if needed
  • Loop through all usernames found with GET /users/:username.json
  • Collect user.user_fields[GITHUG_USER_FIELD_ID] for each one

(Michael Downey) #5

It should only be necessary to do this for trust_level_0 since it includes all users in “higher” trust levels.

How do I get a list of all users from the API?
(Burke) #6

Cool. Thanks. I’ll modify my recipe.