# Using Python to programmatically create 100 groups

**URL:** https://meta.discourse.org/t/using-python-to-programmatically-create-100-groups/269793
**Category:** Development
**Created:** [June 27, 2023, 4:15pm UTC](https://meta.discourse.org/t/using-python-to-programmatically-create-100-groups/269793 "2023-06-27T16:15:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Seth\_Godin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/seth_godin/32/119667_2.png) [@Seth\_Godin](https://meta.discourse.org/u/Seth_Godin)
#### Post date: [June 27, 2023, 4:15pm UTC](https://meta.discourse.org/t/using-python-to-programmatically-create-100-groups/269793/1 "2023-06-27T16:15:56Z")

</div>

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–

and

 ![Screenshot 2023-06-27 at 12.14.30 PM](https://global.discourse-cdn.com/meta/original/4X/7/6/e/76e8e416f31659df2b003c59c0b98cac3568ccb7.jpeg)

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

GPT SAYS:

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:

```plaintext
import requests

api_key = 'your_api_key'
api_username = 'your_api_username'
base_url = 'https://your_discourse_url.com'

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': 'example.com'
    }
    response = requests.post(f'{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 `'https://your_discourse_url.com'` with your actual API key, API username, and Discourse URL.

THANKS.

---

<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: [June 27, 2023, 4:19pm UTC](https://meta.discourse.org/t/using-python-to-programmatically-create-100-groups/269793/2 "2023-06-27T16:19:45Z")

</div>

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](https://meta.discourse.org/t/reverse-engineer-the-discourse-api/20576) to see that you get all of your group settings like you want them.

See also [Use the Discourse API ruby gem](https://meta.discourse.org/t/use-the-discourse-api-ruby-gem/17587)

---

<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: [June 27, 2023, 8:12pm UTC](https://meta.discourse.org/t/using-python-to-programmatically-create-100-groups/269793/3 "2023-06-27T20:12:01Z")

</div>

Something like this:

```ruby
group_names.each do |name|
  begin
  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
end

```

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 27, 2023, 8:12pm UTC](https://meta.discourse.org/t/using-python-to-programmatically-create-100-groups/269793/4 "2023-07-27T20:12:02Z")

</div>

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