2019-03-14 17:56:02 +01:00
---
title: Import & Export
---
2022-11-08 14:42:06 +01:00
import ApiRequest from '@site/src/components/ApiRequest'
2019-03-14 17:56:02 +01:00
2022-11-08 14:42:06 +01:00
:::info Availability
The import and export API first appeared in Unleash 3.3.0.
:::
2019-03-15 15:39:26 +01:00
refactor: move docs into new structure / fix links for SEO (#2416)
## What
This (admittedly massive) PR updates the "physical" documentation
structure and fixes url inconsistencies and SEO problems reported by
marketing. The main points are:
- remove or move directories : advanced, user_guide, deploy, api
- move the files contained within to the appropriate one of topics,
how-to, tutorials, or reference
- update internal doc links and product links to the content
- create client-side redirects for all the urls that have changed.
A number of the files have been renamed in small ways to better match
their url and to make them easier to find. Additionally, the top-level
api directory has been moved to /reference/api/legacy/unleash (see the
discussion points section for more on this).
## Why
When moving our doc structure to diataxis a while back, we left the
"physical' files lying where they were, because it didn't matter much to
the new structure. However, that did introduce some inconsistencies with
where you place docs and how we organize them.
There's also the discrepancies in whether urls us underscores or hyphens
(which isn't necessarily the same as their file name), which has been
annoying me for a while, but now has also been raised by marketing as an
issue in terms of SEO.
## Discussion points
The old, hand-written API docs have been moved from /api to
/reference/api/legacy/unleash. There _is_ a /reference/api/unleash
directory, but this is being populated by the OpenAPI plugin, and mixing
those could only cause trouble. However, I'm unsure about putting
/legacy/ in the title, because the API isn't legacy, the docs are. Maybe
we could use another path? Like /old-docs/ or something? I'd appreciate
some input on this.
2022-11-22 10:05:30 +01:00
Unleash supports import and export of feature toggles and strategies at startup and during runtime. The main purpose of the import/export feature is to bootstrap new Unleash instances with feature toggles and their configuration. If you are looking for a granular way to keep seperate Unleash instances in sync we strongly recommend that you take a look at the Unleahs Admin API's.
2022-11-14 10:24:20 +01:00
The import mechanism guarantees that:
2022-01-06 10:54:27 +01:00
- all imported features will be non-archived
- existing updates to strategies and features are included in the event history
2019-03-14 17:56:02 +01:00
2019-03-15 15:39:26 +01:00
All import mechanisms support a `drop` parameter which will clean the database before import (all strategies and features will be removed).
2019-03-14 17:56:02 +01:00
2022-01-06 10:54:27 +01:00
:::caution Dropping in production
Be careful when using the `drop` parameter in production environments: cleaning the database could lead to unintended loss of data.
:::
2019-03-14 17:56:02 +01:00
2021-06-04 11:17:15 +02:00
## Runtime import & export {#runtime-import--export}
2019-03-14 17:56:02 +01:00
2021-06-04 11:17:15 +02:00
### State Service {#state-service}
2019-03-14 17:56:02 +01:00
Unleash returns a StateService when started, you can use this to import and export data at any time.
```javascript
const unleash = require('unleash-server');
2020-11-23 21:52:57 +01:00
const { services } = await unleash.start({...});
const { stateService } = services;
2021-03-12 11:08:10 +01:00
const exportedData = await stateService.export({includeStrategies: false, includeFeatureToggles: true, includeTags: true, includeProjects: true});
2020-11-23 21:52:57 +01:00
await stateService.import({data: exportedData, userName: 'import', dropBeforeImport: false});
await stateService.importFile({file: 'exported-data.yml', userName: 'import', dropBeforeImport: true})
2019-03-14 17:56:02 +01:00
```
2019-03-15 15:39:26 +01:00
If you want the database to be cleaned before import (all strategies and features will be removed), set the `dropBeforeImport` parameter.
2021-08-10 14:02:48 +02:00
It is also possible to not override existing feature toggles (and strategies) by using the `keepExisting` parameter.
2019-03-14 17:56:02 +01:00
2021-06-04 11:17:15 +02:00
### API Export {#api-export}
2019-03-14 17:56:02 +01:00
2022-01-06 10:54:27 +01:00
The api endpoint `/api/admin/state/export` will export feature-toggles and strategies as json by default.
2021-03-09 21:44:45 +01:00
You can customize the export with query parameters:
2019-03-14 17:56:02 +01:00
2020-09-25 09:43:23 +02:00
| Parameter | Default | Description |
| --- | --- | --- |
| format | `json` | Export format, either `json` or `yaml` |
| download | `false` | If the exported data should be downloaded as a file |
| featureToggles | `true` | Include feature-toggles in the exported data |
| strategies | `true` | Include strategies in the exported data |
2021-03-12 11:08:10 +01:00
| tags | `true` | Include tagtypes, tags and feature_tags in the exported data |
| projects | `true` | Include projects in the exported data |
2019-03-14 17:56:02 +01:00
2021-03-12 11:08:10 +01:00
For example if you want to download just feature-toggles as yaml:
2019-03-14 17:56:02 +01:00
2022-11-08 14:42:06 +01:00
< ApiRequest verb = "get" url = "api/admin/state/export?format=yaml&featureToggles=1&strategies=0&tags=0&projects=0&download=1" title = "Export features (and nothing else) as YAML." / >
2021-03-17 07:34:23 +01:00
2021-06-04 11:17:15 +02:00
### API Import {#api-import}
2019-03-14 17:56:02 +01:00
2023-01-04 12:24:34 +01:00
:::caution Importing environments in Unleash 4.19 and below
This is only relevant if you use **Unleash 4.19 or earlier** :
2022-12-14 15:26:41 +01:00
If you import an environment into an instance that already has that environment defined, Unleash will delete any API keys created specifically for that environment. This is to prevent unexpected access to the newly imported environments.
:::
2019-03-14 17:56:02 +01:00
You can import feature-toggles and strategies by POSTing to the `/api/admin/state/import` endpoint (keep in mind this will require authentication).\
You can either send the data as JSON in the POST-body or send a `file` parameter with `multipart/form-data` (YAML files are also accepted here).
2021-08-10 14:02:48 +02:00
You can customize the import with query parameters:
| Parameter | Default | Description |
| --- | --- | --- |
| drop | `false` | If the database should be cleaned before import (see comment below) |
| keep | `true` | If true, the existing feature toggles and strategies will not be override |
If you want the database to be cleaned before import (**all strategies and features will be removed**), specify a `drop` query parameter.
2019-03-15 15:39:26 +01:00
2022-12-14 15:26:41 +01:00
:::caution
You should be cautious about using the `drop` query parameter in production environments.
:::
2019-03-14 17:56:02 +01:00
Example usage:
2022-11-08 14:42:06 +01:00
< ApiRequest verb = "post" url = "api/admin/state/import" payload = {{ " version " : 3 , " features " : [ { " name " : " a-feature-toggle " , " enabled " : true , " description " : " # 1 feature-toggle " } ] } } title = "Import data into Unleash." / >
2021-03-17 07:34:23 +01:00
2021-06-04 11:17:15 +02:00
## Startup import {#startup-import}
2019-03-14 17:56:02 +01:00
2022-01-06 10:54:27 +01:00
You can import toggles and strategies on startup by using an import file in JSON or YAML format. As with other forms of imports, you can also choose to remove the current toggle and strategy configuration in the database before importing.
Unleash lets you do this both via configuration parameters and environment variables. The relevant parameters/variables are:
| config parameter | environment variable | default | value |
|--------------------|-----------------------------|---------|---------------------------------------------------------|
| `importFile` | `IMPORT_FILE` | none | path to the configuration file |
| `dropBeforeImport` | `IMPORT_DROP_BEFORE_IMPORT` | `false` | whether to clean the database before importing the file |
2022-01-07 08:03:49 +01:00
### Importing files
2019-03-14 17:56:02 +01:00
2022-01-07 08:03:49 +01:00
To import strategies and toggles from a file (called `configuration.yml` in the examples below), either
- use the `importFile` parameter to point to the file (you can also passed this into the `unleash.start()` entry point)
``` shell
unleash-server --databaseUrl [...] \
--importFile configuration.yml
```
2019-03-14 17:56:02 +01:00
2022-01-07 08:03:49 +01:00
- set the `IMPORT_FILE` environment variable to the path of the file before starting Unleash
2019-03-14 17:56:02 +01:00
2022-01-07 08:03:49 +01:00
``` shell
IMPORT_FILE=configuration.yml
```
2019-03-15 15:39:26 +01:00
2022-01-07 08:03:49 +01:00
### Drop before import
2022-01-06 10:54:27 +01:00
:::caution
You should never use this in production environments.
:::
2022-01-07 08:03:49 +01:00
To remove pre-existing feature toggles and strategies in the database before importing the new ones, either:
- add the `dropBeforeImport` flag to the `unleash-server` command (or to `unleash.start()` )
``` shell
unleash-server --databaseUrl [...] \
--importFile configuration.yml \
--dropBeforeImport
```
- set the `IMPORT_DROP_BEFORE_IMPORT` environment variable (note the leading `IMPORT_` ) to `true` , `t` , or `1` . The variable is case-sensitive.
``` shell
IMPORT_DROP_BEFORE_IMPORT=true
```