How to create a public Shopify app?

ByAdam

How to create a public Shopify app?

We will build a Shopify app in Ruby with the official Shopify API gem and the Sinatra framework for Ruby apps.

Before starting this tutorial, you need to ensure that you have all of the followings:

  • Ruby installed
  • RubyGems (Ruby’s package management tool)
  • Bundler (the dependency manager of Ruby)
  • Example code from GitHub repository

Now, let’s start!

Step 1: Expose the app to the Internet

The simplest way to expose your app to the Internet is using tunneling service- ngrok. It helps create a secure tunnel from the Internet to your machine. Do as follows to expose your app:

  • Download ngrok at https://ngrok.com
  • Navigate to the folder where you download ngrok in terminal
  • If you are using OSX or Linux, you will start ngrok with ./ngrok http 4567 or in case you are working on Windows, you need to start ngrok witj ngrok http 4567

Then it will build a tunnel to your local matching on default port 4567

Step 2: Configure the Shopify app

Configuring your app will help Shopify find your app. Do as follows:

  • In the sample app code directory, open rb then navigate to the beginning of the GiftBasket class
  • After starting ngrok, you will be assigned a randomly generated subdomain so now you need to change APP_URL parameter in order to match to this subdomain.
  • Create a new file and name it as .env then open it in a text editor
  • Login to the Shopify Partner Account > Apps > choose a app name from App list > Get API credentials
  • Copy the API key and API Secret key then paste them to the .env as below:
    API_KEY=YOUR_API_KEY
    API_SECRET=YOUR_SECRET_KEY
  • Save file .env and close it

Configure the app URLs:

  • Login to Shopify Partner dashboard > Apps > choose an app name > App info
  • For App URL, Insert https://#{app_url}>/giftbasket/install and for Whistlisted redirection URL, enter https://#{app_url}/giftbasket/auth

Step 3: Locally run the Shopify app

  • Open a new terminal window
  • Go to the example code directory and enter ruby 01\ Getting\ Started/app.rb

Step 4: Build the Shopify app

  • Libraries: some of useful libraries for developing the app:
  • Initialization:
  • Installation:
  • Authentication with Shopify
    Verify request:

    Get the access token:
  • Make a webhook subscription:
  • Verify the webhooks

    Inside the body of the verify_webhook helper function, the SHA256 digest is computed and compared against the digest provided by Shopify in the HTTP_X_SHOPIFY_HMAC_SHA256 header. A value of true or false is returned:

    After successfully comparing, extract the store name from the request header and find the access token necessary to enable a session with Shopify.
  • Create API calls
    • Test the property line_items of each order
    • Test the property variant_id of each line item and determines whether they include a metafield property with key ingredients or not
    • Reduce the inventory quantity for the product variants:

That’s all steps you need to do for building a Shopify app.

About the author

Adam administrator

Leave a Reply