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

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)?

2 Likes

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.

2 Likes

Thanks, I had a similar lightbulb moment after posting.

1 Like