Sarah's endpoint for the orders/create subscription. Trace what it does, and in what order.
import express from "express";
import { enqueue } from "./queue.js";
const app = express();
// Raw body: the bytes must stay untouched for signature checks.
app.use("/webhooks/orders-create", express.raw({ type: "application/json" }));
app.post("/webhooks/orders-create", (req, res) => {
const topic = req.headers["x-shopify-topic"]; // "orders/create"
const shop = req.headers["x-shopify-shop-domain"]; // "northwind-coffee.myshopify.com"
const webhookId = req.headers["x-shopify-webhook-id"];
// TODO(sarah): HMAC verification before we trust any of this. Next PR.
// Acknowledge first. Everything below the .end() runs after
// Shopify already has its answer.
res.status(200).end();
const order = JSON.parse(req.body.toString("utf8"));
enqueue("order-sync", {
webhookId,
shop,
orderId: order.id,
orderNumber: order.name,
lines: order.line_items.map((line) => ({
sku: line.sku,
quantity: line.quantity,
})),
});
});
app.listen(3000);