# Bulk group management

**URL:** <https://community.omni.co/t/bulk-group-management/305>\
**Category:** Administration\
**Tags:** users\
**Created:** [April 16, 2025, 5:10am UTC](https://community.omni.co/t/bulk-group-management/305 "2025-04-16T05:10:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![russ](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.omni.co/russ/32/296_2.png) [@russ](https://community.omni.co/u/russ)\
**Post date:** [April 16, 2025, 5:10am UTC](https://community.omni.co/t/bulk-group-management/305/1 "2025-04-16T05:10:05Z")

</div>

The best approach to managing users and their groups is to derive them from an SSO system, and Omni supports many systems through its SAML and SCIM such as [Okta](https://docs.omni.co/docs/administration/saml/okta), [Active Directory](https://docs.omni.co/docs/administration/saml/active-directory) and more.  
But sometimes you have a need to bulk associate users to groups in a pinch, this script is for you.

The [Python SDK](https://github.com/exploreomni/omni-python-sdk) now supports the api endpoints to make group assignment a piece of cake 🎂 .

You can see the script for performing these updates [here](https://github.com/exploreomni/omni-python-sdk/blob/0.1.10/examples/user_management/user_groups_management.py). But we’ll walk through the setup step by step.

First, install the python SDK. You may want to create a virtual environment first.

#### Optional Step 0 - Create a virtual environment

Starting in a folder you’d like the script to live in. [Double check you have at least python 3.9+](https://www.naukri.com/code360/library/how-to-check-python-version-in-cmd)

```bash
python -m venv ./.venv

```

Activate the virtual environment:

```bash
source ./.venv/bin/activate

```

[windows instructions](https://www.geeksforgeeks.org/creating-python-virtual-environment-windows-linux/)

#### Step 1 - Pip install the sdk

```bash
pip install omni_python_sdk

```

#### Step 2 - Create the script and fill out API key and Omni Host

```python
from omni_python_sdk import OmniAPI
import csv, time

api_key = '<<your api key>>'
base_url = 'https://<<your omni host>>'

# Initialize the API with your credentials
api = OmniAPI(api_key, base_url)

with open('user_groups.csv', newline='') as csvfile:
    user_groups = csv.DictReader(csvfile)
    for row in user_groups:
        time.sleep(4)
        email = row.pop('email')
        op = row.pop('op')
        if op == '+':
            user = api.return_user_by_email(email)
            api.add_user_to_group(
                row['group_name'],
                user['id']
            )
           print(f"{email} successfully added to {row['group_name']}")
        elif op == '-':
            user = api.return_user_by_email(email)
            api.remove_user_from_group(
                row['group_name'],
                user['id']
            )
           print(f"{email} successfully removed from {row['group_name']}")

```

#### Step 3 - Set up your CSV

 ![image](https://canada1.discourse-cdn.com/flex004/uploads/omni/original/1X/fb956d0cc638a3c8ac07e03e2d6edb40b2616ce0.png)

- The `op` column should either be ‘+’, which will add the user to the group, or ‘-’ which will remove a user from a group
- The `group_name` column is the name, not the id, of the group
- A user can be in multiple groups, they will be repeated rows, one for each group to add
- Emails normally tie 1:1 to a user, but this script will fail if there is more than one user with the same email address

#### Step 4 – Run it

```bash
python <<name of your script>>.py

```

It will pause 2 seconds in between each update to ensure it doesn’t hit the API throttle and proceed silently

That’s it! you’ve updated your group membership

---

<div class="post-metadata">

**Author:** ![Alison](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.omni.co/alison/32/384_2.png) [@Alison](https://community.omni.co/u/Alison)\
**Post date:** [April 17, 2025, 2:52pm UTC](https://community.omni.co/t/bulk-group-management/305/2 "2025-04-17T14:52:40Z")

</div>

A few tips and notes:

- Change `time.sleep(2)` to `time.sleep(4)`. With the time delay between API calls set at 2, the script stopped at row 29 with the error `Request Failed: 429 Client Error: Too Many Requests`.
- Add `print(email + " completed")` as the last line of the script. It should be indented to be under the `for` loop. This will give you feedback in the terminal window that something is indeed happening and make it easy for you to know how far it’s gotten.
- If a user-to-user group connection for a row of the CSV already exists then the API call for that specific row will fail with `Request Failed: 500 Server Error: Internal Server Error`. Though the script will keep going. I moved the first 28 rows out to a separate file to keep track of what had been run and not go through each failure message.
- If the email address does not exist as a user account then the script will fail with `Found 0 users for email@email.com`.

---

<div class="post-metadata">

**Author:** ![russ](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.omni.co/russ/32/296_2.png) [@russ](https://community.omni.co/u/russ)\
**Post date:** [April 17, 2025, 3:59pm UTC](https://community.omni.co/t/bulk-group-management/305/3 "2025-04-17T15:59:11Z")

</div>

Thank you Alison! Very helpful suggestions I’ve modified in the script above 😎

---

<div class="post-metadata">

**Author:** ![Jamiefry](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@Jamiefry](https://community.omni.co/u/Jamiefry)\
**Post date:** [July 3, 2025, 8:17am UTC](https://community.omni.co/t/bulk-group-management/305/4 "2025-07-03T08:17:18Z")

</div>

this can act as an alternative to above if you dont want to run your own scripts:

> [@rocket Open Sourced: Bulk User & Group Management CLI for Omni.co (Beta)](https://community.omni.co/t/open-sourced-bulk-user-group-management-cli-for-omni-co-beta/342):
>
> I’ve just open sourced [omni-user-manager](https://pypi.org/project/omni-user-manager/0.2.1/), a Python CLI package that provides easy-to-use wrappers around the [Omni.co](http://Omni.co) APIs for user and group management. Key features: Comprehensive sync operations - The sync command orchestrates multiple API calls to handle users, groups, and attributes in one operation Flexible data sources - Import from CSV/JSON with support for SCIM 2.0 format Full CRUD operations - Search, create, update, delete users and groups with simple CLI commands Smart updates - On…
