Create group via API

rewrite-pending

(Sol) #1

Is it possible to create/delete groups via the API? I could only find how to assign a user to a group but not how to create them. In the end I would like to sync the Discourse groups with my BuddyPress groups as I want to replace bbPress but I like the actual integration it has with BuddyPress :slight_smile: unfortunately the php api is a bit behind so it looks like a rocky way to get there :frowning: Maybe I am not alone with this idea and we can work together on this as my budget is not that great and I want to keep it open source of possible :slight_smile:


(Sam Saffron) #2

Run Chrome dev tools while performing an operation and you will have the full API info + example payloads.


(Jeff Atwood) #3

Sam can you post a screenshot of this and some basic steps for people to follow as this gets asked a lot.


(Sam Saffron) #4

Done


(Sol) #5

Thank you very much for the hints and your great project! Keep up the great work :thumbsup:


(Christopher Heald) #6

This topic keeps coming up in my searches. Here’s how to create a group via the API:

curl -X POST --data "name=GROUPNAME&alias_level=0&visible=true&automatic_membership_retroactive=false&title=GROUPTITLE&primary_group=false" http://localhost:4000/admin/groups?api_key=SOMETHING&api_username=SOMEONE

Here are the parameters you can set:

  • alias_level: 0
  • automatic: false
  • automatic_membership_email_domains: “”
  • automatic_membership_retroactive: false
  • grant_trust_level: 1
  • name: “NAME”
  • primary_group: false
  • title: “TITLE”
  • visible: true

Also adding this to the API documentation.


(Erlend Sogge Heggen) #7

Good stuff. Better yet, just make a new topic in Howto, and we’ll move this one out to Support.


(Kwame Iwegbue) #8

This is not working for me. When I try to POST using curl like

curl -X POST --data "name=GROUPNAME&alias_level=0&visible=true&automatic_membership_retroactive=false&title=GROUPTITLE&primary_group=false" http://localhost:4000/admin/groups?api_key=MY_API_KEY&api_username=MYUSERNAME

i get “[‘BAD CSRF’]”. When I try to post from python i dont get any response. Is there a setting in Discourse that i need to enable?


(Blake Erickson) #9

Looks like the api changed for this around 4 months ago. All your variables will need to be inside of a group array like group[name] instead of just name.


(Kwame Iwegbue) #10

Thanks for the reply. Can you kindly give an example? There is no documentation of this in the API


(Kwame Iwegbue) #11

I think I’ve got it :slight_smile:

PYTHON EXAMPLE

__dict = {
                'group[alias_level]': 0,
                'group[automatic]': 'false',
                'group[automatic_membership_email_domains]': "",
                'group[automatic_membership_retroactive]': 'true',
                'group[allow_membership_requests]': 'true',
                'group[grant_trust_level]': 0
                'group[name]': 'NAME',
                'group[title]': 'TITLE',
                'group[visible]': 'true',
                'group[primary_group]': 'true',
                'api_key': discourse_api_key,
                'api_username': discourse_api_username
            }
            form_data = urllib.urlencode(__dict)

            headers = {'Content-Type': 'application/x-www-form-urlencoded'}
            result = urlfetch.fetch(
                url='http://localhost:4000//admin/groups',
                payload=form_data,
                method=urlfetch.POST,
                headers=headers)

            logging.debug("RESULT: {}".format(result.content))