# Add a user to a group via API?

**URL:** https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077
**Category:** Development
**Tags:** rest-api
**Created:** [June 1, 2016, 9:42am UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077 "2016-06-01T09:42:55Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jefflau](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jefflau/32/121720_2.png) [@jefflau](https://meta.discourse.org/u/jefflau)
#### Post date: [June 1, 2016, 9:42am UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/1 "2016-06-01T09:42:55Z")

</div>

I’m trying to use the Discourse API to update a group with a particular user. In the docs that I have found, there wasn’t any information on this, only the ability to create a group.

I also tried to watch the XHR request when I’m actually updating a group on discourse with another user. The API seems to be calling this URL: `groups/[group_name]/members.json`, which I can call with a `GET` and get back the group info, but I get an error when I tried to `PUT` with the `usernames` parameter.

```plaintext
{
  "errors": [
    "You are not permitted to view the requested resource."
  ],
  "error_type": "invalid_access"
}

```

Any advice on how I can get around this? I have put my API key and username as a query as per the API documentation instructions.

Thanks.

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [June 1, 2016, 11:25am UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/2 "2016-06-01T11:25:25Z")

</div>

Something similar to this will add users “`bill`” and “`ben`” to group “`flower_pot_men`”:

```php
	/**
	 * Add users to a specified group, create the group if it doesn't exist.
	 *
	 * @param string $group_name name of group
	 * @param array $usernames users to add to group
	 *
	 * @return mixed HTTP return code and API return object
	 */

	public function add_users_to_group( $group_name, $usernames = array() ) {
		$group_id = null;

		$result = $this->get_request( "/admin/groups.json" );
		if ( $result->http_code != 200 ) {
			return $result;
		}

		foreach ( $result->result as $group ) {
			if ( $group->name === $group_name ) {
				$group_id = $group->id;
				break;
			}
			$group_id = false;
		}

		if ( ! $group_id ) {
			$result = $this->post_request( '/admin/groups', array( 'name' => $group_name ) );
			if ( $result->http_code != 200 ) {
				return $result;
			}
			$group_id = $result->result->basic_group->id;
		}
		if ( ! empty( $usernames ) ) {
			$params = array(
				'usernames' => implode( ',', $usernames )
			);
			$result = $this->put_request( '/admin/groups/' . $group_id . '/members.json', $params );
		}

		return $result;
	}

```

```PHP
$group_name = 'flower_pot_men';
$usernames = array( 'bill', 'ben' );
$result = $api->add_users_to_group( $group_name, $usernames );
if ( $result->http_code != 200 ) {
	// @todo handle the error
}

```

EDIT: The key point here is the `/admin` part is different from yours.  
EDIT #2: Updated to correctly reflect ID usage.

> **More about Bill and Ben**
>
> [![](https://global.discourse-cdn.com/meta/original/4X/a/9/3/a9391477d19da50d92816649777bbe0a53772af8.jpeg "ATV - "Bill & Ben The Flower Pot Men"") ](https://www.youtube.com/watch?v=hcF9JSxkUSE&t=2m4s)
> 
> “Wubb-ooooh”

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [June 1, 2016, 11:35am UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/3 "2016-06-01T11:35:32Z")

</div>

> [@jefflau](#):
>
> I also tried to watch the XHR request when I’m actually updating a group on discourse with another user. The API seems to be calling this URL: groups/[group\_name]/members.json, which I can call with a GET and get back the group info, but I get an error when I tried to PUT with the usernames parameter.

That’s the right approach. Discourse is very happy to reply with a `403` if _anything_ isn’t as expected. I’ve been struggling with this too…

I recommend using a Proxy (if you’re using Windows, look at [Fiddler](http://www.telerik.com/fiddler)) to carefully compare your request with a correct one from the web interface. Things to look at:

- Are you really using `PUT`?
- Do all parameters match 100%?
- Do any relevant headers differ?

Also triple-check that you are really giving a correct API key and a valid username.

---

<div class="post-metadata">

### Author: ![jefflau](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jefflau/32/121720_2.png) [@jefflau](https://meta.discourse.org/u/jefflau)
#### Post date: [June 1, 2016, 5:57pm UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/4 "2016-06-01T17:57:02Z")

</div>

Is the groupID supposed to be a number or the name of the group? From the API call I saw it was an ID. I actually get a 200 success when I use the ID, but the groups don’t seem to be updating for that user

---

<div class="post-metadata">

### Author: ![DeanMarkTaylor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/deanmarktaylor/32/102462_2.png) [@DeanMarkTaylor](https://meta.discourse.org/u/DeanMarkTaylor)
#### Post date: [June 1, 2016, 6:17pm UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/5 "2016-06-01T18:17:30Z")

</div>

Yes @jefflau you are correct it is a numeric group ID that is required.

I’ve updated the post above to reflect one method of implementing adding by name…  
… specifically looks up the group ID by name, if not found create a group with that name…

… then add the users.

---

<div class="post-metadata">

### Author: ![jefflau](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jefflau/32/121720_2.png) [@jefflau](https://meta.discourse.org/u/jefflau)
#### Post date: [June 4, 2016, 9:36am UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/6 "2016-06-04T09:36:17Z")

</div>

Managed to figure it out, but I used a completely different method:

```js
addUserToGroup(userId, groupId){
    let url = `${this.url}admin/users/${userId}/groups`;
    let options = {
      params: {
        group_id: groupId
      },
      query: this.defaultQuery
    }
    return httpPromise('POST', url, options);
}

```

Uses post and uses a different url related to the user. Is there a difference to doing it one way or the other? I just couldn’t figure out what I was doing wrong with the other way.

Feel like someone should be keeping the API docs up to date. Shouldn’t have to go around fishing for what API calls work and what not!

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 4, 2016, 9:45am UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/7 "2016-06-04T09:45:36Z")

</div>

[The docs are the live app](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576), since the Discourse JavaScript app that you’re running right now makes those very same calls in the group management UI.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [January 31, 2018, 7:03pm UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/8 "2018-01-31T19:03:45Z")

</div>

This script will create a user and optionally add it to a group:

[https://github.com/pfaffman/discourse-user-creator](https://github.com/pfaffman/discourse-user-creator)

---

<div class="post-metadata">

### Author: ![Anton\_Akhmerov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/anton_akhmerov/32/169548_2.png) [@Anton\_Akhmerov](https://meta.discourse.org/u/Anton_Akhmerov)
#### Post date: [March 16, 2023, 3:13pm UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/10 "2023-03-16T15:13:44Z")

</div>

I’m getting 404 on `PUT /admin/groups/{group_id}/owners.json` on discourse 3.1.0.beta2 (f8863b0f98); is this topic up to date?

EDIT: it seems that one should use `PUT /groups/{group_id}/owners.json` instead. Additionally the input data is now `{"members": "username1,username2,..."}`.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [March 17, 2023, 10:48am UTC](https://meta.discourse.org/t/add-a-user-to-a-group-via-api/45077/11 "2023-03-17T10:48:29Z")

</div>


