Building an E-commerce site with Shopify

In this tutorial, you will setup a new Gatsby website that fetches product data from Shopify. The site displays a list of all products on a product listing page, and a page for every product in the store.

If you are already comfortable with Gatsby and Shopify, you might want to check out the Gatsby Shopify starter, which provides many of the same features as this example.

Setting up your Shopify account

  1. Create a new Shopify account and store if you don’t have one.
  2. Create a private app in your store by navigating to Apps, then Manage private apps.
  3. Create a new private app, with any “Private app name” and leaving the default permissions as Read access under Admin API.
  4. Enable the Shopify Storefront API by checking the box that says “Allow this app to access your storefront data using Storefront API”. Make sure to also grant access to read product and customer tags by checking their corresponding boxes.

Set up the Gatsby Shopify plugin

  1. If you do not already have one ready, create a Gatsby site.

  2. Install the gatsby-source-shopify plugin and shopify-buy package.

  1. Enable and configure the plugin in your gatsby-config.js file, replacing [some-shop] with your shop name and [token] with your Storefront access token.
  1. Run gatsby develop and make sure the site compiles successfully.

Querying Shopify data and listing products

Open the Gatsby GraphiQL interface by visiting http://localhost:8000/___graphql. With at least one example product added into Shopify you should see several new types of nodes in the Explorer tab, like allShopifyProduct. To query all products in your store sorted by title, try running the query:

To add a simple page listing all products, add a new file at /src/pages/products.js.

Generating a page for each product

You can programmatically create pages in Gatsby for every product in your Shopify store.

Create a template for your product pages by adding a new file, /src/templates/product.js.

Edit your gatsby-node.js file and add the following code. This queries all Shopify products with GraphQL, then creates a page using the template in product.js. Each page gets assigned a URL in the format of /products/[product handle].

Additional Resources