DEV Community

qing
qing

Posted on

How to Build a Shopify App with Python

How to Build a Shopify App with Python

How to Build a Shopify App with Python

You don’t need Node.js or React to extend Shopify’s ecosystem. While Shopify’s official tooling leans heavily toward JavaScript frameworks, you can build powerful, production-ready Shopify apps using Python and a lightweight web framework like Flask. Whether you’re automating inventory, customizing the storefront, or building a backend service for analytics, Python gives you a clean, readable path to integrate with Shopify’s API. Let’s get your first app running today.

Why Python for Shopify Apps?

Shopify’s documentation and sample apps often feature Node, Rails, or React, which can make Python developers feel like they’re missing out. But the reality is straightforward: Shopify’s API is HTTP-based, and any language that can make HTTP requests and handle webhooks can build a Shopify app. Python shines here with its robust libraries for authentication, JSON handling, and async operations.

The main trade-off is that you’ll need to implement OAuth and webhook handling manually, since Shopify doesn’t provide an official Python SDK for app scaffolding (unlike their Node or Ruby libraries). But with Flask and the shopify Python package, you can do this efficiently.

Step 1: Set Up Your Development Environment

Before writing code, you need the right tools:

  • Python 3.9+ installed
  • Flask as your backend framework
  • Shopify API key from the Shopify Partner Dashboard
  • ngrok (or similar) to tunnel local traffic to Shopify for testing

Install the core dependencies:

pip install flask shopifyapi requests python-dotenv
Enter fullscreen mode Exit fullscreen mode

Create a .env file to store your secrets securely:

SHOPIFY_API_KEY=your_api_key_here
SHOPIFY_SHARED_SECRET=your_secret_here
HOSTNAME_FOR_SHOPIFY=https://your-ngrok-url.ngrok.io
Enter fullscreen mode Exit fullscreen mode

💡 Tip: If you’re on ngrok’s free plan, your URL changes every time you restart. You’ll need to update your Shopify app’s URLs in the Partner Dashboard each time.

Step 2: Create Your Shopify App in the Partner Dashboard

Go to your Shopify Partner DashboardAppsCreate app.

  • Give your app a meaningful name (this becomes the slug in the Shopify App Store).
  • Set the Application URL to: https://your-ngrok-url.ngrok.io/shopify
  • Set the Redirection URL to: https://your-ngrok-url.ngrok.io/shopify/finalize
  • Enable Shopify web admin embedded settings if you plan to embed your app in the Shopify admin.

Click Create app, then save your API key and secret into your .env file.

Step 3: Build the Flask Backend with OAuth

Now, let’s write the core of your app: handling OAuth to authenticate merchants.

Create app.py:

from flask import Flask, request, redirect, url_for
import shopify
import os
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)

SHOPIFY_API_KEY = os.getenv("SHOPIFY_API_KEY")
SHOPIFY_SHARED_SECRET = os.getenv("SHOPIFY_SHARED_SECRET")
HOSTNAME = os.getenv("HOSTNAME_FOR_SHOPIFY")

shopify.ShopifyResource.set_api_key(SHOPIFY_API_KEY)
shopify.ShopifyResource.set_secret(SHOPIFY_SHARED_SECRET)

@app.route("/shopify")
def install():
    shop = request.args.get("shop")
    if not shop:
        return "Shop parameter is missing", 400
    shop_url = f"https://{shop}/admin/oauth/authorize"
    params = {
        "client_id": SHOPIFY_API_KEY,
        "redirect_uri": f"{HOSTNAME}/shopify/finalize",
        "scope": "read_products,write_products"
    }
    query = "&".join(f"{k}={v}" for k, v in params.items())
    return redirect(f"{shop_url}?{query}")

@app.route("/shopify/finalize")
def finalize():
    shop = request.args.get("shop")
    token = request.args.get("token")
    if not shop or not token:
        return "Invalid authorization", 400

    shopify.ShopifyResource.set_site(f"https://{shop}/admin")
    shopify.ShopifyResource.set_token(token)

    # Store the token securely (e.g., in a database)
    # For demo, we’ll just print it
    print(f"Access token for {shop}: {token}")

    return f"✅ App installed successfully for {shop}"

if __name__ == "__main__":
    app.run(port=5000, debug=True)
Enter fullscreen mode Exit fullscreen mode

Start ngrok to expose your local server:

ngrok http 5000
Enter fullscreen mode Exit fullscreen mode

Update your .env with the new ngrok URL, then restart your Flask app.

Step 4: Test Your App on a Development Store

You need a development store to test your app:

  1. In your Partner Dashboard → StoresAdd store → Choose Development store.
  2. Once created, go to your app in the dashboard → Test your app → Click Install.
  3. Select your dev store and grant permissions.

Navigate to:

https://your-ngrok-url.ngrok.io/shopify?shop=your-dev-store.myshopify.com

You should see the OAuth redirect and a success message.

Step 5: Make API Calls to Fetch Products

Now that your app is authenticated, let’s fetch product data:

@app.route("/products")
def get_products():
    shop = request.args.get("shop")
    token = request.args.get("token")  # Retrieve from your database

    shopify.ShopifyResource.set_site(f"https://{shop}/admin")
    shopify.ShopifyResource.set_token(token)

    products = shopify.Product.find()
    return {"products": [{"title": p.title, "id": p.id} for p in products]}
Enter fullscreen mode Exit fullscreen mode

This endpoint returns a list of product titles and IDs. You can extend it to create, update, or delete products using shopify.Product.create(), save(), etc.

Step 6: Handle Webhooks (Optional but Powerful)

Webhooks let your app react to store events (e.g., orders/create, products/update).

Add a webhook endpoint:

@app.route("/webhooks/orders/create", methods=["POST"])
def orders_created():
    import json
    data = json.loads(request.data)
    order_id = data["id"]
    print(f"New order created: {order_id}")
    return "OK", 200
Enter fullscreen mode Exit fullscreen mode

In your Partner Dashboard, register this URL under WebhooksAdd webhook → Event: orders/create.

Step 7: Deploy Your App

Once tested locally:

  • Backend: Deploy Flask to Heroku, AWS, DigitalOcean, or Render.
  • Frontend (if needed): Use Vercel or Netlify for a React frontend.
  • Update your Shopify app’s URLs in the Partner Dashboard to match your production domain.

What You Can Build Today

With this setup, you’re ready to:

  • Automate inventory sync across platforms
  • Build custom admin dashboards
  • Create analytics tools for store performance
  • Develop embedded apps that live inside Shopify Admin

Final Thoughts

Building a Shopify app with Python isn’t just possible—it’s practical, readable, and powerful. You don’t need to force yourself into Node.js if Python is your strength. By leveraging Flask, the shopify package, and ngrok for testing, you can go from zero to a working app in under an hour.

Ready to ship? Start by installing your app on a dev store, then pick one feature to build—maybe fetching products or handling an order webhook. The Shopify ecosystem is open to all languages, and Python is a fantastic choice.

🚀 Your next step: Clone the code above, replace the placeholders with your keys, and run python app.py. If you hit a snag, drop a comment below—I’ll help you troubleshoot. Let’s build something great together.


If you found this helpful, consider buying me a coffee ☕ — it keeps these articles coming!

Also check out my AI tools collection: AI 次元世界 — free AI tools for developers.


💡 Related: **Content Creator Ultimate Bundle (Save 33%)* — $29.99*


🔒 Want More?

This article covers the basics. In Content Creator Ultimate Bundle (Save 33%) ($29.99), you get:

  • Complete source code
  • Advanced techniques
  • Real-world examples
  • Step-by-step tutorials
  • Bonus templates

Get instant access →

Top comments (0)