by Lazy Sloth
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
SHOPIFY_WEBHOOK_SECRET = os.environ.get('SHOPIFY_WEBHOOK_SECRET')
def verify_webhook(data, hmac_header):
digest = hmac.new(SHOPIFY_WEBHOOK_SECRET.encode('utf-8'), data, digestmod=hashlib.sha256).digest()
computed_hmac = base64.b64encode(digest)
return hmac.compare_digest(computed_hmac, hmac_header.encode('utf-8'))
@app.post("/webhook")
async def receive_webhook(request: Request, x_shopify_hmac_sha256: str = Header(None)):
if not SHOPIFY_WEBHOOK_SECRET:
raise HTTPException(status_code=500, detail="Shopify webhook secret is not set.")
Shopify Order Creation Webhook Handler
Created: | Last Updated:
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.
- Log in to your Shopify admin dashboard.
- Go to Settings > Notifications > Webhooks.
- Create a new webhook for the event you want to listen to (e.g., order creation).
- Copy the shared secret provided by Shopify for the webhook.
- Go to the Environment Secrets tab within the Lazy Builder.
- 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:
- Go back to your Shopify admin dashboard where you set up the webhook.
- Enter the dedicated server link provided by Lazy as the webhook URL.
- 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
Headers:
Content-Type: application/json
X-Shopify-Hmac-SHA256: [Your HMAC SHA256 Header]
Body:
{
"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.
Technologies
![Shopify](https://cdn.iconscout.com/icon/premium/png-256-thumb/shopify-2749341-2284731.png?f=webp&w=128)