A demo app created using Django and ShopifyAPI v8.4+.
To run this app locally, you can clone the repository and do the following.
- Create a
.envfile to specify this app'sAPI keyandAPI secret keyapp credentials that can be found in the Shopify Partners dashboard.
SHOPIFY_API_KEY=<The API key app credential specified in the Shopify Partners dashboard>
SHOPIFY_API_SECRET=<The API secret key app credential specified in the Shopify Partners dashboard>
APP_URL=<The public app URL specified in the Shopify Partners dashboard>
SCOPES=<Scopes needed for the app>Note: It's recommended to follow along the tutorial Build a Shopify App with Node and React to understand how to retrieve the
API keyandAPI secret keyapp credentials.
- Run the following to install the required dependencies:
$ pip install -r requirements.txt- Change directories to the main
sample_django_appapp and run all pending migrations:
$ cd sample_django_app
$ python manage.py migrate- Ensure ngrok is running on port
8000:
$ ngrok http 8000- In a new terminal, run the server:
$ python manage.py runserver- Create an
APP_URLenvironment variable based on the URL ngrok gives you. This is used in theCSRF_TRUSTED_ORIGINSandALLOWED_HOSTSsection ofsettings.py. Do not include a schema (http:// or https://) in this variable.
export APP_URL=<ngrok-url.ngrok.io>- From the Partner dashboard, update the "App URL" and "Allowed redirection URL(s)" to include the callback URL:
<https://ngrok-url.ngrok.io>/auth/shopify/callback
- In your browser, open the
httpsngrok url to install and open this app on a shop. Requests to authenticated resources like theproductsview in theapiapp should now be secured with anAuthorization: Bearer <session token>header.
-
After following all the steps mentioned above, open ngrok base url then you will be redirected to
loginurl. -
At login url, enter shopify store domain ex:
keshav-demo-store.myshopify.com -
If everything works well, then you will be redirected to your store .
-
Mainly there are 4 folders
- api (django app)
- home (django app)
- sample_django_app (django project, contains settings.py)
- shopify_app (django app)
1. The
apiapp containsurls.pyandviews.pyit is used to serve api requests that are made through our app that is embedded (yes we have embedded django template file in shopify store 😊).It uses a
@session_token_requireddecorator for getting the context of session and perform query on shopify. This will only work when we make api requests usingshopifyAuthenticatedFetch()from our app in shopify dashboard.2. The
homeapp is the app which we can treat it as normal django app. However I have made few changes in the decorators@known_shop_requiredso that it can work perfectly and also added few template files (.html) for embedding our app in admin dashboard, discussed about this in later in this section.We have to wrap our function based views in
@xframe_options_exempt @known_shop_required @latest_access_scopes_requiredso that it can get executed for our embedded apps.-
@xframe_options_exemptis for allowing our template to get embedded thus exempting xframe -
@known_shop_requiredis for getting required session based on shopify store. we have made certain changes in it so that it can work perfectly. You don't have to bother about changes, just use it. -
latest_access_scopes_requiredis used in conditional rendering of our template so that we can take actions if some scope changes in our partner app.
3. The
sample_django_appis the project folder which containssettings.pyand most import our root route file i.e.urls.py. You can add more url route paths in this file as per your requirements.settings.pyfile will work out of the box however if you want to alter it feel free to do so.4. The
shopify_appapp is the app where most of the magic happens. It has definition for all the decorators we use and all the different function necessary for authentication and session creation and management. It also contains a model for storing shop details (name, access_token etc.)I have also altered few decorators and functions to enhance its usage. You may find comments ending with "- KJ" wherever I have made changes.
NOTE: I have added some more webhooks and also added a function to get all the webhooks and delete all the registered hooks and register it again every time our apps get authenticated.
-
Templates
There are mainly 2 templates directory:
- ( shopify_app/templates/shopify_app )[default]
- ( home/templates/home )[custom made]
a. Default template is just the template that you have seen when you tried authenticating your app with the store (box with a field to enter store name).
b. Templates inside home app are the templates which gets embedded in the shopify admin dashboard. Lets explore about these tamplates and how it gets rendered in shopify dashboard.
Inside home directory of the home/templates/ there is a folder named utils which contains a
base.html. This is the base html file for all other templates that are embedded into admin dashboard.base.htmlcontains several cdns for app-bridge and polaris. Which provides base for nav-link and shopifyAuthenticatedFetch.I have passed api_key and shop_origin in each template for creating app instance in app-bridge.
There are multiple templates, each perform its own intended function, some using data that is passed through home/views/function context and some using data that is fetched from api/views response.
