Shopify Order Creation Webhook Handler

 import os
import hmac
import hashlib
import base64
import requests
import json
from fastapi import FastAPI, Request, HTTPException, Header, Response
from fastapi.responses import JSONResponse

app = FastAPI()

# Environment variable

def verify_webhook(data, hmac_header):
    digest ='utf-8'), data, digestmod=hashlib.sha256).digest()
    computed_hmac = base64.b64encode(digest)
    return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))"/webhook")
async def receive_webhook(request: Request, x_shopify_hmac_sha256: str = Header(None)):
        raise HTTPException(status_code=500, detail="Shopify webhook secret is not set.")

About this template

A webhook handler app for Shopify that can receive webhooks related to the creation of orders on your Shopify store.

Introduction to the Shopify Order Creation Webhook Handler Template

Welcome to the Shopify Order Creation Webhook Handler Template! This template is designed to help you seamlessly integrate Shopify webhooks into your application. With this template, you can easily set up a webhook listener that will receive notifications about new orders created in your Shopify store. This guide will walk you through the steps to get this template up and running on the Lazy platform.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button. This will initialize the template within the Lazy Builder interface, pre-populating the code for you.

Initial setup: Adding Environment Secrets

Before you can test and use the webhook handler, you need to set up an environment secret that the code will use to verify the authenticity of the webhooks sent by Shopify.

  1. Log in to your Shopify admin dashboard.
  2. Go to Settings > Notifications > Webhooks.
  3. Create a new webhook for the event you want to listen to (e.g., order creation).
  4. Copy the shared secret provided by Shopify for the webhook.
  5. Go to the Environment Secrets tab within the Lazy Builder.
  6. Add a new secret with the key SHOPIFY_WEBHOOK_SECRET and paste the shared secret you copied from Shopify as the value.

This secret will be used by the template to verify that incoming webhook requests are indeed from Shopify.

Test: Pressing the Test Button

Once you have set up the environment secret, you can test the webhook handler by clicking the "Test" button. This will deploy the app and launch the Lazy CLI. No user input is required at this stage since the template does not prompt for it.

Using the App

After pressing the "Test" button, Lazy will provide you with a dedicated server link to use the API. This link is where Shopify will send webhook notifications. Additionally, since the code uses FastAPI, you will also receive a link to the API documentation, which you can use to understand and interact with the API endpoints.

To integrate this webhook handler with Shopify:

  1. Go back to your Shopify admin dashboard where you set up the webhook.
  2. Enter the dedicated server link provided by Lazy as the webhook URL.
  3. Save the webhook.

Now, whenever an order is created in your Shopify store, Shopify will send a webhook to the URL you provided, and the webhook handler will process it and print the order details.

Integrating the App

If you want to integrate this webhook handler into another service or frontend, you will need to use the server link provided by Lazy. For example, you might want to send the order details to an email service or log them in a database. Here's a sample request you might use to interact with the webhook handler:

POST [Your Lazy Server Link]/webhook
    Content-Type: application/json
    X-Shopify-Hmac-SHA256: [Your HMAC SHA256 Header]
        "current_subtotal_price": "100.00",
        "currency": "USD",
        "customer": {
            "first_name": "John",
            "last_name": "Doe"

And a sample response from the webhook handler might look like this:

    "current_subtotal_price": "100.00",
    "currency": "USD",
    "customer": {
        "first_name": "John",
        "last_name": "Doe"

Remember to replace [Your Lazy Server Link] with the actual server link provided by Lazy and [Your HMAC SHA256 Header] with the HMAC header that Shopify sends with its webhooks.

By following these steps, you can quickly set up and integrate the Shopify Order Creation Webhook Handler with your Shopify store and any other services you use.

Last published
June 15, 2024

More templates like this

Get Orders using Shopify API

A python app for getting orders in a store using the Shopify API. The python FastAPI is used for making the API call. The app requires a SHOPIFY_ADMIN_API_TOKEN and "orders" scope permissions to authenticate requests. This app can be customized to get all orders by name, by order ID (order number), fulfilled orders only and so on.

Fast API

Update Metafields in Shopify using API

A app for managing metafields in a Shopify store. It includes endpoints to create, update, and retrieve metafields for resources in a Shopify store. The app requires a SHOPIFY_ADMIN_API_TOKEN to authenticate requests


Get Product Metafields using Shopify API

A FastAPI application that retrieves all products from a Shopify store and returns them in JSON format. Requires the SHOPIFY_ADMIN_API_TOKEN environment secret. The app includes an endpoint at "/product_metafields" where users can provide a product ID and the Shopify store URL to retrieve the product's metafields. The only environment secret required is SHOPIFY_ADMIN_API_TOKEN, which must be set for the app to authenticate with the Shopify API.

Fast API
Shopify Order Creation Webhook Handler