> 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/copy-flag-definition.md).

# Copy and Overwrite Feature Flag Definitions Between Environments

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

Use this script to copy feature flag definitions for a given flag in your workspace from one environment to another, and overwrite existing definitions in the target environment.

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

* Install the [Python Admin API Wrapper](/feature-management-experimentation/management-and-administration/api-best-practices/wrappers/python-admin-api.md).
* You've created an Admin API key from the Split UI.

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

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

* `ADMIN API KEY`: Your Split Admin API key.
* `sourceSplitName`: The name of the feature flag you want to use as the source.
* `sourceWorkspaceName`: The name of the workspace that contains the source feature flag.
* `sourceEnvironmentName`: The environment where the source feature flag is defined.
* `targetEnvironmentName`: The environment where you want to copy or apply the feature flag or related changes.

Run this script using Python 3 from your local machine or preferred development environment.

```python
from splitapiclient.main import get_client
import json

#############################################
sourceSplitName="clients"
sourceWorkspaceName="Default"
sourceEnvironmentName="Production"
targetSplitName=sourceSplitName
targetWorkspaceName=sourceWorkspaceName
targetEnvironmentName="Staging"
#############################################

client = get_client({'apikey': 'ADMIN API KEY'})
sourceWs = client.workspaces.find(sourceWorkspaceName)
sourceEnv = client.environments.find(sourceEnvironmentName, sourceWs.id)
sourceSplitDef = client.split_definitions.find(sourceSplitName, sourceEnv.id, sourceWs.id)
targetWs = client.workspaces.find(targetWorkspaceName)
targetEnv = client.environments.find(targetEnvironmentName, targetWs.id)
targetSplitDef = client.split_definitions.find(targetSplitName, targetEnv.id, targetWs.id)

trs = []
for tr in sourceSplitDef._treatments:
   trs.append(tr.export_dict())
rls = []
for rl in sourceSplitDef._rules:
   rls.append(rl.export_dict()) 
drls = []
for drl in sourceSplitDef._default_rule:
   drls.append(drl.export_dict()) 
splitDefinition = {"treatments": trs,"defaultTreatment": sourceSplitDef._default_treatment, "rules": rls, "defaultRule": drls}
targetSplitDef.update_definition(splitDefinition)
```
