Skip to content

Building a Shopify App with Node.js: A Detailed Guide

May 21, 2023 | React & Node.js, Shopify, Shopping Carts

In this article, we’re going to explore how to build a Shopify app using Node.js. Shopify has an extensive ecosystem of third-party apps that add new features and capabilities to Shopify stores. If you’re a Node.js developer, you can contribute to this ecosystem and even generate income from your apps.

 mkdir my-shopify-app && cd my-shopify-app

To follow along with this guide, you should have a basic understanding of JavaScript and Node.js. You’ll also need to have Node.js and npm installed on your machine.

Step 1: Setting Up Your Development Environment

Before we start coding, we need to set up our development environment. We’ll be using shopify-node-api which is a Shopify API library for Node.js. It helps simplify making requests to Shopify’s API.

First, create a new folder on your machine and navigate into it:

Then initialize a new Node.js project and install express, dotenv, and shopify-node-api: bash
 npm init -y
npm install express dotenv shopify-node-api

We use dotenv to load our environment variables securely.

Step 2: Registering Your Shopify App

Before we can interact with Shopify’s API, we need to register a new app in the Shopify Partners dashboard:

  1. Log in to your Shopify Partners account. If you don’t have one, you can create it for free on the Shopify Partners website.
  2. Once logged in, navigate to “Apps” -> “Create app”.
  3. Fill in the app name and app URL (for development, this can be https://localhost:8081, or another port of your choosing).
  4. Make note of the API key and API secret key that are generated. We’ll need them later.

Step 3: Setting Up the Server

Now we can start setting up our server. Create a new file server.js and add the following code:

Javascript:

 require('dotenv').config();
const express = require('express');
const Shopify = require('shopify-node-api');

const app = express();

const shopify = new Shopify({
  shop: 'your-shop-name', // replace with your shop name
  shopify_api_key: process.env.SHOPIFY_API_KEY,
  shopify_shared_secret: process.env.SHOPIFY_SECRET_KEY,
  shopify_scope: 'read_products',
  redirect_uri: 'https://localhost:8081/auth/callback',
  nonce: '' // you must provide a randomly selected value unique for each authorization request
});

app.get('/auth', (req, res) => {
  const authURL = shopify.buildAuthURL();
  res.redirect(authURL);
});

app.get('/auth/callback', (req, res) => {
  const { code } = req.query;
  shopify.exchange_temporary_token(req.query, (err, data) => {
    // data contains your access token
    res.send(data);
  });
});

app.listen(8081, () => console.log('Server is listening on port 8081'));

This script sets up a basic Express.js server with two routes. The /auth route starts the OAuth process, and the /auth/callback route handles the callback from Shopify. We’re using the shopify-node-api package to simplify the OAuth process.

Don’t forget to create a .env file in your project root and put your Shopify API key and secret there:

makefile
 SHOPIFY_API_KEY=yourapikey
SHOPIFY_SECRET_KEY=yoursecretkey

Step 4: Implementing Your App’s Functionality

This is where you would implement your app’s functionality. It could be anything from modifying product prices, managing inventory, or creating custom storefronts. The possibilities are endless.

For example, let’s say we wanted to fetch a list of all products. We could add a new route to do that:

javascript
 app.get('/products', (req, res) => {
  const params = { limit: 5 };
  shopify.get('https://eadn-wc02-1251331.nxedge.io/admin/products.json', params, (err, data) => {
    res.send(data.products);
  });
});

This route fetches the first 5 products from the store and sends them back in the response.

Step 5: Testing Your App

With our server set up, we can start it by running:

 bash


node server.js

Visit https://localhost:8081/auth in your web browser and you should be redirected to Shopify, where you can install your app and authorize it to access your shop. After authorizing the app, you should be redirected back to your server and see your access token.

You can then visit https://localhost:8081/products to see a list of your store’s products.

Conclusion

In this article, we’ve built a simple Shopify app using Node.js and the shopify-node-api package. This is just the tip of the iceberg, and there’s much more you can do with Shopify’s API. Be sure to check out the official Shopify API documentation for more information on what’s possible.

Contact Us Today!

"*" indicates required fields

This field is for validation purposes and should be left unchanged.
I would like to be contacted by:*
Select all that apply.

Join Our Newsletter List!

* indicates required
Test