FerriteDocs

Webhooks

Receive provider events on an api route by forwarding the signature header, verifying the raw body, recording event IDs atomically, and bounding traffic in your handler.

Provider webhooks arrive on a standard api route. The gateway hands your application the request body byte-for-byte and passes only the caller headers listed in forward_headers. Those two guarantees let your handler authenticate the provider over the exact transmitted bytes before parsing or applying any event.

Declare only the required header

{
  "services": [
    { "name": "shop-requests", "primitive": "queue",
      "workload": { "read_qps": 20, "write_qps": 10, "record_size_kb": 0, "keying": "PerKey", "hot_set_gib": 0 } },
    { "name": "shop-data", "primitive": "store" }
  ],
  "applications": [{
    "name": "shop",
    "artifact_key": "apps/shop@v1",
    "artifact_sha256": "0000000000000000000000000000000000000000000000000000000000000000",
    "bindings": [
      { "handle": "requests", "kind": "queue", "service": "shop-requests" },
      { "handle": "data", "kind": "store", "service": "shop-data" }
    ],
    "routes": [{
      "prefix": "/api/webhooks/stripe",
      "binding": "requests",
      "kind": "api",
      "forward_headers": ["stripe-signature"]
    }]
  }]
}

The shop-data store holds processed event IDs alongside the route budget bucket.

Omitting forward_headers delivers an empty header list to your handler, and leaving the signature header off the list drops it at the gateway; either mistake causes every signature check to fail. Forwarded header names arrive lowercased. Treat a missing signature the same way you treat a forged one and reject the request.

Verify the signature over the raw bytes, reject timestamps outside the provider’s tolerance window, record the provider’s event ID in the same commit that applies the change, and return success for any event you have already processed.

Verify a Stripe event with the payments package

For Stripe webhooks, add the payments package (ferrite add payments) to get a verifier and event ledger ready to use. payments::webhook::verified_event checks the stripe-signature header over the raw body, rejects timestamps more than 300 seconds from the host clock, and parses the event only after both checks succeed. Because the package exposes no verifier that accepts an already-parsed payload, you cannot accidentally verify re-serialized JSON.

let secret = ctx.secret("stripe-webhook-secret")
    .ok_or("webhook_not_configured")?;
let event = crate::payments::webhook::verified_event(
    &secret, &req.headers, &req.body, ctx.now_ms(),
)?;

Store a payments::ledger::EventLog inside the same record as the domain state the event modifies. Call admit(&event.id) at the start of each attempt, and record the event ID with record_at in the same commit that applies the change. Because one commit writes both, a crash cannot leave a state change without its marker, and redelivered events are recognized immediately. EventLog::prune drops markers older than the retention window you pass; the package default, RETENTION_MS, is 30 days. An event replayed after its marker is pruned looks new to the ledger, so protect non-repeatable transitions with domain state as well, such as refusing to settle an order that is already paid.

For other providers, follow the same order with hmac_sha256 from the sha256-hmac package and compare digests with constant_time_eq, never with ==.

Process after verification

The Shop example (ferrite-demo-shop) settles orders from a signed Stripe webhook. After verifying the signature, the handler rejects any event whose order, Checkout session, or amount fails to match the stored order, then writes the event marker, updated payment state, and an email outbox entry in one guarded commit. Confirmation email sends afterward from that outbox entry. The handler never treats a browser return from Checkout as proof of payment.

Bound the route

Bound webhook traffic inside your application with the http-route-budget package (ferrite add http-route-budget). Call http_route_budget::guard immediately after decoding the request and before any handler effect, and add application counters for provider-specific quotas.

Platform enforcement of rate_limit_per_min is not active on Ferrite Cloud, so ferrite deploy and ferrite cost hold any manifest that carries the key and explain why. Keep the key if you already declared it rather than bypassing the hold through raw deploy_source; manifests without the key are not held (Rate limits).

Because http_route_budget::guard runs before signature verification and shares one bucket across every caller, a flood of unsigned requests can exhaust the budget and return 429 to genuine deliveries. Providers that retry refused deliveries (including Stripe) send the event again later, so keep signature verification intact, size the budget well above normal provider traffic, and keep handler work bounded.

Keep secrets and bodies out of logs

Store signing secrets and provider API keys through Secrets & Egress. Secrets are write-only in the control plane and delivered to your running application through ctx.secret. Because your handler holds the signing secret in memory during verification, never log, return, or store it, and keep full request bodies and signature headers out of logs. Declare the provider’s API host as an egress destination in ferrite.json before calling it.

Limits

  • An api request body is capped at 256 KiB (Limits). Larger payloads are refused before reaching your application, so verify that your provider’s largest event fits.
  • When your account balance reaches zero, your applications stop. Stopped applications and their data are kept for 30 days and then deleted without recovery. While stopped, webhook deliveries fail and providers retry on their own schedules.
  • Signature verification proves who sent an event, while your handler remains responsible for checking that the order, amount, and state transition are valid.

Next: see where this handler sits in a full checkout flow in the Shop example.

Ferrite counts page views on this site. Privacy · Terms