---
source_url: https://www.pubnub.com/docs/sdks/python/api-reference/channel-groups
title: Channel Groups API for Python SDK
updated_at: 2026-06-16T12:52:09.771Z
sdk_name: PubNub Python SDK
sdk_version: 10.7.1
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Channel Groups API for Python SDK

PubNub Python SDK, use the latest version: 10.7.1

Install:

```bash
pip install pubnub@10.7.1
```

[Channel groups](https://www.pubnub.com/docs/general/channels/subscribe#channel-groups) allow PubNub developers to bundle thousands of [channels](https://www.pubnub.com/docs/general/channels/overview) into a group that can be identified by a name. These channel groups can then be subscribed to, receiving data from the many back-end channels the channel group contains.

:::note Channel group operations
You can't publish to a channel group. You can only subscribe to it. To publish within the channel group, you need to publish to each channel individually.
:::

:::note Request execution and return values
You can decide whether to perform the Python SDK operations synchronously or asynchronously.
* .sync() returns an Envelope object, which has two fields: Envelope.result, whose type differs for each API, and Envelope.status of type PnStatus. 1pubnub.publish() \2 .channel("myChannel") \3 .message("Hello from PubNub Python SDK") \4 .sync()
* .pn_async(callback) returns None and passes the values of Envelope.result and Envelope.status to a callback you must define beforehand. 1def my_callback_function(result, status):2 print(f'TT: {result.timetoken}, status: {status.category.name}')3 4pubnub.publish() \5 .channel("myChannel") \6 .message("Hello from PubNub Python SDK") \7 .pn_async(my_callback_function)
:::

## Add channels to a channel group

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the PubNub [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

This function adds channels to a channel group.

### Method(s)

`Adding Channels` is accomplished by using the following method(s) in the Python SDK:

:::note Maximum number of channels
You can add up to 200 channels to a channel group per API call.
:::

```python
pubnub.add_channel_to_channel_group() \
    .channels(String|List|Tuple) \
    .channel_group(String)
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| channels | String | Yes |  | The `channel` to add to the `channel_group`. |
| channel_group | String | Yes |  | The `channel_group` to add the `channels` to. |

### Sample code

#### Add channels

:::tip Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
:::

###### Builder Pattern

```python
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.exceptions import PubNubException

def add_channels_to_group(pubnub: PubNub):
    try:
        pubnub.add_channel_to_channel_group() \
            .channels(["ch1", "ch2"]) \
            .channel_group("cg1") \
            .sync()
        print("Channels added to channel group successfully.")
    except PubNubException as e:
        print(f"Error: {e}")

def main():
    # Configuration for PubNub instance
    pn_config = PNConfiguration()
    pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')
    pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo')
    pn_config.user_id = os.getenv('USER_ID', 'my_custom_user_id')

    # Initialize PubNub client
    pubnub = PubNub(pn_config)

    # Add channels to group
    add_channels_to_group(pubnub)

if __name__ == "__main__":
    main()
```

###### Named Arguments

```python
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.exceptions import PubNubException

def add_channels_to_group(pubnub: PubNub):
    try:
        pubnub.add_channel_to_channel_group(
            channels=["ch1", "ch2"],
            channel_group="cg1"
        ).sync()
        print("Channels added to channel group successfully.")
    except PubNubException as e:
        print(f"Error: {e}")

def main():
    # Configuration for PubNub instance
    pn_config = PNConfiguration()
    pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')
    pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo')
    pn_config.user_id = os.getenv('USER_ID', 'my_custom_user_id')

    # Initialize PubNub client
    pubnub = PubNub(pn_config)

    # Add channels to group
    add_channels_to_group(pubnub)

if __name__ == "__main__":
    main()
```

### Returns

The `add_channel_to_channel_group()` operation returns an `Envelope` which contains the following fields:

| Field | Type | Description |
| --- | --- | --- |
| result | [PNChannelGroupsAddChannelResult](#pnchannelgroupsaddchannelresult) | A detailed object containing the result of the operation. |
| status | `PNStatus` | A status object with additional information. |

#### PNChannelGroupsAddChannelResult

```json
Channel successfully added
```

## List channels in a channel group

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

This function lists all channels in a channel group.

### Method(s)

`Listing Channels` is accomplished by using the following method(s) in the Python SDK:

```python
pubnub.list_channels_in_channel_group() \
    .channel_group(String)
```

| Parameter | Description |
| --- | --- |
| `channel_group` *Type: String | The `channel group` to fetch channels. |

### Sample code

#### List channels

##### Builder Pattern

```python
result = pubnub.list_channels_in_channel_group() \
    .channel_group("cg1") \
    .sync()
```

##### Named Arguments

```python
result = pubnub.list_channels_in_channel_group(channel_group="cg1").sync()
```

### Returns

The `list_channels_in_channel_group()` operation returns an `Envelope` which contains the following fields:

| Field | Type | Description |
| --- | --- | --- |
| result | [PNChannelGroupsListResult](#pnchannelgroupslistresult) | A detailed object containing the result of the operation. |
| status | `PNStatus` | A status object with additional information. |

#### PNChannelGroupsListResult

| Field | Type | Description |
| --- | --- | --- |
| `channels` | Dictionary | A list of channels in a channel group. |

## Remove channels from a channel group

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

This function removes channels from a channel group.

### Method(s)

`Removing Channels` is accomplished by using the following method(s) in the Python SDK:

```python
pubnub.remove_channel_from_channel_group() \
    .channels(String|List|Tuple) \
    .channel_group(String)
```

| Parameter | Description |
| --- | --- |
| `channels` *Type: String | List | Tuple | The `channels` to remove from the channel group. |
| `channel_group` *Type: String | Specifies `channel_group` to remove the `channels` from. |

### Sample code

#### Remove channels

##### Builder Pattern

```python
pubnub.remove_channel_from_channel_group() \
    .channels(["son", "daughter"]) \
    .channel_group("channel_group") \
    .sync()
```

##### Named Arguments

```python
pubnub.remove_channel_from_channel_group(channels=["ch1", "ch2"], channel_group="cg1").sync()
```

### Returns

The `remove_channel_from_channel_group()` operation returns an `Envelope` which contains the following fields:

| Field | Type | Description |
| --- | --- | --- |
| result | [PNChannelGroupsRemoveChannelResult](#pnchannelgroupsremovechannelresult) | A detailed object containing the result of the operation. |
| status | `PNStatus` | A status object with additional information. |

#### PNChannelGroupsRemoveChannelResult

```json
Channel successfully removed
```

## Delete a channel group

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

This function deletes a channel group.

### Method(s)

`Deleting Channel Group` is accomplished by using the following method(s) in the Python SDK:

```python
pubnub.remove_channel_group(channel_group)
```

| Parameter | Description |
| --- | --- |
| `channel_group` *Type: String | The `channel group` to remove. |

### Sample code

#### Delete channel group

##### Builder Pattern

```python
pubnub.remove_channel_group() \
    .channel_group("cg1") \
    .sync()
```

##### Named Arguments

```python
pubnub.remove_channel_group(channel_group="cg1").sync()
```

### Returns

The `remove_channel_group()` operation returns an `Envelope` which contains the following fields:

| Field | Type | Description |
| --- | --- | --- |
| result | [PNChannelGroupsRemoveGroupResult](#pnchannelgroupsremovegroupresult) | A detailed object containing the result of the operation. |
| status | `PNStatus` | A status object with additional information. |

#### PNChannelGroupsRemoveGroupResult

```json
Group successfully removed
```

## Terms in this document

* **Channel** - A pathway for sending and receiving messages between devices, created automatically when you first use it, that can handle any number of users and messages for different communication needs, like 1-1 text chats, group conversations, and other data streaming.
* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.