Importing Data From CSV Files to Contentstack

The import and export scripts allow you to migrate content from other systems (CMS) to Contentstack. Besides migrating content from other content management systems, you can also migrate content from a CSV file and move it to Contentstack.

By using this script, you can export content types, entries from a CSV file and convert them into a suitable format which can then be imported into Contentstack using the import script.

Format of the CSV File

The details of the content types and entries in the CSV file should be in the following format:  

  • The content type name should be the same as that of the CSV filename.
  • The content type schema should be defined in the header row (first row) of the CSV file. It is suggested to keep two partitions here: one without reference and one with references.

    Below is the CSV file that does not use any references:

    //Insert the image

    Here is the one with references:

    //insert the image

  • The "title" in the header row is the name of the field separated by the | character with type (data type) as string.
  • In case of multi-referencing and single-referencing, the third string separated by the | character will be the referenced content type. So in the above screenshot, category is the referenced content type.
  • And in the "related_post | type:singleref | blog" field blog is the referenced content type.

NoteIt is mandatory for your CSV file to follow the above format.

Things to keep in mind about the CSV file format:

  • If the blog content type refers to the author content type, the author content type should be a new content type and uploaded along with the blog content type.
  • Serial numbers in the above images act as unique identifier for every entry in that content type.
  • No entry UID should clash with another entry UID of another content type, especially in the when you are references.
  • Cross referencing is not supported because the script requires the referred content type to exist in order to work. In case of a single reference, do not use colon separated string as they are supported only in multi referencing.

Install the Script and Set it up

Now that we have the data to be imported ready in the CSV file, let's move ahead and install the script by following the steps given below:

  1. Download this script from our GitHub repo.  
  2. Once the script is downloaded, unzip it. Fire up your terminal and run the following command:
    npm install
    The above command will install the required node dependencies (node modules).
  3. Open the script in any code editor of your choice and open the index.json file which is inside the config folder. 
  4. If you are exporting a single file, you need to add the relative path of the file inside the 'csv_filename' key. 
  5. If you are exporting multiple files into Contentstack, you need to add the relative path of the folder (that stores your CSV files) in the 'csv_folder' key.

    NoteIf 'csv_folder' is specified in the config, it will override the value of the 'csv_filename' key.

Export the Modules

Now that the basic setup is ready, let's export the content of the CSV file by exporting the modules. You can either add all modules or only specific modules depending upon your requirements. 

Staying inside the project root directory, run the following command to export all the modules: 

npm run export

The above command will extract the data from the CSV files and convert them into the required JSON format that is supported in Contentstack. These files will be stored in the path mentioned in the 'data' key in the 'config/index.json' file. 

To export a specific module, run the following command:

npm run export-contenttypes
npm run export-entries

You can find the logs of the export process under libs/utils/logs. The files included are 'success' and 'error'. Successfully performed processes are recorded under 'success' and the errors are logged under 'errors'. 

Import Content

Copy the 'contents' folder from your project and paste it in the path mentioned in the 'data' key within the 'config/index.js' file.

NoteFor adding references, ensure that you have imported the referenced content type before. 

Now, run the contentstack-importer script to import the content to Contentstack. Read more about importing content in Contentstack using the import script.