1 | ---
|
2 | id: import_export
|
3 | title: Import & Export
|
4 | ---
|
5 |
|
6 | _since v3.3.0_
|
7 |
|
8 | 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.
|
9 |
|
10 | All import mechanisms support a `drop` parameter which will clean the database before import (all strategies and features will be removed).
|
11 |
|
12 | > You should never use this in production environments.
|
13 |
|
14 | ## Runtime import & export
|
15 |
|
16 | ### State Service
|
17 |
|
18 | Unleash returns a StateService when started, you can use this to import and export data at any time.
|
19 |
|
20 | ```javascript
|
21 | const unleash = require('unleash-server');
|
22 |
|
23 | unleash.start({...})
|
24 | .then(async ({ stateService }) => {
|
25 | const exportedData = await stateService.export({includeStrategies: false, includeFeatureToggles: true});
|
26 | await stateService.import({data: exportedData, userName: 'import', dropBeforeImport: false});
|
27 | await stateService.importFile({file: 'exported-data.yml', userName: 'import', dropBeforeImport: true})
|
28 | });
|
29 | ```
|
30 |
|
31 | If you want the database to be cleaned before import (all strategies and features will be removed), set the `dropBeforeImport` parameter.
|
32 |
|
33 | > You should never use this in production environments.
|
34 |
|
35 | ### API Export
|
36 |
|
37 | The api endpoint `/api/admin/state/export` will export feature-toggles and strategies as json by default.\
|
38 | You can customize the export with queryparameters:
|
39 |
|
40 | | Parameter | Default | Description |
|
41 | | -------------- | ------- | --------------------------------------------------- |
|
42 | | format | `json` | Export format, either `json` or `yaml` |
|
43 | | download | `false` | If the exported data should be downloaded as a file |
|
44 | | featureToggles | `true` | Include feature-toggles in the exported data |
|
45 | | strategies | `true` | Include strategies in the exported data |
|
46 |
|
47 | For example if you want to download all feature-toggles as yaml:
|
48 |
|
49 | ```
|
50 | /api/admin/state/export?format=yaml&featureToggles=1&download=1
|
51 | ```
|
52 |
|
53 | ### API Import
|
54 |
|
55 | You can import feature-toggles and strategies by POSTing to the `/api/admin/state/import` endpoint (keep in mind this will require authentication).\
|
56 | 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).
|
57 |
|
58 | If you want the database to be cleaned before import (all strategies and features will be removed), specify a `drop` query parameter.
|
59 |
|
60 | > You should never use this in production environments.
|
61 |
|
62 | Example usage:
|
63 |
|
64 | ```
|
65 | POST /api/admin/state/import
|
66 | {
|
67 | "features": [
|
68 | {
|
69 | "name": "a-feature-toggle",
|
70 | "enabled": true,
|
71 | "description": "#1 feature-toggle"
|
72 | }
|
73 | ]
|
74 | }
|
75 | ```
|
76 |
|
77 | ## Startup import
|
78 |
|
79 | ### Import files via config parameter
|
80 |
|
81 | You can import a json or yaml file via the configuration option `importFile`.
|
82 |
|
83 | Example usage: `unleash-server --databaseUrl ... --importFile export.yml`.
|
84 |
|
85 | If you want the database to be cleaned before import (all strategies and features will be removed), specify the `dropBeforeImport` option.
|
86 |
|
87 | > You should never use this in production environments.
|
88 |
|
89 | Example usage: `unleash-server --databaseUrl ... --importFile export.yml --dropBeforeImport`.
|
90 |
|
91 | These options can also be passed into the `unleash.start()` entrypoint.
|