You are here

How to Build a Shopify Application with Node.js and Express

In this tutorial, you’ll learn how to build a Shopify app from scratch with Node. You’ll learn how to request an access token from a shop and then make an API call. You can find the full tutorial here at the Shopify API blog

Getting Started

To get things going, you’ll need to create a public HTTPS address for the purposes of Authentication. You can do this with ngrok by starting an ngrok tunnel on port 3000 in your terminal and then copying the created HTTPS address into Shopify as your app URL.

Creating and Configuring Your App

Next step is to create an app in the Spotify Partner Dashboard. For this, you’ll need to enter your app URL following by ‘/shopify’. Then once the app is created configure it by clicking ‘app info’ and then entering your app’s Callback url, which will be the HTTPS address plus ‘/shopify/callback’. In the app credentials section, find out your API key and secret key to use as environment variables in your app. 

Create a Node.js Project

Now you can start on your app. Create a Node.js project by creating a folder named shopify-express-application, and then running ‘npm init’ from within the folder to create a package. JSON file. Then enter the app name as shopify-express-application and press return to accept all the defaults. 
You’ll then need to install all the packages for your app. So run ‘npm install express dotenv cookie nonce request request-promise --save’ in the terminal. Once this is done, create an .env file and enter your Shopify API Key and API secret key like so:


Finally, create a .gitignore file that contains ‘.env’ to make sure your credentials will never be made public via GitHub or any other public Repo

Building your Node.js app

To start your app, create an index.js file with the following content:

Remember to add your own ngrok HTTPS address in the code before running the app in the terminal. 

Creating Shopify Routes

First off, add two Shopify routes to your app, the install and callback routes. The install route uses the shop URL to redirect the merchant to the Shopify app authorization prompt to accept or reject the installation request. Add this route with the following code to index.js:

Then restart your app and visit ‘{your ngrok forwarding address}/shopify?’ to check that you get redirected to the app authorization prompt.

Once a user accepts the install request, they’ll be sent to the redirect_uri in the above code. This needs to be the redirection url you entered in the Partner Dashboard. You then want to write code so that the user will receive a permanent access token by adding the following code to index.js:

Then restart your app and go to ‘{your ngrok address}/shopify?’ in the browser. You should now see a page that says HMAC validated rather than being presented with the former prompt.

Now add some code to collect the permanent access token by replacing ‘res.status(200).send('HMAC validated')’ in your code with:

Then restart the app and visit ‘{your ngrok address}/shopify?’ again. You should now go to a page that says you have an access token.

Now you can finally make an API call to the shop Endpoint with the access token. To do that, replace ‘res.status(200).send("Got an access token, let's do something with it")’ in index.js with the following:


You can now run your app. Simply restart your app and visit the same link as above. You should now see the raw JSON response to your API call. 


Be sure to read the next Application Development article: How to Work with Service Workers In Angular 5

Original Article

Building a Shopify application with Node.js and Express