Webhooks are how most WooCommerce integrations hear about new orders. WooCommerce sends an HTTP request to your endpoint when something happens, such as an order being created or updated. They are simple to set up and easy to get subtly wrong: an endpoint that skips the signature check accepts fake orders, and one that is slow or fragile gets switched off.
This guide walks through a receiver that verifies every request, replies quickly, and processes each order exactly once. The examples use Node.js and Express, but the same steps apply in any language.
How WooCommerce sends a webhook
You create webhooks under WooCommerce → Settings → Advanced → Webhooks. Each one has a topic (for example “Order created”), a delivery URL and a secret.
Each delivery is a POST with a JSON body (the same shape as the REST API’s order object) and headers that include:
X-WC-Webhook-Topic, such asorder.createdX-WC-Webhook-Source, the store’s URLX-WC-Webhook-Signature, a base64-encoded HMAC-SHA256 of the raw body, keyed with the webhook’s secret
By default WooCommerce queues deliveries in the background with Action Scheduler, so a webhook can arrive a little after the order is placed. If deliveries keep failing, WooCommerce disables the webhook, and nothing tells your system that orders have stopped arriving.
Verify the signature on the raw body
The signature is computed over the exact bytes WooCommerce sent. If your framework parses the JSON first and you re-serialise it, the bytes change and every check fails. Keep the raw body, compute the HMAC yourself, and compare in constant time.
import crypto from "node:crypto";
import express from "express";
const app = express();
const SECRET = process.env.WC_WEBHOOK_SECRET;
// Keep the raw body: the signature is computed over the exact bytes WooCommerce sent.
app.post("/webhooks/woocommerce", express.raw({ type: "*/*", limit: "1mb" }), async (req, res) => {
const body = req.body; // a Buffer
const received = req.get("X-WC-Webhook-Signature") ?? "";
const expected = crypto.createHmac("sha256", SECRET).update(body).digest("base64");
const valid =
received.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(received), Buffer.from(expected));
if (!valid) {
// Saving a webhook in WooCommerce sends a test request ("webhook_id=...").
// Acknowledge it, but never treat an unsigned request as an order.
if (body.toString("utf8").startsWith("webhook_id=")) return res.sendStatus(200);
return res.sendStatus(401);
}
const topic = req.get("X-WC-Webhook-Topic"); // e.g. "order.created"
const order = JSON.parse(body.toString("utf8"));
// Reply fast, then do the slow work in a queue.
await queue.add({ topic, orderId: order.id, modified: order.date_modified_gmt });
res.sendStatus(200);
});
// Stand-in for your real queue (BullMQ, SQS, a database table...).
const queue = { add: async (job) => console.log("queued", job) };
app.listen(3000);
The test request when you save
When you save a webhook, WooCommerce sends a test request whose body is webhook_id= followed by the ID, not an order. The example answers it with a 200 so the webhook saves cleanly, but it never treats an unsigned request as an order.
Keep the secret out of the code
Read the secret from an environment variable or your platform’s secret store. If it leaks, change it in WooCommerce and in your environment at the same time.
Answer fast, then queue the work
Do as little as possible before replying: check the signature, record the job, return 200. Calling a POS, ERP or accounting system inside the request makes your endpoint slow, and slow or failing endpoints count as failed deliveries. Push the real work onto a queue, whether that is BullMQ, SQS or a simple database table polled by a worker.
Process each order once
You will often receive several requests about the same order: an order.created followed by order.updated events, sometimes seconds apart, as payment and stock steps complete. Make the worker idempotent. A simple key is the order ID plus its last-modified time, which changes only when the order does.
const seen = new Set(); // use a database table in production
export async function handle(job) {
const key = `${job.orderId}:${job.modified}`;
if (seen.has(key)) return; // already processed this version of the order
seen.add(key);
// Fetch the full order with your REST API keys (read access is enough).
const url = `https://yourstore.example/wp-json/wc/v3/orders/${job.orderId}`;
const auth = Buffer.from(`${process.env.WC_KEY}:${process.env.WC_SECRET}`).toString("base64");
const response = await fetch(url, { headers: { Authorization: `Basic ${auth}` } });
if (!response.ok) throw new Error(`WooCommerce returned ${response.status}`);
const order = await response.json();
await sendToPos(order); // your POS or ERP call
}
async function sendToPos(order) {
console.log("sending", order.id, order.line_items.length, "lines");
}
Fetching the full order from the REST API before acting has two benefits: you always work with the current state, and a forged payload can’t inject data, because you only trust what the store itself returns. Create REST API keys with read access only for this.
Add fields to the payload
If your integration needs data that isn’t in the standard order payload, such as a custom meta field, add it on the store side with the woocommerce_webhook_payload filter. Put it in a small plugin rather than the theme.
<?php
/**
* Plugin Name: Store webhook extras
* Description: Adds the warehouse code to order webhook payloads.
*/
add_filter( 'woocommerce_webhook_payload', function ( $payload, $resource, $resource_id, $webhook_id ) {
if ( 'order' !== $resource ) {
return $payload;
}
$order = wc_get_order( $resource_id );
if ( $order ) {
// get_meta() works with both the posts table and High-Performance Order Storage.
$payload['warehouse_code'] = $order->get_meta( '_warehouse_code' );
}
return $payload;
}, 10, 4 );
Use wc_get_order() and the order object’s methods, not direct post meta calls, so the code keeps working on stores that use High-Performance Order Storage.
Watch for silent failures
- Check the webhook’s status in WooCommerce from time to time, or alert when no order has arrived for longer than usual.
- Log every delivery your endpoint receives, with the topic and the order ID.
- Run a scheduled job that compares recent orders from the REST API against what your system has processed, and picks up anything missed.
Checklist
- The endpoint verifies
X-WC-Webhook-Signatureagainst the raw body, in constant time. - The secret lives in an environment variable, not in the code.
- The endpoint replies within a second or two and queues the work.
- The worker is idempotent and fetches the current order before acting.
- REST API keys are read-only unless the integration must write.
- Something alerts a person when deliveries stop.
When it’s worth paying for a build
If you run one or two webhooks into a no-code tool, its built-in WooCommerce trigger may be all you need. A custom receiver like this one earns its cost when orders matter enough that a silent failure would hurt, when you sync with more than one system, or when your rules don’t fit a no-code tool.
