07 February 2019
How to Build a Simple Shopify App
What is Shopify?
Shopify is a platform which helps to create a simple online store very quickly and safely.
If you want to build custom functionality for your shop, Shopify provides you with robust functionality to solve any of your issues in this regard. The platform has the App Store, where you can install tons of apps and plugins of your choice, and a powerful API for developers, which helps to create any kind of app.
In this article, I’ll try to explain the difference between private and public apps and show how to make a Shopify app using Ruby on Rails.
The difference between private and public apps in Shopify
First, you need to decide on the type of application to use. Shopify allows both private and public apps.
Public apps are well-known among Shopify merchants as they can be installed from Shopify’s App Store, either for free or paid by subscription. This kind of app can work with Shopify API on behalf of multiple stores. Public apps are made by third-party developers, not by Shopify.
Another type of apps is a private app. These are custom apps built specifically for one store which means that nobody has access to it except the merchant and the developer who made it. The private app uses the same API as the public one but has a different authentication method.
This guide will focus on public Shopify app development.
How to build a Shopify app using Ruby on Rails
Before you start building your first Shopify app, you need some preparation steps:
You need to have a partner account in Shopify. If you don’t have that type of account, you can register for one at the following link.
Then, you need to create an app in your Shopify account in order to get the app and the API keys. Go to the “Apps” tab and click “Create App” button.
Finally, create a store for app testing. Go to the “Development Stores” tab and create a store there.
Now you can start building your first Shopify app. As I have previously mentioned, we’ll use Ruby on Rails for development.
Basic environment configuration
1. Let’s create a RoR app with a database.
$ rails new simple-shopify-app --database=postgresql
$ rails db:setup
2. Check that the application starts up and everything works.
$ rails s
Configuration and installing shopify_app gem
1. Add the gem ‘shopify_app’ into Gemfile and run bundle installs.
2. After that, start the generator and add the ShopifyApp Engine. The generator will add shopify_app.rb to config/initializers.
$ rails generate shopify_app
The config file looks like this:
ShopifyApp.configure do |config|
config.application_name = "Shopify App"
config.api_key = Rails.application.secrets.shopify[‘api_key’]
config.secret = Rails.application.secrets.shopify[‘secret’]
config.scope = "read_products"
config.embedded_app = true
config.after_authenticate_job = false
config.session_repository = Shop
The attributes config.api_key and config.secrets need to be replaced with the keys that you received at the time of creating the application in the partner’s dashboard. In this case, we have written these keys in config/secrets.yml and it looks like this:
api_key: 2415d202f987db74f4fb583788bccd997de18274 #example
secret: 157068ba5602934734cadb584c80fa #example
After you have configured the environment and gem, start creating the controller and models for the Shopify App. By running the next generator, get shop_model and migration.
$ rails generate shopify_app:shop_model
$ rails db:migrate
Next, create an example controller that builds and displays products using Shopify API.
$ rails generate shopify_app:home_controller
Deployment and Testing
Testing Shopify applications is not a difficult task; it can be effected using such tools as localtunnel or ngrok.
After creating the URL, you need to update the “Whitelisted redirection URL(s)” in the settings of the Shopify app and add the URL (example: https://shopifyapp.localtunnel.me/auth/shopify/callback), which you have implemented using localtunnel / ngrok.
As for deployment, it is the same as the usual Ruby on Rails application development. You can either use the Heroku provider or read our article on deploying a Ruby on Rails application using ElasticBeanstalk.
It would be difficult to develop an online store using Shopify without any third-party applications. Though many ready-made applications can be found in the Shopify App Store, you’ll probably need some custom plugins made specifically for your store.
I hope my small Ruby on Rails Shopify tutorial was useful for you. If you have any questions on Shopify app development pricing, or you want our team to develop a Shopify app for you (either public or private), drop me a message in our live chat.
Is there a challenge your organization or company needs help solving? We’d love to discuss it.