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

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.
1 Like

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.

1 Like