API - Add User to moderator group

AI-generated summary

In a discussion titled ‘API - Add User to moderator group,’ sdiscourse seeks to add a user to the moderator group via API but encounters an ‘invalid_access’ error. pfaffman initially suggests the API key may not be properly passed, but sdiscourse confirms using an admin API key and notes that adding users to custom groups works fine. pfaffman clarifies that moderators aren’t managed via group membership — instead, the user’s ‘moderator’ field must be updated via the user API. sdiscourse attempts using the admin user/groups endpoint to assign group ID 2 (moderator) but receives an error: ‘You cannot modify an automatic group.’ codinghorror explains that moderators are not assigned through group editing, advising to use the admin UI’s Grant Moderation button and inspect the network requests via f12 to discover the correct API call. sdiscourse expresses gratitude for the solution, calling it ‘really cool’ and ‘awesome.’ The key takeaway is that moderation status must be granted via the user API or admin UI, not through group membership endpoints, as moderator is an automatic group that cannot be modified directly.

I’m looking to add moderator group to a user using API.

here is the call I used
https://discourse.example.com/groups/{group_id}/members.json

Even though I get an error

“errors”: [

    "You are not permitted to view the requested resource."

],

"error_type": "invalid_access"

It’s most likely that you’re not passing the API key properly.

Thanks for your reply.

I thought of the same at one point but I have admin api key and its still the same error

Additionally, instead of moderator if I try to add the user to any other custom group it gets added successfully.

Wait. You just want to make a user a moderator? You just need to update the moderator field in the user record. It’s not really a group. That’s the problem.

You’d do this with the user API not the group API.

1 Like

I’m not certain is this is method if you are referring to.
However, I tried assigning moderator using this method.

https://discourse.example.com/admin/users/{id}/groups

{
“group_id”: 2 //moderator
}

and I got the following error.

“You cannot modify an automatic group”

Group editing is not how you make someone a moderator. Look at the admin page for that user, press the Grant Moderation button, and watch the f12 console to see what requests were sent.

2 Likes

Vow this is really cool.. Awesome Thank you Jeff!

1 Like