Using Python to programmatically create 100 groups

I asked the GPT bot, and it probably came close, but I’m a novice and would love some smart insight before I break something.

I have a list of 100 group names (can be comma delimited, or a file, or whatever)

I want to import them and have them all have the same settings, as listed here:

Allow users to join the group freely (Requires publicly visible group)
Allow users to leave the group freely
–no automatic members–


The following from the bot seems like a start, but I’m not really comfortable hacking my way through it:


However, you can use the Discourse API to create groups programmatically. This will require some technical knowledge and familiarity with APIs.

Here’s a basic example of how you might do this in Python using the requests library:

import requests

api_key = 'your_api_key'
api_username = 'your_api_username'
base_url = ''

headers = {
    'Api-Key': api_key,
    'Api-Username': api_username,

for i in range(1, 101):
    data = {
        'name': f'group_{i}',
        'visible': True,
        'automatic_membership_email_domains': ''
    response ='{base_url}/admin/groups', headers=headers, data=data)
    print(f'Created group {i}, response code: {response.status_code}')

Replace 'your_api_key', 'your_api_username', and '' with your actual API key, API username, and Discourse URL.



Looks mostly true.

See if it works for one or two before you loop through a hundred, though!

You can check Reverse engineer the Discourse API to see that you get all of your group settings like you want them.

See also Use the Discourse API ruby gem


Something like this:

group_names.each do |name|
  response = client.create_group(name: name,
    visibility_level: 0,
    members_visibility_level: 0,
    mentionable_level: 3,
    messageable_level: 3,
    public_exit: true,
    public_admission: true
  group_id = response["basic_group"]["id"]
  group_name =response["basic_group"]["name"]
  puts "Created #{group_name}(#{group_id})"
  sleep 1
rescue => e
  puts "oops. Had a problem with #{name} #{e}"
  sleep 1


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.