> 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-targeting-rule.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Add targeting rules to an existing feature flag

Learn how to add targeting rules to an existing feature flag using the Admin API.

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

Use this script to add targeting rules to an existing feature flag.

## 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. If you only have one workspace, use the Default workspace.
* `environmentName`: The name of the environment.
* `splitName`: The name of the feature flag.

Run this script using Python 3 from your local machine or preferred development environment. This example adds two targeting rules.

```python
from splitapiclient.main import get_client

############################################
workspaceName = "Default"
environmentName = "Production"
splitName = "clients_on"
############################################

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

trs = []
for tr in splitDef._treatments:
    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())
rls.append({"buckets": [{"treatment": "on","size": 100}],"condition": {"matchers": [{"attribute":"userGroup", "type": "MATCHES_STRING","string": "employees"}], "combiner": "AND"}})
splitDefinition = {"treatments": trs,"defaultTreatment": splitDef._default_treatment, "rules": rls, "defaultRule": drls}
splitDef.update_definition(splitDefinition)
```

## Next steps

* Go to [Copy and overwrite feature flag definitions between environments](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/copy-flag-definition.md) to copy flag definitions between environments.
* Go to [Export all feature flag definition JSON structures](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/export-flag-definitions.md) to export flag definitions as JSON.

{% @harness-feedback/feedback %}
