How to Build an E-commerce Website with Nuxt and Cosmic JS

What we will be building.

For simple shopping websites, going with full-fledged solutions like Shopify or Woocommerce can be a total overkill. In this tutorial, we’re going to build an e-commerce website using Nuxt and Cosmic JS. We’ll also use Buefy, which is a UI component library for Vue JS based on the much loved Bulma.



You need to have node and npm installed in your machine already. If not, visit node js website and install the latest stable version.

Getting Started

For our project, it makes sense to build our backend models first.

Setup Cosmic JS backend

Login to your Cosmic JS dashboard and select ‘Add new Bucket’. In the next screen, provide a name for your Bucket and select ‘Start from scratch’.

Import Bucket data

Download the bucket.json file from the repository. Then go to Settings -> Import/Export page from the dashboard and import the downloaded file. Now we’ll have the objects and object types available in the dashboard.

Setting up Stripe

For any e-commerce website, we’ll have to accept payment from the customer at one point or another. Setting up a payment processor used to be a difficult area, but luckily we now have a lot of convenient solutions.

For our website, we’ll use Stripe. To start with, go to their website and create an account. After you fill in details and everything’s setup, go to the API section and grab your public and private keys.
We’ll need these keys to configure Stripe in our website, using the much convenient Stripe Checkout element.

Installing the Demo

cd vue-ecommerce
npm install
There is one more step left before running the app. Rename the


file to 


and file in the enviroment variables there.

Since the HOST variable for production needs to be, and NODE_ENV needs to be ‘production’, we’ll set them both in


file, and is not needed in the 



Now run

npm run dev

to run the app in development mode. If you want to build for production, run

npm run build

and start the app with

npm start

Understanding the Source Code

One major perk of using Nuxt for building Vue.js applications is that Nuxt is opinionated. So one can jump into any Nuxt project and find a familiar structure there.

The most important file is the


. You’ll find three plugins imported from the


folder for Buefy, Stripe and Cosmic JS.

import Vue from 'vue';
import VueStripeCheckout from 'vue-stripe-checkout';

const options = {
  key: process.env.STRIPE_PUBLIC_KEY,
  image: '',
  locale: 'auto',
  currency: 'USD',
  billingAddress: true,
  panelLabel: 'Pay {{amount}}'

Vue.use(VueStripeCheckout, options);

Any environment variable that need to be used in the client side needs to be defined in the env section and we’ll also have stripe script linked in the scripts section.



folder contains the home and cart pages. The checkout method int the cart page handles the stripe checkout. Basically what we’re doing is sending the token generated by the checkout component and other identifying details to the app server. Checkout the vue-stripe-checkout package to learn more about the usage.

For a simple Nuxt app, we can just use server only for SSR and have all the code shipped to client side. But in this case, we need to handle the stripe checkout separately in the server. Inside the


folder, we have the Express server setup in


file and a single route handler in


file. We’ll first create a customer in Stripe with


function and then charge with



Once the payment is successful, we’ll add the order to our Cosmic JS Bucket under the ‘Orders’ object type.

let content = ''
items.forEach(item => {
  content+= `${item.title} x ${item.count}, `
const params = {
  type_slug: 'orders',
  title: name,
  content: content,
  metafields: [
      key: 'Address',
      type: 'text',
      value: address
      key: 'City',
      type: 'text',
      value: city
cosmic.addObject(params).then(data => {
}).catch(err => {

Deploying the App

Deploying the app with Cosmic JS is pretty easy. Just go to your Bucket and then navigate to Settings -> Deploy to Web.

Provide your Github repository url and the branch to be deployed. Cosmic makes the Bucket slug, read key and write key environment variables available by default.

Choose the repository there and branch to deploy (which in most cases is master), and click ‘Deploy to Web’. That is all there is to it and you will get a confirmation mail once deployment is successfull. Once it succeeds, do not forget to supply the stripe keys and environment variables through the ‘Set Environment Variables’ section in case you haven’t setup .env file.

PS: Any e-commerce app needs to be secured with https. So do enable SSL in your app from deployment section when going live.

read original article here