Importing, Exporting, & Transferring Data with Strapi CLI
Tags: strapi, datamigration
Published: 2023-05-16 10:28:28

As a developer working with Strapi in the past, there have been many scenarios where I wished I could have easily taken a backup of my data. To export my data from one Strapi instance and then import it back to another Strapi instance when needed was a frequent chore. Thanks to the Strapi CLI, this is now a trivial task. 

In this article, we will show how you can export your data from one instance and then import it again to another instance with a couple of simple commands.

While importing and exporting data is useful, sometimes you want to push or pull data you already have in a local Strapi instance to a remote Staging instance. You also may want to pull production data down to a local Strapi instance for testing. This is where the data transfer commands come in handy. We will also demonstrate how to use these commands in the Strapi CLI as well.

Read on to learn how to use the import, export, and data transfer options in the CLI.

## Prerequisites

Before you can jump into this content, you need to have a basic understanding of the following.

1. Basic knowledge of JavaScript
2. A [Node.js](https://nodejs.org/en) ready environment
3. Basic understanding of Strapi - get started [here](https://docs.strapi.io/dev-docs/quick-start).

## What is Strapi?

Strapi is an open-source headless CMS based on Node.js that is used to develop and manage content using a Restful API and/or GraphQL.

With Strapi, we can scaffold our API faster and consume the content via APIs using any HTTP client or GraphQL enabled frontend.

## Scaffolding a Strapi Project

Scaffolding a new Strapi project is very simple and works precisely as installing a new front-end framework.

We are going to start by running the following commands and testing them out in our default browser.

```javascript
npx create-strapi-app strapi-api --quickstart
# OR
yarn create strapi-app strapi-api --quick start
```

The command above will scaffold a new Strapi project in your specified directory.

Next, run yarn build to build your app and yarn develop to run the new project if it doesn't start automatically.

The last command will open a new tab with a page to register your new admin of the system. Go ahead and fill out the form and click on the submit button to create a new Admin.


## Using the Strapi CLI Export then Import

We will use the Strapi blog template as a starter in this example. We have one project populated with the starter data and another with **the same schema** but no data. 

It is important to note that your schemas must match the Strapi data import/export CLI commands to succeed. Another important thing to note is that Strapi will not copy over admin users, API tokens, or third-party content, strictly files and content within the Strapi instance. 

In our start blog, you can see our two projects below: our project with content and our other project that currently does not have any content.

![project with content](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/Project_with_Content_48a8a7bfc8.png "Instance with Content")

![project with no content](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/strapi_without_content_cff56fd2e3.png "Instance without Content")

The first thing we have to do is export the content from our populated instance. Many options can be applied to this CLI command, but we will just stick to the basics in this example. You can see a complete list of CLI commands in [the documentation](https://docs.strapi.io/dev-docs/data-management/export).

![strapi export command](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/export_command_c2503bae4f.png "Strapi CLI Export")

Now that we have exported our data and encrypted the export, we can move over to our empty project. We will import this data in the console for our empty project by running the following commands from the CLI. Again, further options can be found in [the documentation](https://docs.strapi.io/dev-docs/data-management/import).

![strapi import command](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/import_command_7ef0cf481f.jpg "Strapi CLI Import")

And just like that, we now have our content from our local environment replicated in our remote staging environment. Below is a screenshot again of our local environment and our remote staging environment, except this time after using the Strapi CLI export and then import features.

![project with content after export](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/strapi_with_content_after_export_f01e965cc2.png "Instance with Content After Export")

![project with new content after export](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/strapi_with_imported_content_834c60178b.png "Instance with new Content After Import")

## Pushing Data to a Remote with Strapi Transfer

Next, we will use the same Strapi blog template project to demonstrate how to push our local content to a remote project (our staging environment). We first have to get an authorization token from our remote project to do this. This token will be a push token allowing us to push data into the remote instance. You can create this token in the Strapi admin, as shown below.

![push transfer token admin](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/transfer_token_a9965423f9.png "Push Transfer Token Admin")

Now that we have our push token, we can run the Strapi transfer CLI command. Again, before this is run, you must ensure that the schema of your local project and your remote project match. In this case, they do as both projects are the Strapi Blog Template, except our remote is missing the start content. Below you can see us run the transfer command. For all the options of the transfer command, you can [read the docs](https://docs.strapi.io/dev-docs/data-management/transfer).

![data transfer command](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/strapi_transfer_command_6607662e46.png "Strapi CLI Transfer")

And just like that, we have our data from our local environment pushed into our remote! It's really that easy. Below is a screenshot of first our local environment and second our remote.

![local project with content](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/local_transfer_with_content_6777b41973.png "Local Instance with Content")

![remote project with content](https://d2zv2ciw0ln4h1.cloudfront.net/uploads/remote_transfer_with_content_c0f73f9923.png "Remote Instance with new Content After Transfer")


## Wrap Up

From our examplese above you can see that both the import and export CLI features as well as the data transfer CLI features are welcoming additions to the Strapi CLI. When you need to create a backup for an archive or simply to import to another project, the Import and Export features of the Strapi CLI should be your go to. Likewise, if you want to push or pull data to or from a remote instance, the Strapi CLI Transfer feature should be your go to. I hope you enjoyed this article, thanks for reading. 

> If you prefer to follow a video tutorial for importing and exporting data with the Strapi CLI, check our [YouTube channel](https://www.youtube.com/@Strapi/featured).{% youtube nZ5CRZTGkMQ %}