Webhook icon
Schedule icon
Script icon
Process icon
If icon
Fail icon
Queries icon
Query icon
SlackIncomingWebhook icon
Pause icon
Exit icon

Deliver Signed Webhooks with a Replayable PostgreSQL Ledger

Verify inbound HMAC, track callback attempts in PostgreSQL, retry transient failures, and require approval before replaying failed webhooks.

Categories
DataInfrastructure

A webhook receiver going down should not make an event disappear or turn a retry into a second business action. This flow checks the inbound signature, stores an event by ID in PostgreSQL, signs the outbound callback, and records the result. It uses a local HTTP receiver and PostgreSQL by default.

How it works

  1. signed_event (Webhook) accepts a text/plain JSON envelope; verify_inbound checks HMAC-SHA256 over its exact bytes and validates event_id and payload before any external call.
  2. ensure_ledger (PostgreSQL Queries) creates the event and attempt tables. store_event inserts each ID once and rejects a changed payload under the same ID.
  3. claim_due_delivery (PostgreSQL Query) claims an eligible row atomically. post_to_receiver sends the JSON payload with a stable Idempotency-Key and outbound HMAC. The receiver must return HTTP 200 with {"accepted":true,"event_id":"the-same-id"}.
  4. record_outcome writes the result and attempt row together. A failed delivery stays FAILED; a duplicate inbound event does not resend it.
  5. Manual operation=REPLAY checks FAILED, posts an approval request, pauses, then rechecks FAILED before claiming. operation=SWEEP or the disabled schedule recovers PENDING and expired SENDING rows only.

Error handling

Bad signatures and malformed envelopes fail before database access. A conflicting event ID fails without replacing the stored payload. Network, 429, and 5xx errors receive bounded exponential retries; 4xx and malformed acknowledgements fail closed. The PostgreSQL claim token prevents a late result overwriting a newer claim. A crash may leave SENDING until the lease expires. The receiver must deduplicate the stable event ID because no HTTP caller can guarantee exactly-once delivery after an ambiguous timeout. errors alerts with the execution and recovery action.

What this blueprint teaches about Kestra

A keyed Webhook plus raw-body HMAC validation protects ingestion. If branches separate new events, duplicate events, and approved replay. Pause is a human gate, while a PostgreSQL row claim provides durable state across executions and a disabled Schedule offers recovery.

What you get

  • An event ledger with states PENDING, SENDING, FAILED, and DELIVERED, plus one row per delivery round in webhook_delivery_attempt.
  • Flow outputs event_id and result; failed delivery alerts name the event and replay command.

Prerequisites

PostgreSQL 13+ and an HTTP receiver reachable from the Kestra worker. The receiver verifies X-Delivery-Signature-256, deduplicates Idempotency-Key, and returns the acknowledgement above. Configure a Slack-compatible incoming webhook for alerts.

Secrets

  • WEBHOOK_LEDGER_KEY: keyed Kestra trigger URL.
  • WEBHOOK_SIGNING_SECRET: inbound HMAC secret shared with the sender.
  • DELIVERY_SIGNING_SECRET: outbound HMAC secret shared with the receiver.
  • POSTGRES_URL, POSTGRES_USERNAME, POSTGRES_PASSWORD: ledger connection.
  • SLACK_WEBHOOK_URL: approval and failure notifications.

Inputs

  • operation (SELECT, SWEEP): select manual recovery or REPLAY; the webhook sets INGEST.
  • event_id (STRING, empty): failed event to replay.
  • receiver_url (STRING, http://host.docker.internal:9996/events): internal callback endpoint in the local Docker test setup; change it for your deployment.
  • wire_attempts (INT, 3): maximum HTTP sends per round.
  • lease_seconds (INT, 120): recovery delay for an interrupted delivery.
  • approval_timeout (STRING, P1D): expiry of a replay decision.
  • kestra_url (STRING, http://localhost:8080): UI links in alerts.

Outputs

  • event_id (STRING): claimed event, or empty when nothing was sent.
  • result (STRING): DELIVERED or NO_DELIVERY on successful runs. A failed callback ends in WARNING, and flow outputs may be absent; inspect the ledger row and alert.

Quick start

  1. Store the seven secrets and point receiver_url to a receiver implementing the HMAC, idempotency and acknowledgement contract.
  2. POST a text/plain UTF-8 body such as {"event_id":"demo-1","payload":{"order":42}} to the keyed webhook URL. Sign those exact bytes with HMAC-SHA256 and send X-Webhook-Signature-256: sha256=<hex>.
  3. Inspect webhook_delivery_ledger and webhook_delivery_attempt. Repeat the same webhook to see a no-op. If the receiver fails, run with operation=REPLAY, event_id=demo-1, review, and approve the paused execution.
  4. Enable recover_pending only after the receiver is configured. It does not automatically retry FAILED deliveries.

Plugins used

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.