A minimal e-commerce store using Gatsby, SANITY, Stripe, Use-Shopping-Cart and Netlify

Overview

πŸͺ Gatsby Starter Stripemart

Like a supermarket but for Stripe.

No ongoing monthly costs. Perfect for artists, creators and independent builders doing the side hustle shuffle!

This is a basic and minimalist e-commerce storefront built with Gatsby, SANITY, Stripe, Use-Shopping-Cart, and Netlify.

The main features are:

  • πŸ”₯ Blazing fast frontend with Gatsby
  • πŸ“¦ Product and inventory management with SANITY
  • πŸ”’ Secure payments and cart validation with Stripe
  • πŸ’½ Serverless Jamstack architecture with Netlify
  • πŸ›’ Shopping cart state management with Use-Shopping-Cart

Some nice-to-haves:

  • 🚚 Optional support for basic shipping calculations
  • 🎁 Optional support for add ons (e.g. wrapping paper) in the checkout flow
  • πŸ“ Optional support for free local pickup
  • πŸ’Έ Optional support for free shipping

The benefits of using this set of technologies together is that there are no ongoing monthly costs - you only pay the Stripe processing fees when you actually sell something.

Demo

⚠️ Heads up!

A few words of caution. If you have an active and ongoing business you should probably not use this starter!

  • Please consider using Shopify or Snipcart. They are great products and are way better for most businesses, most of the time. They have whole teams of security and performance experts. Amazing integrations. I am one developer only.
  • This starter will never reach v1.0, it is intended as a barebones starter that you can fork and make your own! I will likely never add more significant features because there are better products for a complete e-commerce experience. You can read the roadmap below to see what is planned however.
  • The intended audience is other developers with some prior experience with Jamstack technologies

🧱 Tech stack

I chose this tech stack because it is one I know really well, and am confident in.

  • Gatsby (frontend), /web
  • SANITY (backend), /studio
  • Stripe (payments)
  • Use-Shopping-Cart (cart state management)
  • Netlify (hosting)
  • Theme-UI (styling)
  • React-Hook-Form (form validation)
  • Reach-UI for a11y cart modal
  • Framer Motion (animations)

πŸš€ Getting started

To use this starter properly you will need the Gatsby CLI, the SANITY CLI, the Netlify CLI and the Stripe CLI.

You will need to create or already have a Stripe account, a SANITY account, and a Netlify account.

I assume you are somewhat familiar with Jamstack/modern frontend web development in this getting started guide. Having said that, please don't hesistate to reach out if you have a question or a step is too vague.

  1. Use this template to create a fresh copy of these files into your own GitHub account. Clone and download the files to start working locally.
  2. Run yarn install or npm install in the /web and /studio folders. The web folder is your Gatsby frontend. The studio folder is your SANITY backend.
  3. Create a .env.development and a .env.production following the format in .example.env. We will add the information we need in a bit.
  4. Inside of /studio run sanity init to initialize a new database for the store. You can use the default options.
  5. Type sanity manage to open your project's management console, then go to Settings > API > Tokens, and use the Add new token button to open the token creation dialog. You need a token with read + write privileges. Copy and paste this into your .env files. While you are here add http://localhost:8888 to your CORS origins with credentials.
  6. You will also need the projectId and dataset which can be found in /studio/sanity.json. Copy and paste these into your .env files.
  7. To make life easier you can import some basic starting data. Run this command to seed some basic data into the database, cd data sanity dataset import production.tar.gz.
  8. You will need to deploy the graphql data layer for Gatsby, run sanity graphql deploy inside of the /studio directory.
  9. Now we need to hop over to Stripe and get your secret key and public key. You should be using test keys which start with sk_test_... and pk_test_.... You can read more about Stripe Api Keys. Copy and paste these into your .env files as well.
  10. Whew! Almost there! Run netlify dev to start the Netlify development server (which will also compile Gatsby). If you have done everything right you should see your glorious new store on http://localhost:8888!
  11. Last step! Now you need to setup a local Stripe webhook to fire when a purchase has been successful(checkout.session.completed). The Stripe CLI lets you setup a test webhook listener with the command stripe listen --forward-to localhost:8888/.netlify/functions/handle-purchase. After running this command it will tell you what your webhook secret is, something like whsec_.... Add this to the .env files. Now when there is a successful purchase this serverless function will update the stock on the SANITY backend.

πŸŽ‰ πŸŽ‰ Congrats! You now have a basic e-commerce store up and running using the latest and greatest in Jamstack tech! Deploy that greatness!

Deployment

  • Double check CORS origins when you deploy, you will likely need to make sure they are setup correctly
  • You will need to set a live webhook endpoint for the handle-purchase function
  • Don't forget to set all those .env variables correctly in Netlify
  • If you want to auto-publish from SANITY to Netlify you can use a webhook for this
  • Stripe webhook secrets for production are going to be different than what you are running locally when testing, make sure you update them!
  • Keep those API keys safe! Make sure you don't check those into git.

Cart validation and security

The main cart validation is handled on the backend by Stripe. All payments are handled securely by Stripe. If the product price passed from the frontend to Stripe and the price listed in the backend with SANITY do not match then the transaction will not be approved. There are some other basic checks that also happen before a purchase is completed, e.g. is there enough stock, is the shipping calculated correctly, is shipping actually enabled.

I have also included some basic validation on the frontend, e.g. if you select a stock of "-5" if will show an error. It also checks the stock number against the quantity selected, try selecting a quantity above the current listed stock and you will also see an error.

Most of this happens in web/netlify/functions/handle-checkout.ts and web/netlify/functions/handle-purchase.ts which are Netlify serverless functions.

Store settings

The following store settings area available in Site Settings > Store Settings. These allow for configuration of some Stripe settings. If you want to manually update the Stripe checkout settings see the file in web/netlify/functions/handle-checkout.ts.

Shipping

Shipping is optional and can be toggled off in the store settings, see above.

There are three different types of shipping available, all of them are optional. Local pickup, Standard shipping, and Free shipping. Each can be toggled on and off if you don't want to offer a local pickup option for example.

Local shipping and free shipping are pretty straightforward. Note the cutoff amount for the free shipping can be set to whatever value you want.

Standard shipping is calculated using a formula. You set a shipping percentage, a minimum amount, and a maximum amount. This is then used to calculate the shipping presented to the customer, and validated again server side.

So for example imagine you set the following values:

  • Shipping percent: 20%
  • Minimum shipping: $5.00
  • Maximum shipping: $20.00

If you had a $15.00 item shipping should be calculated as the minimum value of $5.00. If the item was $45.00 the shipping would be calculated using the percentage as $9.00. If the item was $180.00 the maximum shipping amount would be charged of $20.00.

This formula isn't perfect, but it isn't trying to be either 🀷 . This gives some flexibility in shipping charged with minimal work on your part. If you need perfectly calculated shipping fees your probably need a more formal e-commerce product, see the warning above.

Add ons

Add ons are optional and can be toggled off in the store settings, see above.

This allows you to add an item that is available for purchase in the checkout cart, for example giftwrap or some kind of other small item a customer may want to add onto their order. These add on items are functionally the same as all other products, but they are not shown in the store, only in the add ons area of the checkout.

Styling with Theme-UI

Theme-UI is a css-in-js library with a constraints based design system. You can find the main theme file in src/styles/theme.js. Try changing around some of the colors and you will see how quickly you can customize the look and feel of the site.

Theme-UI is based on Emotion and is very similar to Chakra-UI if you are familiar with that library.

Roadmap

  • Check stock on last time on payment_intent_created. Right now stock is checked on the frontend, and checked serverside before reaching checkout, but it would be good to check it again one last time just before payment goes through.
  • Complete transition to typescript. I have made most of the important files and functions typescript however want to finish transitioning the entire repo and fix some of the places I cheated with :any.
  • Maybe add a search function
  • Better form inputs in SANITY for currencies, right now they are just number or integer inputs. It would be nice if these showed a dollar sign and proper punctuation.
  • Any and all a11y improvements!
  • Maybe some tests with Cypress, this is a bit of a stretch goal, but something I might do if there is some momentum with the project.
  • Add support for Gatsby Cloud. I was hoping to do this with Gatsby functions however they do not play nice with Stripe currently and so went back to tried-and-true Netlify functions.

Contribution.

All contribution is welcome however I have no plans to add major features to this starter other than those mentioned above in the roadmap. Would love some help to improve TS and any a11y improvements!

You might also like...

Minimal E-Commerce Store built with NextJS using Shopify Storefront API

NextJS Shopify Store Minimal E-Commerce Store built with NextJS using Shopify Storefront API Usage: run: npm install && npm run dev OR If you have doc

Nov 29, 2022

A wee E-Commerce website powered by Stripe, Auth0, Strapi and Next.js

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Dec 19, 2022

A Gatsby-based starter theme with e-commerce styled components

A Gatsby-based starter theme with e-commerce styled components

This beautiful theme from the Matter Design Team gives you the styling and scaffolding for your next e-commerce site. You can customize to your heart'

Jan 3, 2023

A Gatsby storefront for a Shopify store.

Leah Gardner Storefront A Gatsby storefront for a Shopify store. Built as a learning project for Shopify and Gatsby integration. This project is boots

Dec 14, 2022

Stablo is a minimal blog website template built with Next.js, TailwindCSS & Sanity CMS

Stablo is a minimal blog website template built with Next.js, TailwindCSS & Sanity CMS

Stablo Blog Template - Next.js & Sanity CMS Stablo is a JAMStack Starter template built with Next.js, Tailwind CSS & Sanity CMS by Web3Templates. Clic

Dec 30, 2022

A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed

A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed

WordPressWooCommerceDashboard - A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed. This program provides shipping tracking for Delhivery.

Jan 3, 2022

A demo to show how to re-use Eleventy Image’s disk cache across Netlify builds.

Re-use Eleventy Image Disk Cache across Netlify Builds Live Demo This repository takes all of the high resolution browser logos and processes them thr

Apr 5, 2022

This is a demo of updating a map to show air quality data for the user’s current location using Next.js Advanced Middleware, powered by Netlify Edge Functions.

Show Local Air Quality Based on the User's Location Use AQI data to show the air quality near the current user. This is built using Next.js Advanced M

Nov 4, 2022

A new Node.js resource built using Gatsby.js with React.js, TypeScript, and Remark.

Nodejs.dev Nodejs.dev site built using Gatsby.js with React.js, TypeScript, SCSS, and Remark. You can find the latest Figma design protype here. πŸš€ Ge

Jan 5, 2023
Releases(v0.1.2)
Owner
Eric Howey
Mental health therapist and occasional web developer. Working at the intersection of care and code.
Eric Howey
This is an imitation of the Amazon online shopping store. Users can add and remove items from the cart, proceed to checkout and make payments.

AMAZON This is an imitation of the Amazon online shopping store. Users can add and remove items from the cart, proceed to checkout and make payments.

Kevv_in 7 Mar 23, 2022
NextJS E-commerce starter kit with Sanity.io and Stripe API πŸ›

?? Next.js Sanity E-commerce Starter Kit A Next.js E-commerce app with Sanity.io and Stripe API. Built with TailwindCSS framework & SASS CSS extension

Lougie Quisel 11 Dec 29, 2022
Shopping Cart - Project of HTML, CSS, and JavaScript developed by me (Raphael Martins) at the end of the Unit 9 Module 1 of the Web Development course at Trybe

Project of HTML, CSS, and JavaScript developed by me (Raphael Martins) at the end of the Unit 9 Module 1 of the Web Development course at Trybe. I was approved with 100% of the mandatory and optional requirements met.

Raphael Martins 13 Nov 27, 2022
Shopping cart built with React

Shopping Cart In this project, I performed operations such as listing products, adding products to the cart and removing products from the cart, which

Efecan PΔ±nar 6 Jun 17, 2021
A build plugin to integrate Gatsby seamlessly with Netlify

Essential Gatsby Plugin This build plugin is a utility for supporting Gatsby on Netlify. To support build caching and Gatsby functions on Netlify, you

Netlify 72 Dec 27, 2022
Gatsby-starter-minimal-blog - Typography driven, feature-rich blogging theme with minimal aesthetics.

Gatsby-starter-minimal-blog - Typography driven, feature-rich blogging theme with minimal aesthetics. Includes tags/categories support and extensive features for code blocks such as live preview, line numbers, and line highlighting.

Priya Chakraborty 0 Jan 29, 2022
Gatsby-blog-cosmicjs - πŸš€βš‘οΈ Blazing fast blog built with Gatsby and the Cosmic Headless CMS πŸ”₯

Gatsby + Cosmic This repo contains an example blog website that is built with Gatsby, and Cosmic. See live demo hosted on Netlify Uses the Cosmic Gats

Priya Chakraborty 0 Jan 29, 2022
Starter-gatsby-blog - Gatsby starter for a Contentful project from the community.

Contentful Gatsby Starter Blog Create a Gatsby blog powered by Contentful. Static sites are scalable, secure and have very little required maintenance

Priya Chakraborty 0 Jan 29, 2022