> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-v6-beta2-flutter-uikit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Create A Group

> Create public, private, or password-protected groups in CometChat React Native SDK, with optional member and ban lists at creation time.

<Info>
  **Quick Reference** - Create a group:

  ```javascript theme={null}
  // Create a public group
  const group = new CometChat.Group("GUID", "Hello Group!", CometChat.GROUP_TYPE.PUBLIC, "");
  await CometChat.createGroup(group);

  // Create with members
  const members = [new CometChat.GroupMember("UID", CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)];
  await CometChat.createGroupWithMembers(group, members, []);
  ```
</Info>

<Note>
  **Available via:** [SDK](/sdk/react-native/create-group) | [REST API](/rest-api/groups/create) | [UI Kits](/ui-kit/react-native/groups)
</Note>

## Create a Group

*In other words, as a logged-in user, how do I create a public, private or password-protected group?*

You can create a group using `createGroup()` method. This method takes a `Group` object as input.

To create an object of `Group` class, you can use either of the below two constructors:

1. `new Group(String GUID, String name, String groupType, String password)`
2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)`

The `groupType` needs to be either of the below 3 values:

1.`CometChat.GROUP_TYPE.PUBLIC`

2.`CometChat.GROUP_TYPE.PASSWORD`

3.`CometChat.GROUP_TYPE.PRIVATE`

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    var GUID = "GUID";
    var groupName = "Hello Group!";
    var groupType = CometChat.GROUP_TYPE.PUBLIC;
    var password = "";

    var group = new CometChat.Group(GUID, groupName, groupType, password);

    CometChat.createGroup(group).then(
      group => {
      	console.log("Group created successfully:", group);
      }, error => {
      	console.log("Group creation failed with exception:", error);
      }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    var GUID: string = "GUID";
    var groupName: string = "Hello Group!";
    var groupType: string = CometChat.GROUP_TYPE.PUBLIC;
    var password: string = "";

    var group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType, password);

    CometChat.createGroup(group).then(
      (group: CometChat.Group) => {
          console.log("Group created successfully:", group);
      }, (error: CometChat.CometChatException) => {
          console.log("Group creation failed with exception:", error);
      }
    );
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — `createGroup()` returns the created Group object:

  <span id="create-group-object" style={{scrollMarginTop: '100px'}} />

  **Group Object:**

  | Parameter        | Type    | Description                           | Sample Value                  |
  | ---------------- | ------- | ------------------------------------- | ----------------------------- |
  | `guid`           | string  | Unique group identifier               | `"group_1772435275327"`       |
  | `name`           | string  | Group name                            | `"Creation"`                  |
  | `type`           | string  | Group type                            | `"public"`                    |
  | `owner`          | string  | UID of the group owner                | `"cometchat-uid-7"`           |
  | `scope`          | string  | Logged-in user's scope in the group   | `"admin"`                     |
  | `membersCount`   | number  | Number of members in the group        | `1`                           |
  | `hasJoined`      | boolean | Whether logged-in user has joined     | `true`                        |
  | `isBanned`       | boolean | Whether logged-in user is banned      | `false`                       |
  | `joinedAt`       | number  | Unix timestamp when user joined       | `1772435275`                  |
  | `createdAt`      | number  | Unix timestamp when group was created | `1772435275`                  |
  | `conversationId` | string  | Conversation identifier for the group | `"group_group_1772435275327"` |
</Accordion>

The createGroup() method takes the following parameters:

| Parameter | Description                  |
| --------- | ---------------------------- |
| `group`   | An instance of `Group` class |

After successful creation of the group, you will receive an instance of `Group` class which contains all the information about the particular group.

<Warning>
  GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed. CometChat automatically converts any uppercase characters in the GUID to lowercase.
</Warning>

## Add members while creating a group

You can create a group and add members at the same time using the `createGroupWithMembers()` method. This method takes the `Group` Object, Array of `Group Member` Object to be added & Array of `UIDs` to be banned.

To create an object of `Group` class, you can use either of the below two constructors:

1. `new Group(String GUID, String name, String groupType, String password)`
2. `new Group(String GUID, String name, String groupType, String password, String icon, String description)`

The `groupType` needs to be either of the below 3 values:

1. `CometChat.GROUP_TYPE.PUBLIC`
2. `CometChat.GROUP_TYPE.PASSWORD`
3. `CometChat.GROUP_TYPE.PRIVATE`

To create an object of `Group Member` class, you can use the below constructor:

* new CometChat.GroupMember(String UID, String scope)

<Tabs>
  <Tab title="JavaScript">
    ```javascript theme={null}
    let GUID = "cometchat-guid-11";
    let UID = "cometchat-uid-1";
    let groupName = "Hello Group!";
    let groupType = CometChat.GROUP_TYPE.PUBLIC;

    let group = new CometChat.Group(GUID, groupName, groupType);
    let members = [
    new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)
    ];
    let banMembers = ["cometchat-uid-2"];

    CometChat.createGroupWithMembers(group, members, banMembers).then(
      response => {
      	console.log("Group created successfully", response);
      }, error => {
      	console.log("Some error occured while creating group", error)
      }
    );
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    let GUID: string = "cometchat-guid-11";
    let UID: string = "cometchat-uid-1";
    let groupName: string = "Hello Group!";
    let groupType: string = CometChat.GROUP_TYPE.PUBLIC;

    let group: CometChat.Group = new CometChat.Group(GUID, groupName, groupType);
    let members: Array<CometChat.GroupMember> = [
    new CometChat.GroupMember(UID, CometChat.GROUP_MEMBER_SCOPE.PARTICIPANT)
    ];
    let banMembers: Array<String> = ["cometchat-uid-2"];

    CometChat.createGroupWithMembers(group, members, banMembers).then(
      (response: Object) => {
      	console.log("Group created successfully", response);
      }, (error: CometChat.CometChatException) => {
      	console.log("Some error occured while creating group", error)
      }
    );
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — `createGroupWithMembers()` returns an object with `group` and `members` keys:

  <span id="create-group-with-members-response-object" style={{scrollMarginTop: '100px'}} />

  **Response Object:**

  | Parameter | Type   | Description                          | Sample Value                                             |
  | --------- | ------ | ------------------------------------ | -------------------------------------------------------- |
  | `group`   | object | The created Group object             | [See below ↓](#create-group-with-members-group-object)   |
  | `members` | object | Per-UID results for member additions | [See below ↓](#create-group-with-members-members-object) |

  ***

  <span id="create-group-with-members-group-object" style={{scrollMarginTop: '100px'}} />

  **`group` Object:**

  | Parameter        | Type    | Description                           | Sample Value                               |
  | ---------------- | ------- | ------------------------------------- | ------------------------------------------ |
  | `guid`           | string  | Unique group identifier               | `"group_with_members_1772435203225"`       |
  | `name`           | string  | Group name                            | `"Test Group With Members"`                |
  | `type`           | string  | Group type                            | `"public"`                                 |
  | `owner`          | string  | UID of the group owner                | `"cometchat-uid-7"`                        |
  | `scope`          | string  | Logged-in user's scope in the group   | `"admin"`                                  |
  | `membersCount`   | number  | Number of members in the group        | `4`                                        |
  | `hasJoined`      | boolean | Whether logged-in user has joined     | `true`                                     |
  | `isBanned`       | boolean | Whether logged-in user is banned      | `false`                                    |
  | `joinedAt`       | number  | Unix timestamp when user joined       | `1772435203`                               |
  | `createdAt`      | number  | Unix timestamp when group was created | `1772435203`                               |
  | `conversationId` | string  | Conversation identifier for the group | `"group_group_with_members_1772435203225"` |

  ***

  <span id="create-group-with-members-members-object" style={{scrollMarginTop: '100px'}} />

  **`members` Object:**

  | Parameter         | Type   | Description         | Sample Value |
  | ----------------- | ------ | ------------------- | ------------ |
  | `cometchat-uid-1` | string | Result for this UID | `"success"`  |
  | `cometchat-uid-2` | string | Result for this UID | `"success"`  |
  | `cometchat-uid-3` | string | Result for this UID | `"success"`  |

  Each key is a UID, and the value is either `"success"` or an error message describing why the operation failed.
</Accordion>

This method returns an Object which has two keys: `group` & `members` . The group key has the Group Object which contains all the information of the group which is created. The members key has the `UID` of the users and the value will either be `success` or an `error` message describing why the operation to add/ban the user failed.

## Group Class

| Field        | Editable                                                        | Information                                                               |
| ------------ | --------------------------------------------------------------- | ------------------------------------------------------------------------- |
| guid         | Needs to be specified at group creation. Cannot be edited later | A unique identifier for a group                                           |
| name         | Yes                                                             | Name of the group                                                         |
| type         | No                                                              | Type of the group: Can be 1. Public 2. Password 3. Private                |
| password     | No                                                              | Password for the group in case the group is of type password.             |
| icon         | Yes                                                             | An URL to group icon                                                      |
| description  | Yes                                                             | Description about the group                                               |
| owner        | Yes                                                             | UID of the owner of the group.                                            |
| metadata     | Yes                                                             | Additional data for the group as JSON                                     |
| createdAt    | No                                                              | The unix timestamp of the time the group was created                      |
| updatedAt    | No                                                              | The unix timestamp of the time the group was last updated                 |
| hasJoined    | No                                                              | A boolean to determine if the logged in user is a member of the group.    |
| joinedAt     | No                                                              | The unix timestamp of the time the logged in user joined the group.       |
| scope        | Yes                                                             | Scope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant |
| membersCount | No                                                              | The number of members in the groups                                       |
| tags         | Yes                                                             | A list of tags to identify specific groups.                               |

## Best Practices

<AccordionGroup>
  <Accordion title="Use createGroupWithMembers for initial setup">
    If you know the initial members at creation time, use `createGroupWithMembers()` instead of creating the group first and then adding members separately. This reduces API calls and ensures atomic group setup.
  </Accordion>

  <Accordion title="Keep GUIDs consistent with your system">
    Use the same group identifier from your backend as the CometChat GUID. This simplifies mapping between your system and CometChat.
  </Accordion>

  <Accordion title="Check member add results individually">
    The `createGroupWithMembers()` response includes per-UID results ("success" or error). Check each result rather than assuming all members were added successfully.
  </Accordion>
</AccordionGroup>

## Troubleshooting

<AccordionGroup>
  <Accordion title="createGroup fails with 'GUID already exists'">
    Each GUID must be unique across your CometChat app. If the group already exists, use a different GUID or retrieve the existing group with `getGroup()`.
  </Accordion>

  <Accordion title="GUID validation error">
    GUIDs can only contain alphanumeric characters, underscores, and hyphens. Spaces, punctuation, and other special characters are not allowed. Uppercase characters are automatically converted to lowercase.
  </Accordion>

  <Accordion title="Members not added during createGroupWithMembers">
    Check the `members` key in the response object for per-UID error messages. Common causes include invalid UIDs or users that don't exist in your CometChat app.
  </Accordion>
</AccordionGroup>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Retrieve Groups" icon="layer-group" href="/sdk/react-native/retrieve-groups">
    Fetch group lists, search groups, and get group details
  </Card>

  <Card title="Join a Group" icon="right-to-bracket" href="/sdk/react-native/join-group">
    Join public or password-protected groups
  </Card>

  <Card title="Group Members" icon="users" href="/sdk/react-native/group-members">
    Manage members, roles, and permissions within groups
  </Card>

  <Card title="Send Messages" icon="paper-plane" href="/sdk/react-native/send-message">
    Send text, media, and custom messages to groups
  </Card>
</CardGroup>
