Build an E-commerce Website Using Contentstack DataSync

This example e-commerce website is built using the data fetched through Contentstack DataSync.

View Demo View Code

Screenshots

Quickstart

Here’s a quick guide on how to create an e-commerce website using Contentstack DataSync and Node.js SDK.

Prerequisites

Note: For this tutorial, we have assumed that you are familiar with Contentstack and Contentstack DataSync. If not, then please refer to the docs (Contentstack docs and Contentstack DataSync) for more details.

Step 1: Create a Stack

Log in to your Contentstack account, and create a new stack. This stack will hold all the data, specific to your e-commerce website. Learn more on how to create a stack.

Step 2: Add a Publishing Environment

To add an environment in Contentstack, navigate to ‘Settings' -> 'Environment', and click on the '+ New Environment’ tab. Provide a suitable name for your environment, say ‘staging’. Specify the base URL (e.g., ‘http://YourDomainName.com’), and select the language (e.g., English - United States). Then, click on 'Save'. Read more about environments.

Step 3: Import Content Types

A content type is like the structure or blueprint of a page or a section of your web or mobile property. Read more about Content Types.

For this e-commerce website example, four basic content types are required: Header, Footer, Category, and Product. For quick integration, we have already created these content types. You simply need to import them to your stack. You can also create your own content types. Learn how to do this here.

To import the content types, first, save the zipped folder of the JSON files given below on your local machine. Extract the files from the folder. Then, go to your stack in Contentstack. The next screen prompts you to either create a new content type or import one into your stack. Click the "Import" link, and select the JSON file saved on your machine.

Here’s a brief overview of all the content types required for this project.

  • Header: This content type lets you add content for the header section of the site. 
  • Footer: The Footer content type enables you to add content for the footer section of your site. 
  • Categories: This content type lets you create the various categories of the products of your e-commerce website. The fields of this content type include name and URL.
  • Product: This content type will help you create the product entries of your e-commerce site. The fields include name, URL, category, price, image, and so on.

Download all content types from here.

Now that all the content types are ready, let’s add some content for your e-commerce website.

Step 4: Add Content

Create entries for the Category and Products content type. Add a few entries each for the Category and Products content type. Save and publish these entries. Learn how to create and publish entries.

Create entries for the Header and Footer content types. Using the header content type, add the name and other details of your site. Similarly, add footer content using the footer content type.

With this step, you have created sample data for your e-commerce site. The next step is to fetch the published data through Contentstack DataSync. 

Step 5: Initiate Contentstack DataSync

Start Contentstack DataSync to fetch all the published entries and assets onto your local storage. Read how to start Contentstack DataSync here.

Step 6: Build and Configure the Website

To get your app up and running quickly, we have created a sample e-commerce web app (using Node.js) for this project. You simply need to download it and change the configuration.

Download code from the GitHub.

Once you have downloaded the project, add Contentstack API Key, Delivery Token, baseDir (the base directory where Contentstack DataSync will syn the data), and the locales to the project. (Learn how to find your Stack's API Key and Delivery Token.)

The development.js file inside the config folder would look something like this:

 const config = {
 sdk: 'datasync-filesystem-sdk',
 locales: [
   {
     code: 'en-us',
     relative_url_prefix: '/'
   },
   {
     code: 'es-es',
     relative_url_prefix: '/es/'
   }
 ],
 contentStore: {
   baseDir: '../datasync-boilerplate/_contents'
 },
 assetStore: {
   baseDir: '../datasync-boilerplate/_contents'
 },
 port: 4000
}

module.exports = config

Fire up your terminal (command prompt or similar on Windows), point it to your project location and run the following:

$ npm install
$ npm start

This will initiate your project. However, you will not be able to fully use the checkout process until the project is live on a domain.

If you want to use MongoDB instead of Filesystem, learn more how to do it here.

Was this article helpful?
top-arrow