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:

‘SHOPIFY_API_KEY="{YOUR_API_KEY}"
SHOPIFY_API_SECRET="{YOUR_API_SECRET_KEY}"’

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?shop=your-development-shop.myshopify.com’ 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?shop=your-development-shop.myshopify.com’ 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?shop=your-development-shop.myshopify.com’ 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:

Conclusion

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. 

 

Original Article

Building a Shopify application with Node.js and Express

 

Comments

Charles-Kuchlenz

Under: Creating and Configuring your app:

Next step is to create an app in the "Spotify" Partner Dashboard.