Skip to content

Instantly share code, notes, and snippets.

@gempi
Created January 19, 2022 13:20
Show Gist options
  • Save gempi/fa9fa72b1cb09bd389e3ce3cc694afb3 to your computer and use it in GitHub Desktop.
Save gempi/fa9fa72b1cb09bd389e3ce3cc694afb3 to your computer and use it in GitHub Desktop.
import { Handler } from "@netlify/functions";
import crypto from "crypto";
// Signing key from https://{{your-account}}.myshopify.com/admin/settings/notifications
const { SHOPIFY_WEBHOOK_SECRET } = process.env;
const handler: Handler = async (event) => {
const topic = event.headers["x-shopify-topic"];
const shop = event.headers["x-shopify-shop-domain"];
const hmac = event.headers["x-shopify-hmac-sha256"];
if (!hmac || !shop || !topic) {
return {
statusCode: 400,
body: JSON.stringify({ error: "Failed" }),
};
}
const genHash = crypto
.createHmac("sha256", SHOPIFY_WEBHOOK_SECRET)
.update(event.body)
.digest("base64");
if (genHash !== hmac) {
return {
statusCode: 400,
body: JSON.stringify({ error: "Failed" }),
};
}
// Your code here e.g. Add contact to mailchimp
return {
statusCode: 200,
body: JSON.stringify({ message: "Hello World" })
}
};
export { handler };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment