# 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:** 1\
**Showing post:** 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.

---

_[View the full topic](https://meta.discourse.org/t/using-python-to-programmatically-create-100-groups/269793)._
