2019-03-14 17:56:02 +01:00
---
id: import_export
title: Import & Export
---
2019-03-15 15:39:26 +01:00
_since v3.3.0_
2019-03-14 17:56:02 +01:00
Unleash supports import and export of feature-toggles and strategies at startup and during runtime. The import mechanism will guarantee that all imported features will be non-archived, as well as updates to strategies and features are included in the event history.
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
2020-10-06 12:52:25 +02:00
> You should be careful when using `drop` parameter in production environments, as it will clean current state.
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
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
```
2021-03-12 11:08:10 +01:00
/api/admin/state/export?format=yaml& featureToggles=1& strategies=0& tags=0& projects=0& download=1
2019-03-14 17:56:02 +01:00
```
2021-03-17 07:34:23 +01:00
Example with curl:
```sh
curl -X GET -H "Content-Type: application/json" \
-H "Authorization: Basic YWRtaW46" \
http://unleash.herokuapp.com/api/admin/state/export?& featureToggles=1& strategies=0 > export.json
```
2021-06-04 11:17:15 +02:00
### API Import {#api-import}
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
> You should never use this in production environments.
2019-03-14 17:56:02 +01:00
Example usage:
```
POST /api/admin/state/import
{
"features": [
{
"name": "a-feature-toggle",
"enabled": true,
"description": "#1 feature-toggle"
}
]
}
```
2021-03-17 07:34:23 +01:00
Example with curl:
```sh
curl -X POST -H "Content-Type: application/json" \
-H "Authorization: Basic YWRtaW46" -d @export .json \
http://localhost:4242/api/admin/state/import
```
\*) Remember to set correct token for Authorization.
2021-06-04 11:17:15 +02:00
## Startup import {#startup-import}
2019-03-14 17:56:02 +01:00
2021-06-04 11:17:15 +02:00
### Import files via config parameter {#import-files-via-config-parameter}
2019-03-14 17:56:02 +01:00
You can import a json or yaml file via the configuration option `importFile` .
Example usage: `unleash-server --databaseUrl ... --importFile export.yml` .
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), specify the `dropBeforeImport` option.
> You should never use this in production environments.
2019-03-14 17:56:02 +01:00
Example usage: `unleash-server --databaseUrl ... --importFile export.yml --dropBeforeImport` .
These options can also be passed into the `unleash.start()` entrypoint.