How to Integrate Shopify into your Strapi Website
Tags: shopify, strapi, cms, ecommerce
Published: 2021-09-01 10:39:52

Shopify is a subscription service that lets you create, manage and sell your products or services.
When combined with Strapi and its ability to customize and manage content, you have on your hands a sleek, powerful website or app that is both attractive to your customers and very easy to use.

In this tutorial, we will create a Shopify store to sell custom-made clothes and integrate our store into our Strapi-powered website.

## Outline
- Introduction
    - Goal
    - Prerequisites
- Setup Strapi
    - Create the Strapi project.
    - Register your first admin user.
    - Create content types.
    - User permissions
- Setup Shopify
    - Create a Shopify account.
    - Create a "private app" on Shopify.
    - Create products on your Shopify store.
- Integrate Shopify into Strapi
    - Install Shopify API package for NodeJS.
    - Customize controllers for the products.
    - Add Shopify constant with API details.
    - Get metadata from Shopify.
    - Create a product entry on Strapi.
    - Verify that it works.
- Summary
- Resources

## Introduction

## Goal

By following this tutorial, you will learn how to integrate Shopify into your Strapi website using the Shopify API. This tutorial aims to guide you through the entire process by breaking it into small, easy-to-follow steps, ensuring that you understand each step before moving on to the next one.


## Prerequisites

To follow this tutorial, you need to have the following:

- Basic understanding of JavaScript
- [NodeJS](https://nodejs.org/)
- [npm](https://www.npmjs.com/)
- [Yarn](https://classic.yarnpkg.com/lang/en/)
- A Shopify store. Shopify offers a 14-day free trial for new accounts, which will be sufficient for this project. Click [here](https://www.shopify.com) to get started.
- The latest version of [Strapi](https://strapi.io/). Previous knowledge of Strapi will help you understand and follow this tutorial quicker, but you can follow along just fine if you don't have prior experience.


## Setting up Strapi

Below are the steps to set up Strapi account and create a new collection.

### Create the Strapi project.

Open your terminal, navigate to the folder where you want to store your project, and run the following command:

```bash
    npx create-strapi-app strapi-shopify --quickstart
```

or using `yarn`

```bash
    yarn create strapi-app strapi-shopify --quickstart
```

When the project has been successfully created, `cd` into your project folder `strapi-shopify` and run the following command in your terminal:

```bash
    npm run develop
```

or using `yarn`

```bash
    yarn develop
```

### Register your first admin user

When you run `yarn develop` or `npm run develop` in your terminal, it will build the UI for your admin dashboard and automatically open `localhost:1337/admin` on your browser with the following content:


![Register admin user](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1627994436637_Screenshot+2021-08-03+at+13-38-51+Register-admin.png)


Fill the form with your relevant details and click on "LET'S START" to access the admin dashboard.


![Strapi Admin Dashboar](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1627994833039_Screenshot+2021-08-03+at+13-46-11+Homepage.png)



### Create content types

Click on the "Content-Types Builder" tab under Plugins and create a new collection type named "Product."

![Create a collection type](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628014232561_Screenshot+2021-08-03+190840.jpg)


When you click "Continue," a modal will pop up to provide you with options to create fields for your collection type:

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628018712225_Screenshot+2021-08-03+191311.jpg)


We will add four fields to our collection type:

- "Text" field for the name of our product
- "Rich Text" field for our product description
- "Media" field for our product image
- "Text" field to put in our Shopify ID

Select "Text" to add a field for your product name

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628015198175_Screenshot+2021-08-03+191810.jpg)


Click on "+ Add another field" and select "Rich Text" to add a field for your product description.


![Add Rich Text field for product description](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628019129256_Screenshot+2021-08-03+191902.jpg)


Click on "+ Add another field" and select "Media" to add a field for your product image.


![Add field for Media](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628019381871_Screenshot+2021-08-03+191938.jpg)


Click on "+ Add another field" and select "Text" to add a field name `shopifyID`. This will contain the ID of your Shopify product.

![Add field for Shopify ID](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628019652536_Screenshot+2021-08-03+192150.jpg)


When you are done, click the "Finish" button. Strapi will automatically restart your server to implement your changes.

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628120576698_Screenshot+2021-08-03+192311.jpg)



### User permissions

We need to let our users view our products whether they are authenticated or not.
Click on "Settings" located on your dashboard's sidebar, then scroll down to "Users & Permissions Plugin" and click on "Roles."


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628121275341_Screenshot+2021-08-05+at+00-54-12+Settings+-+Roles.png)


Click on "Public" to edit permissions for public users. Scroll down to the "Permissions" section and check the box "findOne" to enable users to see products by their ID.

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628131311246_Screenshot+2021-08-05+034128.jpg)


 Click on “Save” to save your permission updates.


## Setup Shopify

Below are the steps to set up a new Shopify store account.

### Step 1: Create a Shopify account

If you haven't already created your Shopify account and store, open [www.shopify.com](https://www.shopify.com) on your browser, click on "Start a free trial," and fill in the appropriate details for your store.


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628051230055_Screenshot+2021-08-04+at+05-25-41+Start+a+Business+Grow+Your+Business+-+Shopify+14-Day+Free+Trial.png)


If you successfully create your account, you will see something like this. For this tutorial, the name of our store is Divi Apparels.


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628051777914_Screenshot+2021-08-04+at+05-33-58+Divi+Apparels++Home++Shopify.png)



### Create a “private app” on Shopify

Shopify apps are used to add new features to your store or enhance admin capabilities. There are three main app types available on Shopify: Public, custom, and Private apps. Public apps can be installed from Shopify's app store or a Shopify developer's website. 

Custom apps are built specifically for your Shopify store by a developer and don't require API or Admin access to your store. Private apps are also only available to your store, and they require API and Store Admin access. 

We will need this API access to be able to integrate Shopify into Strapi.

On the sidebar of your store's dashboard, click on "Apps" and then click "Manage private apps."

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628054331684_Screenshot+2021-08-04+061314.jpg)


You might see a warning screen saying, "Private apps are currently disabled," along with some extra info to explain why.


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628054543379_Screenshot+2021-08-04+at+06-20-40+Divi+Apparels++Apps++Private+apps++Shopify.png)


Click on "Enable private app development," tick the boxes, and enable private apps.
Next, create your private app by clicking on "Create private app."


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628054865811_Screenshot+2021-08-04+062635.jpg)


In the "App Details" section, put in a name for your Shopify private app and an email address to get technical updates.


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628055195831_Screenshot+2021-08-04+063251.jpg)


Scroll down to the "Admin API" section and click on "Show inactive Admin API permissions."

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628074532839_Screenshot+2021-08-04+063616.jpg)


In the dropdown menu, scroll down until you locate "Products," click on "No Access," and select "Read Access."


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628076847627_Screenshot+2021-08-04+115918.jpg)


Then scroll down to the end of the page and click "Save."

Congratulations, you have successfully created your first private app. Please take note of your API credentials as you will need them later.


### Create products on your Shopify store.

You can skip this step if you already have some products on your Shopify store. If you don't have any, then follow along:

On the sidebar of your store's dashboard, click on "Products" and click on "Add Products."

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628119140163_Screenshot+2021-08-05+at+00-18-02+Divi+Apparels++Products++Shopify.png)


Go ahead and add details of your product, such as its title, description, pictures, pricing, etc.
Be sure to change your product's status from "Drafts" to "Active" when you're ready to display it in your store.

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628119475313_Screenshot+2021-08-05+002319.jpg)


After adding your product details, click on the "Save" button located at the top-right corner of your dashboard's navbar to save your product. Repeat the same process to add as many products as you wish to your store.

## Integrate Shopify with Strapi

Below are the steps to integrate Strapi API with Strapi:

### Install Shopify API package for NodeJS.

[Shopify API](https://www.npmjs.com/package/shopify-api-node) is a handy little package that allows you to connect your website or app to your Shopify store using the API keys you created.

Open your terminal and run the command:

```bash
    npm install --save shopify-api-node
```
 
 or using yarn

```bash
    yarn add shopify-api-node
```

### Customize controllers for the products

In your browser, open the [Strapi documentation](https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#controllers) on customizing controllers in your Strapi backend, scroll down to "Collection Type" in the "Core Controllers" section. 

We are going to copy the `findOne` controller function and modify it to fit our project. 


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628125557623_Screenshot+2021-08-05+020237.jpg)


In your project folder, navigate to `api/product/controllers/products.js` and replace its content with the code above.

To modify it for our project, replace the `restaurant` with the `product` or whatever you named your `collection-type` in your Strapi dashboard. This is how the code will look when you're done:

```
    const { sanitizeEntity } = require("strapi-utils");
    module.exports = {
      /**
       * Retrieve records.
       *
       * @return {Object}
       */
    
      // Find Product by ID
      async findOne(ctx) {
        const { id } = ctx.params;
        const entity = await strapi.services.product.findOne({ id });
        return sanitizeEntity(entity, { model: strapi.models.product });
      },
    };
``` 


### Add Shopify constant with API details

Go to the [Shopify API GitHub page](https://github.com/MONEI/Shopify-api-node#example) and copy the [boilerplate code](https://github.com/MONEI/Shopify-api-node#example) to create a `shopify` constant that will contain your API information. 

Paste this code in the same file as your controllers `(api/product/controllers/products.js`) above your `module.exports`


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628133083526_Screenshot+2021-08-05+041004.jpg)


Replace `'your-shop-name'` with the name of your Shopify store, which you can get from your browser's search bar


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628127736847_Screenshot+2021-08-05+024158.jpg)


Replace `'your-API-key'` with the API key you created for your Shopify private app and `'your-app-password'` with your private app's password. Make sure you input these details as strings.


### Get metadata from Shopify

We need to tell our `findOne` controller to get our product's metadata using our Shopify API details.

In your `findOne()` function, below the `entity` constant, type in this code:

```
    entity.shopify = await shopify.product.get(entity.shopifyID);
```

Your `product.js` file should look like this when you’re done:

![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628162928171_Screenshot+2021-08-05+122814.jpg)


Save your changes to restart your server.


### Create a product entry on Strapi.

We will create a product entry that matches the product we wish to get from our Shopify store. On your Shopify admin dashboard, click on "Products" in the sidebar and click on the product you wish to add to your Strapi website.


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628137157348_Screenshot+2021-08-05+at+05-18-47+Divi+Apparels++Products++Potato+Love+Hoodie++Shopify.png)


In the search bar of the tab displaying your Shopify product, copy the product ID.


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628137265105_Screenshot+2021-08-05+052011.jpg)


In a separate browser tab, open your Strapi admin dashboard, and select "Products" under "Collection Types," then click on "+ Add New Products." Fill in the exact details as you did in the Shopify store, add an image, and in the `shopifyID` section, paste the ID you copied from the Shopify website.


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628137406674_Screenshot+2021-08-05+at+05-23-01+Strapi+-+Content+Manager.png)


Click on “Save” and then click on “Publish”.


### Verify that it works

Open a new browser tab and type http://localhost:1337/products/1 to see the metadata for the first product you created. It should look like this on Firefox:


![](https://paper-attachments.dropbox.com/s_E38AAF7F2A0E2CE97BD53F156C730F7D46BA812E4C4C0522EE7E62F74A0F4936_1628137460964_Screenshot+2021-08-05+at+05-24-05+Screenshot.png)


Congratulations! You've successfully integrated Shopify into your Strapi website.


## Resources
1. [Quick Start Guide - Strapi Documentation](https://strapi.io/documentation/developer-docs/latest/getting-started/quick-start.html)
2. [Understanding the Shopify API - YouTube](https://www.youtube.com/watch?v=U2nIleAHQXE)
3. [App Types - Shopify](https://help.shopify.com/en/manual/apps/app-types)

## Summary

In this tutorial, you learned how to integrate your Shopify store into your Strapi website. Check out the source code for this tutorial [here](https://github.com/dpkreativ/strapi-shopify).

You can take it a step further and build the website's front-end part using your preferred framework.