How to build an e-commerce website with Nuxt and Cosmic JS

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.

TL;DR

Pre-requisites

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.

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.

The pages/ 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 server/ folder, we have the Express server setup in index.js file and a single route handler in api/index.js file. We'll first create a customer in Stripe with stripe.customers.create function and then charge with stripe.charges.create function. Check out https://stripe.com/docs/checkout/express to learn more about the implementation.

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

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.

Resources

Company

Builder Buckets and Free Trial Credits

You can have up to 5 Builder Buckets at any given time each with a 14-day trial. During this trial, you have free access to all Bucket Add-ons including Webhooks, Localization, Revision History and more.

At any point during of your 14-day trial you can upgrade or use the Builder Bucket Referral Program to extend your Builder Bucket free trial time.

Referral Program

Refer a friend to signup and get 14 more days of trial time which can be applied to any of your Builder Buckets. There is no limit to the number of referrals you can use to get extended trial time.

Overage Policy

Overage rate: $8 per additional 10,000 API requests per month.

If you go over your plan's API request limit for the month, Cosmic JS support will reach out to inform you. Your service will never be interrupted without outreach from Cosmic JS. At the point of contact you will have the choice to either pay overages or upgrade your Bucket to the next higher plan.

Stars

How to build an e-commerce website with Nuxt and Cosmic JS Stars

Stars

App Title

Install App using the Cosmic CLI

Run the following code, one line at a time, in your preferred command line tool.
Go to the Cosmic CLI NPM page for more info.