Frequently Asked Questions

Getting Started & Implementation

How do I set up an eCommerce store using Next.js and Hygraph?

To set up an eCommerce store with Next.js and Hygraph, start by creating a Next.js app, then use Hygraph to manage your product data. You can fetch products from Hygraph using Apollo Client and GraphQL queries. The process involves setting up your schema in Hygraph, adding product content, and integrating Snipcart for cart and checkout functionality. For a detailed guide, see the original tutorial.

What are the prerequisites for building a Next.js eCommerce store with Hygraph?

You should have a basic understanding of HTML, CSS, JavaScript, and some experience with React and Next.js. You'll also need Node.js and npm or yarn installed, and a Snipcart account for cart functionality. See the Getting Started section for details.

How do I create and manage product data in Hygraph?

In Hygraph, you create a schema for your products, add fields such as name, image, description, and price, and then use the editing interface to add product items. Save and publish each item to make it available for your Next.js app. For step-by-step instructions, refer to the Hygraph section of the tutorial.

How do I fetch product data from Hygraph in my Next.js app?

You can fetch product data from Hygraph using Apollo Client and GraphQL queries. Use the Content API endpoint provided by Hygraph, and construct queries to retrieve product details. The getStaticProps function in Next.js is used to fetch data at build time. See the code examples in the tutorial.

How do I implement dynamic routing for products in Next.js?

Dynamic routing in Next.js is achieved by creating files with square brackets, such as pages/products/[productslug].js. Use getStaticPaths to generate paths for each product based on its slug, and getStaticProps to fetch product data for each route. See the dynamic routing section for details.

How do I integrate Snipcart for shopping cart and checkout in my Next.js store?

To integrate Snipcart, install its scripts globally in your Next.js app using a custom _document.js file. Add your Snipcart API key to the hidden div, and use the snipcart-add-item class on your "Add to cart" buttons. For payment gateways, configure them in your Snipcart dashboard. See the tutorial for details.

How do I make the cart summary appear as a sidebar in Snipcart?

To display the cart summary as a sidebar, add data-config-modal-style="side" to the hidden Snipcart div in your _document.js file. This configures the modal to appear as a sidebar instead of a full screen.

How do I update the Navbar price to reflect the cart total?

Use the snipcart-total-price class on the price element in your Navbar, and snipcart-checkout to make the price clickable and open the cart summary. This ensures the Navbar displays the actual cart total and provides quick access to the cart.

How do I add a payment gateway in Snipcart?

Sign in to your Snipcart account and visit the payment gateway page in the dashboard. Choose from up to five available gateways, create an account, and click "Connect" to authorize access. For more details, see the tutorial section.

Can I localize product data in Hygraph for my eCommerce store?

Yes, Hygraph supports product data localization, allowing you to manage content in multiple languages for global audiences. This is especially useful for international eCommerce stores. Learn more about localization in Hygraph's documentation.

Where can I find the starter repository for the Next.js eCommerce tutorial?

The starter repository for the Next.js eCommerce tutorial is available on GitHub. It contains all the code and assets needed to follow along with the tutorial.

How do I use Apollo Client with Hygraph's GraphQL API?

Install Apollo Client and GraphQL in your Next.js app, then initialize Apollo Client with your Hygraph Content API endpoint. Use GraphQL queries to fetch product data and pass it to your components. See the code examples in the tutorial for implementation details.

How do I customize the cart appearance in Snipcart?

You can customize the cart appearance by modifying the modal style in the Snipcart configuration and updating the CSS. For sidebar appearance, use data-config-modal-style="side". For further customization, refer to Snipcart's documentation.

How do I handle product images in my Next.js eCommerce store?

Store product images in Hygraph and reference their URLs in your Next.js components. Use Next.js image optimization features to ensure fast loading and better SEO. See the tutorial for code examples.

How do I use getStaticProps and getStaticPaths for product pages?

Use getStaticProps to fetch product data at build time and getStaticPaths to generate dynamic routes for each product based on its slug. This enables static generation of product pages for better performance and SEO.

Can I use Hygraph for other types of content besides products?

Yes, Hygraph is a flexible content management platform that can be used for any type of content, including blogs, media, and structured data. You can define custom schemas and models to fit your needs.

How do I secure my Hygraph API endpoints?

Hygraph allows you to configure permissions and access controls for your API endpoints. You can set up granular permissions for different roles and use audit logs to track changes. For more details, see Hygraph's security features.

What technical documentation is available for Hygraph?

Hygraph provides extensive technical documentation, including API reference, schema components, webhooks, and AI integrations. Access all resources at the Hygraph Documentation page.

How long does it take to implement Hygraph for an eCommerce project?

Implementation time varies by project complexity. For example, Top Villas launched a new project in just 2 months using Hygraph. The platform offers structured onboarding, training resources, and a free API playground for quick starts. See case study for details.

Features & Capabilities

What are the key features of Hygraph for eCommerce and Next.js?

Hygraph offers GraphQL-native architecture, content federation, scalability, localization, asset management, and integration with third-party APIs like Snipcart. It supports fast content delivery, user-friendly tools, and enterprise-grade security. Learn more on the features page.

Does Hygraph support integrations with other platforms?

Yes, Hygraph supports integrations with Digital Asset Management systems (Aprimo, AWS S3, Bynder, Cloudinary, Imgix, Mux, Scaleflex Filerobot), Adminix, Plasmic, and custom APIs via REST and GraphQL. Explore more in the Integrations Documentation.

What APIs does Hygraph provide?

Hygraph offers multiple APIs: Content API (read/write), High Performance Content API (low latency, high throughput), MCP Server API (AI assistant integration), Asset Upload API, and Management API. See the API Reference Documentation for details.

How does Hygraph ensure high performance for content delivery?

Hygraph features high-performance endpoints for low latency and high read-throughput. It actively measures GraphQL API performance and provides optimization advice in its GraphQL Report 2024. Learn more in the performance blog post.

What security and compliance certifications does Hygraph have?

Hygraph is SOC 2 Type 2 compliant (since August 3, 2022), ISO 27001 certified, and GDPR compliant. It offers enterprise-grade security features like granular permissions, audit logs, SSO, encryption, and regular backups. See security features for details.

How easy is Hygraph to use for non-technical users?

Hygraph is praised for its intuitive user interface and ease of setup. Non-technical users can create, update, and manage content independently, reducing developer bottlenecks. Real-time changes are visible on the front-end. See customer feedback on Hygraph for Enterprise.

What pain points does Hygraph solve for eCommerce teams?

Hygraph addresses developer dependency, legacy tech stack modernization, content inconsistency, workflow challenges, high operational costs, slow speed-to-market, scalability issues, complex schema evolution, integration difficulties, performance bottlenecks, and localization challenges. See case studies for examples.

What business impact can I expect from using Hygraph?

Customers report improved operational efficiency, accelerated speed-to-market, cost efficiency, enhanced scalability, and better customer engagement. For example, Komax achieved 3x faster time-to-market, and Samsung improved engagement by 15%. See case studies for more.

What industries use Hygraph for eCommerce and content management?

Hygraph is used in SaaS, marketplace, education technology, media and publication, healthcare, consumer goods, automotive, technology, fintech, travel and hospitality, food and beverage, eCommerce, agency, online gaming, events & conferences, government, consumer electronics, engineering, and construction. See case studies for examples.

Who are some of Hygraph's customers?

Notable customers include Samsung, Dr. Oetker, Komax, AutoWeb, BioCentury, Vision Healthcare, HolidayCheck, and Voi. See customer case studies for details.

Can you share specific success stories of customers using Hygraph?

Yes. Samsung built a scalable API-first application, Komax managed 20,000+ product variations across 40+ markets, AutoWeb increased website monetization by 20%, and Voi scaled multilingual content across 12 countries. See case studies for more.

Pricing & Plans

What pricing plans does Hygraph offer?

Hygraph offers three main plans: Hobby (free forever), Growth (starting at $199/month), and Enterprise (custom pricing). Each plan includes different features and limits. See pricing page for details.

What features are included in the Hygraph Hobby plan?

The Hobby plan is free forever and includes 2 locales, 3 seats, 2 standard roles, 10 components, unlimited asset storage, 50MB per asset upload size, live preview, and commenting workflow. Sign up here.

What features are included in the Hygraph Growth plan?

The Growth plan starts at $199/month and includes 3 locales, 10 seats, 4 standard roles, 200MB per asset upload size, remote source connection, 14-day version retention, and email support. Get started here.

What features are included in the Hygraph Enterprise plan?

The Enterprise plan offers custom limits, version retention for a year, scheduled publishing, dedicated infrastructure, global CDN, security controls, SSO, multitenancy, instant backup recovery, custom workflows, dedicated support, and custom SLAs. Try for 30 days or request a demo.

Competition & Comparison

How does Hygraph compare to traditional CMS platforms?

Hygraph is the first GraphQL-native Headless CMS, offering simplified schema evolution, content federation, and seamless integration with modern tech stacks. It eliminates developer dependency and supports global teams, setting it apart from traditional REST-based CMS platforms. See Headless CMS Comparison.

Why choose Hygraph over other headless CMS solutions?

Hygraph stands out for its GraphQL-native architecture, content federation, enterprise-grade features, scalability, user-friendly tools, and proven ROI. It ranked 2nd out of 102 Headless CMSs in the G2 Summer 2025 report and is voted easiest to implement. See G2 report and case studies.

How does Hygraph solve pain points differently than competitors?

Hygraph offers a user-friendly interface, GraphQL-native architecture, and content federation, addressing developer dependency, integration challenges, and content inconsistency more effectively than traditional CMS platforms. Its cost efficiency and performance optimization further differentiate it. See case studies for examples.

Introducing Click to Edit

How to build an eCommerce store in Next.js

In this guide, we will learn how to build a plant’s e-commerce store with Next.js that fetches all its products from Hygraph and also uses Snipcart to handle shopping operations and cart management systems easily.
Joel Olawanle

Written by Joel 

Aug 10, 2022
Next.js eCommerce

Next.js is a lightweight framework for React applications that allows us to build server-side rendering and static applications in React easily. It takes all the good parts of React and makes it even easier to get an app running with optimized rendering performance.

Here is the project GitHub repository; you can clone or download the complete project.

#Why Next.js for eCommerce

Next.js abstracts a lot of the complexity away for us, so we can build our eCommerce application easily having so many features that align with the specifications required for eCommerce projects, such as:

  • Live collaboration tooling: Next.js Live generates a live URL for your project that you can use to share, chat, draw, and edit code in the browser. This results in an improved collaboration between you and your team.
  • Image optimization: This aids in seamless product discovery by allowing images to load faster, resulting in a better product discovery experience for consumers and higher conversion rates.
  • Built-in internationalization: This is extremely useful for global brands because they can statically serve different languages on your site at build time and in record speed. This is accomplished through internationalization routing.
  • SEO: Next.js makes our e-commerce site easier for search engines to crawl, which leads to more traffic and, ultimately, more sales. Next.js, for example, has a feature called next/head that allows us to append elements to the page's head, such as title and meta tags.
  • Popularity & vast community: Any issue has probably been documented already, so you're likely to find a solution to any potential pitfalls in your way.
  • Next.js features like server-side rendering and static exporting push these React benefits even further by guaranteeing that your website/app will be SEO-friendly. This is something vital to any e-commerce business.

Here are some Next.js e-commerce site examples to help you see examples of many companies and platforms that now make use of Next.js for their eCommerce applications.

#Architecture

For this app, we will use next.js to create an e-commerce application that retrieves products from Hygraph via Apollo client and queries data using GraphQL. We will also use Snipcart to easily integrate a cart system into our e-commerce application. More information on Architectures for Modern eCommerce Applications can be found here.

#Getting Started

We will build a plant store, this store will contain a few plants, their name, prices, and pictures. At the end of this guide, we should have an eCommerce app that functions like the one below:

nextjs ecommerce

Requirements/Prerequisites

To understand this guide and code along, you should have:

  • Basic understanding of HTML, CSS, and JavaScript
  • At least a little experience or knowledge of React and maybe Next.js.
  • Node and npm or yarn installed on your computer
  • A Snipcart account (forever free in test mode).

Note: This is a "follow along" type of article. You'll get a good result if you keep building. If you're only interested in how this works with Hygraph and Snipcart, you can skip the development phase or clone the starter repository (check out the readme page for instructions on how to use it), which has all the codes covered in the development phase working properly.

Setting up Next.js development environment

Let's start by preparing our environment, so we can start building. In your preferred terminal, type the following command:

npx create-next-app ecommerce-demo

A prompt will appear asking you to confirm the name of the project. After that, it will install all of the project dependencies for you. Then, run npm run dev to make our app available at localhost:3000.

Defining a layout

Next.js allows us to create a layout to hold files that will be repeated throughout our application, such as our Header and Footer Components, which are fixed across all pages of our e-commerce application.

Once we are done setting up the layout component, we will add it to the app's main entry point which is located at pages/_app.js via MyApp function which returns the pageProps.

Creating Layout components

For this, we will create a Layout component as well as two additional components for our application's Header and Footer. To begin, we will create a components folder in the base folder, where we will now store all components. Begin by writing the following code in a Header.js file:

// components/Header.js
import Link from 'next/link';
import styles from '../styles/components/Header.module.css';
const Header = () => {
return (
<nav className={styles.navbar}>
<Link href="/">
<a>
<div className={styles.logo}>
<p>
PLANTS <span className={styles.logo_span}></span>
</p>
</div>
</a>
</Link>
<div className={styles.nav_price}>
<span>🛒</span>
<p>$0.00</p>
</div>
</nav>
);
};
export default Header;

Still, in the components directory, let’s also create a Footer.js file for the footer with the following content:

// components/Footer.js
import styles from '../styles/components/Footer.module.css';
const Footer = () => {
return (
<footer className={styles.footer}>
<p>All Rights Reserved. Copyright © 2022</p>
</footer>
);
};
export default Footer;

Integrating components into Layout

Let's now integrate those components into our app by first creating a Layout component that will take the children's props and place them wherever we want. In our case, we want the following pages to appear between the header and footer components:

// components/Layout.js
import Header from './Header';
import Footer from './Footer';
const Layout = ({ children }) => {
return (
<div>
<Header />
{children}
<Footer />
</div>
);
};
export default Layout;

Finally, with our layout components created, all that's left to do is to add it to the _app.js file, so as to configure our app’s layout:

// _app.js
import Layout from '../components/Layout';
import '../styles/globals.css';
function MyApp({ Component, pageProps }) {
return (
<>
<Layout>
<Component {...pageProps} />
</Layout>
</>
);
}
export default MyApp;

If you launch your app in dev mode and navigate to your localhost page, you should now see your app's layout. To obtain the CSS files from the styles folder, go to the starter repository. Now it's time to give our homepage the content it deserves.

Customizing our homepage

So far we have been able to set up our app layout, let’s now work on the index/home page of our application which is the pages/index.js file.

Note: Ensure you visit the repository, to get the products array via pages/data/products.json to get all the data, also ensure you get the images from public/images folder.

For this, we will also pre-render the data we will be getting from pages/data/products.json via getStaticProps, which fetches data at build time. This is what our code will look like:

// pages/index.js
import allProducts from './data/products.json';
import Head from 'next/head';
import Link from 'next/link';
import styles from '../styles/Home.module.css';
export default function Home({ allProducts }) {
return (
<>
<Head>
<title>Plants | Home</title>
</Head>
<div className="container">
<h2 className={styles.title}>
All Products <span>🌿</span>
</h2>
<div className={styles.products_container}>
{allProducts.map((product) => {
return (
<div className={styles.product_card} key={product.id}>
<Link href={`products/${product.slug}`}>
<a>
<div className={styles.product_img}>
<img src={product.image.url} alt={product.name} />
</div>
</a>
</Link>
<div className={styles.product_content}>
<h3>{product.name}</h3>
<p>${product.price}</p>
<button className="btn">Add to cart 🛒</button>
</div>
</div>
);
})}
</div>
</div>
</>
);
}
export async function getStaticProps() {
return {
props: {
allProducts,
},
};
}

We returned the data as props and then looped through all the products using JavaScript’s map() iterator method.

Dynamic routing for each product

When a user attempts to purchase a product, the user will first click on the product to read more information about that specific product. On the development side, you may believe that individual pages are created for each product, but this will be cumbersome and difficult to maintain. This is the reason for dynamic routing.

This would be properly implemented when we fetch data from Hygraph, but for now, let’s create a component in a folder inside the pages' folder. This would look like this pages/products/[productslug].js. In this component, we will add the following code:

// pages/products/[productslug].js
import Head from 'next/head';
import styles from '../../styles/SingleProduct.module.css';
const singleproduct = () => {
return (
<>
<Head>
<title>Dracaena fragrans</title>
</Head>
<div className={styles.single_container}>
<div className={styles.left_section}>
<img src="/images/croton.png" className={styles.left_img} alt="" />
</div>
<div className={styles.right_section}>
<h3 className={styles.title}>Dracaena fragrans</h3>
<p className={styles.price}>$50</p>
<div className={styles.para}>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. At
impedit voluptatum vitae labore molestiae, maiores, hic ad
officiis laudantium in officia, nam vel quod! Nesciunt aperiam
explicabo facere laboriosam eius.
</p>
</div>
<button className="btn">Add to cart 🛒</button>
</div>
</div>
</>
);
};
export default singleproduct;

We manually entered all the data in the preceding code, but we will retrieve them from Hygraph shortly. So far, we have been successful in establishing our e-commerce application. If anything confuses you, or if you were unable to configure your app to produce similar results, make sure to check out the starter repository and compare your codes.

#Hygraph

Hygraph is a cutting-edge content management platform that enables teams to provide content to any channel. If this is your first time exploring Hygraph, create a free-forever developer account via either GitHub, Mail, e.t.c.

We will be making use of Hygraph to store and manage all the products we will be using for our e-commerce web application. Once we have created an account or signed In, we can now create a project using any project name of our choice and location that is close to you. At this point, we now have our project set up and the next step would be to create our schema.

Set up Schema

Schema is the structure we develop for storing our content on Hygraph and for us to set up our schema, we can either click on the schema icon in the sidebar or the highlighted steps as seen below:

schema setup in hygraph

The next step would be to create a model, since we are creating an e-commerce application the major model we need to create is the products model to store all our products

set up product model in hygraph

The next step would be to add our desired fields for the content we wish to store, such as product name, image, description, price, e.t.c

final product model

Note: slug is generated from the name template as seen below:

generate slug from a template

Create & Add content

Once we are done setting up our schema the next step would to start creating/adding our products to the product model based on the fields we created, to do this we can either visit the editing interface via the icon on the sidebar or the highlighted steps as seen below:

create content - add new products.png

We can now click on the model we want to create items for, suppose we have multiple models, and then go ahead to click the “create item” button. Ensure you save and publish them as soon as you are done inputting each content. Once that is done, your model will now have some contents we can fetch into our eCommerce store.

product list in hygraph

Explore Content API

So far we have been able to set up Hygraph and also added our content, the next step would be to learn how to query Hygraph. We would make use of the inbuilt GraphQL Explorer to create our query, we do this by visiting the Hygraph API playground as seen below:

explore the hygraph content api

This would take us to the GraphQL Explorer where we can create our query easily even if we are not used to GraphQL by just ticking the boxes:

graphql playground

Make API Accessible

At this point we have our contents, and we have been able to come up with a query, but we also need to make our API accessible so that these contents can be fetched via the API, to do this we can either click the settings icon on the sidebar or the highlighted steps as seen below:

make hygraph api accessible

We can now initialize by defaults or maybe create permissions if you wish:

initialize defaults on the api

We would now click on Endpoints to get our content API, which would be used to get content from Hygraph when fetching our data via Apollo client in our Next.js web app.

copy api endpoint

Note: Ensure you keep this API link as we would make use of it soon.

Fetch data from Hygraph with GraphQL

Once we have successfully set up Hygraph and created our GraphQL query, the next step would be to query Hygraph via our Next.js web app. The first step would be to install Apollo client and GraphQL, which would enable us to construct the query we would send with Apollo:

npm install @apollo/client graphql

Once this is successful, the next step would be to initialize Apollo client by first importing Apollo client, InMemoryCache and gql from @apollo/client

import { ApolloClient, InMemoryCache, gql } from '@apollo/client';

Once we have imported, we can now proceed to initialize Apollo client to allow us to fetch via our query later on, but for us to initialize, it would be best to make use of the getStaticProps() function so that the content is fetched at build time:

export async function getStaticProps(context) {
return {
props: {},
}
}

We can now initialize apollo client within the getStaticprops() function as seen below:

export async function getStaticProps() {
const client = new ApolloClient({
uri: 'https://api-us-east-1.hygraph.com/v2/cl2cupjxo4izz01z8fcm26gxf/master',
cache: new InMemoryCache(),
});
return {
props: {},
};
}

Note: In the code above, we will make use of the content API we copied from Hygraph Endpoints.

Once that is done, the next step would be to get our data via the GraphQL query we created while exploring the Hygraph content API as seen below:

const data = await client.query({
query: gql`
query ProductsQuery {
products {
id
name
slug
price
image {
url
}
}
}
`,
});

At the end of the day, our getStaticProps() function will look like this:

export async function getStaticProps() {
const client = new ApolloClient({
uri: 'https://api-us-east-1.hygraph.com/v2/cl2cupjxo4izz01z8fcm26gxf/master',
cache: new InMemoryCache(),
});
const data = await client.query({
query: gql`
query ProductsQuery {
products {
id
name
slug
price
image {
url
}
}
}
`,
});
const allProducts = data.data.products;
return {
props: {
allProducts,
},
};
}

Note: We assigned the products array to a new variable, so it’s easier to work with, you can decide to log out the data in your console/terminal to be sure everything works fine. If you are not familiar with getStaticProps() function check here.

Making use of data

So far we have been able to fetch our data via getStaticProps() function so that the content is fetched at build time, let's now make use of these props within our web application by passing in the props:

import Link from 'next/link';
export default function Home({ allProducts }) {
return (
<div className="container">
<h2 className="title">All Products 🌿</h2>
<div className="products-container">
{allProducts.map((product) => {
return (
<div className="product-card" key={product.id}>
<Link href={`products/${product.slug}`}>
<div className="product-img">
<img src={product.image.url} alt={product.name} />
</div>
</Link>
<div className="product-content">
<h3>{product.name}</h3>
<p>${product.price}</p>
<a className="btn">Add to cart 🛒</a>
</div>
</div>
);
})}
</div>
</div>
);
}

In the above, we destructured props to get the variable without needing to make use of props.*allProducts*. We looped through the data, and our app should now look like this:

ecommerce store built with nextjs and hygraph

Dynamic Page creation for each product

For this, we will make use of getStaticPath() function which allows us to define all the paths to be statically generated for that route. Simply we want to be able to access a route link - http://localhost:3000/products/slug where the slug would be dynamic based on the product being fetched.

To do this we are going to make use of dynamic routes which allows us to create dynamic routes with square brackets so that inside the bracket we have a parameter that can be used as the path to dynamically request the page we want to display.

To do this, we would create a file: products/[productSlug].js, then the next thing will be to tell next.js every single path and the slug that corresponds to the path. For this, we will use getStaticPath() function which would return an object, but this time around not props but an array of paths. This would be done similarly to what we fetched contents earlier:

The first step would be to import apollo client, InMemoryCache and gql from @apollo/client

import { ApolloClient, InMemoryCache, gql } from '@apollo/client';

And then we can now initialize and query Hygraph via apollo client:

export async function getStaticPaths() {
const client = new ApolloClient({
uri: 'https://api-us-east-1.hygraph.com/v2/cl2cupjxo4izz01z8fcm26gxf/master',
cache: new InMemoryCache(),
});
const data = await client.query({
query: gql`
query ProductsQuery {
products {
id
name
slug
price
image {
url
}
}
}
`,
});
const paths = data.data.products.map((singleProduct) => {
return {
params: {
productslug: singleProduct.slug,
},
};
});
return {
paths,
fallback: false,
};
}

In the above code, you will also notice that we looped through the entire products and returned all the slugs, and then returned the entire slugs via the paths array.

The next step would be to make use of the getStaticProps() function like before to query Hygraph for the particular product that matches our slug, let’s first visit Hygraph API playground to come up with the GraphQL query which would check for a particular slug parameter:

fetch products by slug.gif

Once that is done, we can now make use of the query in the getStaticProps() function:

export async function getStaticProps({ params }) {
const client = new ApolloClient({
uri: 'https://api-us-east-1.hygraph.com/v2/cl2cupjxo4izz01z8fcm26gxf/master',
cache: new InMemoryCache(),
});
const data = await client.query({
query: gql`
query MyQuery($slug: String) {
product(where: { slug: $slug }) {
id
name
price
slug
description {
html
}
image {
url
}
}
}
`,
variables: {
slug: params.productslug,
},
});
const product = data.data.product;
return {
props: {
product,
},
};
}

Note: at the end of the query request, we added the variables property to set the slug we want to fetch.

Once this is successful, we can now add this props to this web app page so we can display the fetched content:

const singleproduct = ({ product }) => {
return (
<div className="container single-container">
<div className="left-section">
<Image src={product.image.url} width={300} height={700} alt="" />
</div>
<div className="right-section">
<h3>{product.name}</h3>
<p className="price">${product.price}</p>
<div
dangerouslySetInnerHTML={{
__html: product.description.html,
}}
></div>
<a className="btn">Add to cart 🛒</a>
</div>
</div>
);
};
export default singleproduct;

#Implementing Shopping cart and checkout with Snipcart

Hygraph alone can't provide out-of-the-box eCommerce experiences, but paired with other APIs, it's possible to connect Hygraph with a variety of eCommerce APIs to deliver rich buying experiences. You can check here for some headless commerce APIs of which we would be making use, one of which is Snipcart.

Snipcart makes it easy to add a shopping cart to any website and also add a payment gateway easily. The first step is for us to register or sign in, activate our account, and fill in all necessary details. For this guide, we will make use of the test mode just to explain the development process, I also recommend you first work with the test mode before switching to live mode.

Installation

Let’s get started by installing Snipcart to our web application, we need to create a custom document page which is going to allow us to modify outside the body and load the Snipcart installation scripts so that they load globally. This file will be created in the pages folder and named _document.js. Paste the code below into the file:

import { Html, Head, Main, NextScript } from 'next/document';
export default function Document() {
return (
<Html>
<Head>
<link rel="preconnect" href="<https://app.snipcart.com>" />
<link rel="preconnect" href="<https://cdn.snipcart.com>" />
<link
rel="stylesheet"
href="https://cdn.snipcart.com/themes/v3.2.0/default/snipcart.css"
/>
</Head>
<body>
<Main />
<NextScript />
<script async src="https://cdn.snipcart.com/themes/v3.2.0/default/snipcart.js"></script>
<div hidden id="snipcart" data-api-key="YOUR_PUBLIC_API_KEY"></div>
</body>
</Html>
);
}

In the above code, we have imported Snipcart globally. The only thing we have to do now inserts our API Key into the hidden div. You can get your API key from the dashboard credentials page. Ensure you restart your server anytime you update this page.

#Adding products to the cart

At this point, we now have to work on our “Add to cart” button so that anytime it’s clicked for a particular item, that item would be added to the cart. To do this, we add some attributes to our button, these attributes consists of information like the product name, price, description, images, etc.:

<button
className="btn snipcart-add-item"
data-item-id={product.id}
data-item-price={product.price}
data-item-url={`products/${product.slug}`}
data-item-image={product.image.url}
data-item-name={product.name}
>
Add to cart 🛒
</button>

At this point, when you add any item to the cart, a page like this will appear having the information about the item:

adding snipcart checkout

We can customize our cart, but for this guide, we will only make our cart appear as a sidebar rather than a full screen. To do that, we will add data-config-modal-style="side" to the hidden div in the _document.js file.

<div hidden id="snipcart" data-api-key="YOUR_PUBLIC_API_KEY" data-config-modal-style="side"></div>

Note: Let’s ensure we update the “Add to cart” button on the single products page which is being fetched dynamically, so it can also be used to add products to the cart as it should.

#Working on Navbar price

Finally, we need to make the price tag on the Navbar reflect the actual total price, and also when we click the price we want the cart summary to open up as a sidebar, so we see the items in our cart.

adding the cart status information on the frontend

With Snipcart, we perform all these functions by just adding classes to our markup. To add the total price to the tag we add *className*="snipcart-total-price" while to make the text clickable, so it shows the cart summary, we add *className*="snipcart-checkout" meaning, the price section of our Navbar will look like this:

<nav className="navbar container">
...
<div className="nav-price snipcart-checkout">
<span>🛒</span>
<p className="snipcart-total-price">$0.00</p>
</div>
</nav>

#Add Payment gateway in Snipcart

Finally, adding a payment gateway in Snipcart is very easy, all you have to do is ensure you are signed in and then visit the payment gateway page. On this page you will find up to 5 payment gateways, make use of anyone you prefer as all you just have to do is create an account and then click on the connect button to authorize access.

snipcart available payment gateways

#Conclusion

In this guide, we were able to learn how to create an eCommerce application that retrieved content from Hygraph using this guide. There is a lot that can be done with Hygraph, one of which is product data localization. We also learned how server-side data fetching works, as well as how to integrate Snipcart to handle carting and checkout.

Blog Author

Joel Olawanle

Joel Olawanle

Joel Olawanle is a Frontend Engineer and Technical writer based in Nigeria who is interested in making the web accessible to everyone by always looking for ways to give back to the tech community. He has a love for community building and open source.

Share with others

Sign up for our newsletter!

Be the first to know about releases and industry news and insights.