Best way to update or create existing user, via API, from connected client?

AI-generated summary

aaronbauman outlines a common use case where an external system (like Drupal) needs to sync users with Discourse via API, even when users don’t use SSO. Currently, Discourse rejects API user creation if an email already exists. The desired behavior is to link existing Discourse users to Drupal users by email. aaronbauman proposes querying /admin/users/list/all.json?email=$email to check for matching users — if found, link them; otherwise, create a new user. Two key questions are raised: whether this is the best approach, and whether the API query is correctly formed — noting that neither all.json nor the email parameter are officially documented, though a referenced post suggests it’s the best available method. pfaffman agrees the solution seems reasonable and confirms the query looks correct, recommending Reverse engineer the Discourse API as the most up-to-date source of API documentation. The discussion centers on improving user sync reliability between external systems and Discourse without relying on SSO.

Here’s the use case, which I’m sure many people have faced:

  • Have bunch of users in Discourse
  • Have an external system (service provider, Drupal) which uses API create Discourse users and manage group memberships
  • Drupal has partially overlapping set of users. ie. Users with same email address exist in Discourse and Drupal, but aren’t linked
  • Drupal uses API to sync users, groups, and memeberships because users might never use SSO to access Discourse, instead relying on emails

Current behavior:

  • Discourse sometimes rejects the API call to create a Discourse user, because a Discourse user already exists with same email

Desired behavior:

  • Discourse links existing Discourse user to the Drupal user, via email

Proposed solution:

  • Query discourse /admin/users/list/all.json?email=$email API endpoint to fetch any users with matching email address
  • If the matching user exists
    • Connect the existing users (Drupal side)
  • else
    • Create a new discourse user

Questions

  • Does this seem like a reasonable solution, or is there a better way to do it?
  • Is the API query formed correctly?
    Neither all.json nor the email query string param are documented, and I found very few examples, but this post seems to indicate it’s the best way to go.

I’m not as expert as some, but I think so.

It looks right to me. The best, and always up to date, documention is Reverse engineer the Discourse API.