How can I update users via API using the user id (not username)?

AI-generated summary

The discussion revolves around updating users via the Discourse API using their user ID instead of username. wilson29thid is seeking a reliable method to update users’ display names and groups using their Discourse member ID, but the API routes expect a username, which can be changed by users and break the integration.

blake suggests making two API requests: one to fetch the user by ID to obtain their username, and the second to update the user by username. Another option is to update a user by external_id, but this requires single-sign-on enabled, allowing Discourse to store the user’s application ID. wilson29thid acknowledges a similar solution after posting the question.

I’d like my application to update my users’ linked discourse account: specifically the display name and groups the user is a member of (e.g. when they’re promoted in my application, they’re added to another forum group as well).

I expected to store a “discourse member id” reference in my application’s users table, and use that to make updates, e.g. POST /users/{id}.json with a payload like {"name": "new name"}. But it looks like all the API routes expect a username instead of an id.

I don’t understand why this would be the default: can’t users change their username? It seems like that would break the integration if they did.

How can I make changes like this reliably, using a consistent identifier (like id)?

For now I think your best option is to make two api requests. One to fetch the user by id in order to get the username, and the second request to update by username.

There is an option to update a user by external_id but I don’t think this can be used without single-sign-on enabled, but this way Discourse stores the id of the user of your application instead of the other way around.

Thanks, I had a similar lightbulb moment after posting.