# Open Source Script

The [Statsig open-source migration script](https://github.com/statsig-io/migrations) moves feature flags from LaunchDarkly to Statsig. It fetches your LaunchDarkly flags, translates them into Statsig's format, creates matching feature gates in Statsig, and writes the migration status to a CSV file. Use it when you need to customize the import logic beyond what the in-console tool offers.

## Considerations

This script works by default. Start with a test environment of 5–10 flags. Before running the script at scale, consider the following:

* **IMPORTANT**: If you don't need to customize the import script, use [Statsig's in-console tool](https://docs.statsig.com/guides/ui-based-tool) instead.

* The script uses a tag `Imported from LaunchDarkly` to identify migrated flags in Statsig. Ensure this tag is unique and recognizable.

* The script includes a function to delete all Statsig feature gates with a specific tag. Use this function with caution to clean up after a test or failed migration.

* The script requires API keys for both LaunchDarkly and Statsig. Keep these keys secure.

## Installation

To run the script, you need Node.js and npm installed on your system. Execute directly:

```bash
npx @statsig/migrations --from launchdarkly --launchdarkly-project-id default <more-arguments>
```

## Configuration

* Provide your [LaunchDarkly API key](https://docs.launchdarkly.com/home/account/api) and [Statsig Console API key](https://docs.statsig.com/console-api/introduction) in the script.
* Map LaunchDarkly environments to Statsig environments that aren't already the same by passing `--environment-name-mapping` to the script.
* Map LaunchDarkly context kind to Statsig's custom unit ids and custom fields.

You can find the detailed instructions for these steps on the [Github repo](https://github.com/statsig-io/migrations).

## Running the script

To execute the migration script, run the following command in your terminal:

```bash
node index.js
```

The script performs the following actions:

1. Fetch all feature flags from LaunchDarkly.
2. Translate each flag into Statsig's format.
3. Create feature gates in Statsig.
4. Write the migration status and details to a CSV file named `flag_migration_tracker.csv`.

## Example translations

The following examples show how the script translates LaunchDarkly feature flags into Statsig feature gates:

**Example 1:** LaunchDarkly flag with email and name targeting conditions

{% figure %}
![Example 1 - LaunchDarkly to Statsig translation](https://docs.statsig.com/images/tutorials/migration-launchdarkly/open-source/example-translation-1.png)
{% /figure %}

**Example 2:** LaunchDarkly flag with country-based targeting (off flag)

{% figure %}
![Example 2 - LaunchDarkly to Statsig translation](https://docs.statsig.com/images/tutorials/migration-launchdarkly/open-source/example-translation-2.png)
{% /figure %}

## Troubleshooting

If you encounter issues during the migration, check the following:

* Confirm that the API keys are correct and have the necessary permissions.
* Review the error messages in the console to diagnose the problem.

Pull requests and feedback are welcome.
