> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/add-segment.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Add a segment to individual targets for a group of feature flags

Learn how to individually target segments to treatments for a given array of feature flags using the Admin API.

## Overview <a href="#overview" id="overview"></a>

Use this script to individually target segments to treatments for a given array of feature flags in a workspace or environment.

## Prerequisites <a href="#prerequisites" id="prerequisites"></a>

* Install the [Python Admin API Wrapper](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/wrappers/python-admin-api.md).
* You have created an Admin API key from the Harness FME UI.

## Configuration <a href="#configuration" id="configuration"></a>

Before running the script, update the following variables in the code:

* `ADMIN API KEY`: Your Admin API key.
* `workspaceName`: The name of the workspace.
* `environmentName`: The name of the environment.
* `segmentNames`: The name of the segment.
* `splits`: The array of feature flag and treatment names.

Run this script using Python 3 from your local machine or preferred development environment. The script does not verify if the given segment name is added to the same traffic type and environment.

```python
from splitapiclient.main import get_client

#############################################
workspaceName="Default"
environmentName="Production"
splits=[["clients_on", "off"], ["clients", "off"]]
segmentNames=["coldweather"]
#############################################

client = get_client({'apikey': 'ADMIN API KEY'})
ws = client.workspaces.find(workspaceName)
env = client.environments.find(environmentName, ws.id)

for splitDef in client.split_definitions.list(env.id, ws.id):
    for split in splits:
        if splitDef._name == split[0]:
            trs = []
            for tr in splitDef._treatments:
                if tr._name == split[1]:
                    tr._segments = segmentNames
                trs.append(tr.export_dict())
            rls = []
            for rl in splitDef._rules:
                rls.append(rl.export_dict()) 
            drls = []
            for drl in splitDef._default_rule:
                drls.append(drl.export_dict()) 
            splitDefinition = {"treatments": trs,"defaultTreatment": splitDef._default_treatment, "rules": rls, "defaultRule": drls}
            splitDef.update_definition(splitDefinition)
```

## Next steps

* Go to [Add user keys to the individual target section in existing feature flags](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/add-keys.md) to add user keys to individual targets.
* Go to [Copy segment IDs to another segment](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/copy-segment.md) to copy keys from one segment to another.

{% @harness-feedback/feedback %}
