Dev

Security & Reliability

Verify webhook signatures with HMAC-SHA256, handle retries idempotently, and build an endpoint that never loses an event.

Anyone who discovers your webhook URL can POST fake payloads to it. Signature verification is what makes the difference between "an endpoint on the internet" and a trustworthy event feed.

Verify the signature

Every delivery includes an X-CloudCart-Signature header — an HMAC-SHA256 digest of the raw request body, keyed with your webhook secret. Verify it before trusting the payload:

const crypto = require('crypto');

function verifyWebhook(rawBody, signature, secret) {
  const digest = crypto
    .createHmac('sha256', secret)
    .update(rawBody)
    .digest('hex');
  return crypto.timingSafeEqual(
    Buffer.from(signature),
    Buffer.from(digest),
  );
}

// Express: capture the RAW body — parsed JSON re-serialized differently breaks the HMAC
app.post('/webhooks/cloudcart', express.raw({ type: 'application/json' }), (req, res) => {
  if (!verifyWebhook(req.body, req.get('X-CloudCart-Signature'), process.env.CC_WEBHOOK_SECRET)) {
    return res.sendStatus(401);
  }
  res.sendStatus(200); // ack fast, process async
  processEvent(JSON.parse(req.body));
});
$rawBody = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_CLOUDCART_SIGNATURE'] ?? '';

$digest = hash_hmac('sha256', $rawBody, $_ENV['CC_WEBHOOK_SECRET']);

if (!hash_equals($digest, $signature)) {
    http_response_code(401);
    exit;
}

http_response_code(200); // ack fast
$event = json_decode($rawBody, true);
import hmac, hashlib

def verify_webhook(raw_body: bytes, signature: str, secret: str) -> bool:
    digest = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(digest, signature)

# Flask
@app.post("/webhooks/cloudcart")
def cloudcart_webhook():
    if not verify_webhook(request.get_data(), request.headers.get("X-CloudCart-Signature", ""), SECRET):
        return "", 401
    process_async(request.get_json())
    return "", 200

Three classic mistakes

  1. Verifying the parsed body. Compute the HMAC over the raw bytes — re-serialized JSON orders keys differently and the digest won't match.
  2. String comparison with ==. Use timing-safe comparison (timingSafeEqual, hash_equals, compare_digest).
  3. Processing before responding. Slow handlers hit the delivery timeout and cause pointless retries — acknowledge with 200 first, process in a queue.

Design for retries: idempotency

Failed deliveries are retried with exponential backoff (see delivery & retries). Retries mean the same event can arrive more than once — and after an outage, events may arrive out of order. Your handler must be idempotent:

  • Deduplicate on a stable key: event type + resource id + timestamp (store processed keys with a TTL).
  • Don't trust order. An order.updated may arrive before the order.created retry. Treat every payload as the latest full state, or re-fetch.
  • Re-fetch when state matters. The payload is a snapshot from send time. For critical flows, use the webhook as a signal and read fresh data via the REST or Admin API: GET /api/v2/orders/{id}.

Endpoint checklist

  • HTTPS with a valid certificate (plain HTTP and 401/403/404 responses get the webhook disabled, not retried)
  • Signature verified on the raw body, timing-safe compare
  • Responds 200 in under a couple of seconds; heavy work queued
  • Idempotent processing keyed on event + id + timestamp
  • Alerting on repeated failures — after 5 failed retries the webhook is auto-disabled and must be re-enabled in the admin
  • Secret stored in a secret manager, rotated when staff changes

Alternative: API-key header

Webhooks can also be associated with a store API key in the admin panel; deliveries then carry it as X-CloudCart-ApiKey. Comparing that header against your key is a simpler (but weaker) authenticity check — it authenticates the sender but doesn't prove the payload wasn't altered. Verify at least one of the two mechanisms; prefer the HMAC signature when available on your plan.

Where the secret lives

The webhook secret is configured per webhook in managing webhooks — via admin or API. Use a different secret per endpoint so a leak in one integration doesn't compromise the rest.

On this page