Is there an api to set the primary group for a user

Ideally I’d like to set the primary group when I invite a user to be the group I’m adding in the invite, but I don’t see a way to do that. Is there any way via the api to set the primary group for a user?


I think you may be able to update user settings via updateUser operation:

1 Like

Thanks for the pointer – I tried using it – I updated the primary_group_name, primary_group_id, and even the groups array entry for it [‘primary_group’] = true – I get a 200 back when I call update_user – using the gem, but no dice yet. I’ll take a closer look tomorrow, see if there is some indication of what’s going wrong. I didn’t see anything obvious though.

this is the code I ran and got back a 200 code from the server:

client ="")
client.api_key = 'an api key with global permissions for testing this'
client.api_username = 'my admin username -- I think this doesnt matter though'
user = client.user('joepowell')
user['primary_group_name'] = 'dojo'
user['primary_group_id'] = 42
# this is group 42 in the groups array
user['groups'][2]['primary_group'] = true
client.update_user('joepowell', user)
1 Like

See Reverse engineer the Discourse API. That’s the most reliable way to figure out the API and it’s always up to date.


That’s excellent thanks :). I’ll post back here after I do that tomorrow morning :slight_smile:


So I was able to use the following ruby code to update the primary group id. It doesn’t appear to be supported yet via the gem (I searched the code for references to primary_group_id, and didn’t see any that looked like they’d work this way).

require 'curb'
http = Curl.put('') do | http |
  http.headers['Api-Key'] = 'your-api-key';
  http.headers['Api-Username'] = 'your-api-username';
  http.headers['Content-Type'] = 'application/x-www-form-urlencoded'

1 Like

I think it’s ok to use these methods to make requests to paths that aren’t supported by the gem:

So you can do things like:

 response = client.put("admin/users/4/primary_group", primary_group_id: 43)
1 Like

ahh nice, thanks – I figured I might make a pull request after updating the code here:

but I’m deep into it atm getting things running, so is going to be a few before I get around to making changes to the discourse code itself. I am so loving discourse though – played around with flarum briefly at the recommendation of someone who shall not be named, and there’s just frankly no comparison, Discourse is in another league for anyone wanting to do anything outside the box.



It’s not possible with API change the primary group ? :frowning:

If you can change it with the UX you can change it with the API. See Reverse engineer the Discourse API

Oh, but if you mean with the Gem, then I don’t know. Sorry

1 Like

thank for respond @pfaffman

I test with reverse but don’t work I received 404 not found :frowning:


	"errors": [
		"L'URL ou la ressource demandée n'a pas été retrouvée."
	"error_type": "not_found"

and with
/admin/users/2/primary_group.json: (2 = ID of user barto_95)

	"errors": [
		"L'accès à la ressource demandée n'est pas autorisé."
	"error_type": "invalid_access"

If I look on browser with url /admin/users/2/primary_group.json i view the json

The request to test api :

curl --request PUT \
  --url 'https://xxxxxr/admin/users/2/primary_group.json?=' \
  --header 'Api-Key: a7bb1199xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdecbc7b41658' \
  --header 'Apu-Username: Equipe_HACF' \
  --header 'Content-Type: application/json' \
  --data '{   
	"primary_group_id": "67" 

:frowning: thank for your help

Is that user an admin? My best guess is that it’s a permissions issue. One or more of: The user is wrong, the key is wrong, the permissions for the key are wrong.

1 Like

YEs the problem is the right of user I used

thank’s :slight_smile: