Skip to content
Back to skills

Webhook Handling

ASecurity

Use when code receives callbacks from a payment processor, bank, PSP or any provider that reports money events. Covers signature verification on the raw body with timestamp tolerance, treating the payload as a notification and confirming state by API before acting, dedupe by event id, out-of-order delivery, store-then-acknowledge with asynchronous processing, correct status codes for retry, secret rotation, replay tooling and lag monitoring. Triggers on a new webhook endpoint, a missed or dup...

  • 7 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 11, 2026
ai-agentstypescriptrustgoshellapidatabasedocumentation

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 9 files and shows the line behind each finding

Scanned November 29, 2026

npx -y skills add felipealvarenga/fintech-skills --skill webhook-handling --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Webhook Handling?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Webhook Handling
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/felipealvarenga-webhook-handling/badge)](https://www.skillsdirectory.com/skills/felipealvarenga-webhook-handling)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: webhook-handling
description: >-
  Use when code receives callbacks from a payment processor, bank, PSP or any provider that
  reports money events. Covers signature verification on the raw body with timestamp tolerance,
  treating the payload as a notification and confirming state by API before acting, dedupe by
  event id, out-of-order delivery, store-then-acknowledge with asynchronous processing, correct
  status codes for retry, secret rotation, replay tooling and lag monitoring. Triggers on a new
  webhook endpoint, a missed or duplicated event, a provider retry storm, a status that never
  updates. Not for outbound webhooks you send to customers, though most rules transfer.
license: MIT
metadata:
  author: Felipe Alvarenga
  version: 0.1.0
---

# Webhook handling

A webhook is a hint from someone else's system, delivered at least once, in any order, by
anyone who can reach the URL. Code that treats it as a trusted, ordered, exactly-once command
will mark unpaid orders paid and pay refunds twice. Code that follows the rules below is boring,
which is the goal.

## Execution contract

The user installed this skill so these rules are enforced on every matching task:

1. **Never implement unsafe shortcuts.** When a request conflicts with a rule ("keep it simple", "no extra call", "just delete it", "disable verification", "keep floats", "we trust them", "later"), name the broken rule and why it fails in one sentence, refuse the shortcut, and implement the compliant version that still meets the user's goal. Never ship the rule-breaking shortcut.
2. **Ship the complete rule slice, wired up.** Implement every requirement of the triggered rules in this change: storage or schema, validation, error handling, user-facing alerts, status or monitoring wired into an actual entry point, handler or job (not uncalled helpers), and a test file under `tests/`. Do not stop at a minimal patch or leave required safeguards as prose TODOs.
3. **Fix workspace code on audit, incident and review prompts.** When the request describes a discrepancy, an auditor question, a bug report or asks "is this enough?" against files in the workspace, answer the question in full and implement the compliant code and tests in the same change. Stay read-only only when explicitly asked for a report only.
4. **Build directly on greenfield prompts.** If the workspace has no application files or live connections yet, write a self-contained implementation under `src/` and tests under `tests/` instead of stopping to ask for repository paths or credentials.
5. **Write tests even without a shell.** If the environment cannot run tests or databases, still write the full test file under `tests/` and say in one line that tests are unrun. Never omit tests or skip implementation because tools are unavailable.

## When to use

- A new endpoint that receives events from a processor, PSP, bank, KYC vendor or similar.
- A bug where an order stays pending, flips to paid without payment, or gets two refunds.
- A provider dashboard shows failed deliveries or retries.
- A provider rotates or leaks a webhook secret.

## Rules

1. **Verify the signature on the raw body, before parsing.** Use the provider SDK's verifier
   or a constant-time HMAC comparison over the exact bytes received. A framework that parses
   JSON before your handler breaks verification; read the raw body. Reject a timestamp outside
   the tolerance window (Stripe's default is 300 seconds) to limit replays.
2. **The payload is a notification, not the truth.** For any decision that moves money or
   changes a customer-visible status, fetch the object from the provider API by id and act on
   that state. The payload tells you something changed; the API tells you what it is now.
   The fetch runs in the worker (rule 5), so it adds nothing to the request path; "latency
   matters" is an argument for the queue, not for skipping the check. If the user still forbids
   the API call, the compliant minimum is rule 3 plus rule 4: dedupe by event id and a state
   machine that cannot regress. Never write `mark_paid(payload...)` with neither.
3. **Dedupe by event id, in the database, and by object in the state machine.** Insert the event
   id into a table with a unique index before processing; a duplicate insert means the event was
   seen: acknowledge and stop. That is not enough on its own: Stripe documents that one change can
   produce two Event objects with different ids, so the state machine (rule 4) or the refund and
   dispute records must make a second event for the same object, type and state a no-op.
4. **Expect events out of order.** `captured` may arrive before `authorized`. Design handlers
   around the object's current state (rule 2), not the sequence of events. A handler that
   receives an older state than the one already recorded does nothing.
   If the codebase has no payment state machine, write a minimal one in the same change: the
   states (`pending`, `paid`, `partially_refunded`, `refunded`, `disputed`, `failed`), the
   allowed transitions as a table, and one `transition(order, to)` function that rejects any
   move not in the table and ignores a transition to a state at or behind the current one. The
   state machine governs the order's status only: refunds and disputes are their own records,
   keyed by the provider's refund or dispute id with their amounts, so a second partial refund is
   a new refund record (the order's refunded total is the sum), not a repeated transition that the
   guard would drop, and a dispute sits beside the status, not as a step in it. Every handled
   event type maps to one transition or one record through those functions. Do not call
   `mark_refunded` or any other function that does not exist in the repository.
5. **Store, acknowledge, then process.** Persist the raw event and return 2xx within a few
   seconds. Process from a worker that reads the durable table (rows in status `received`), or
   enqueue in the same transaction as the insert. An in-memory enqueue after the commit leaves a
   crash window where the event is stored, acknowledged and never processed, and the provider's
   duplicate deliveries then stop at the dedupe. Slow handlers cause provider timeouts, which
   cause retries, which cause duplicates.
6. **Status codes are for you and for the provider's retry policy, which differs.** Return 2xx
   when the event is stored (even if you do not handle that type). Return 4xx for a bad signature
   or malformed body. Whether a 4xx stops retries depends on the provider: Stripe retries any
   non-2xx for up to three days, so a bad signature keeps failing until the provider gives up,
   which is the outcome you want. Return 5xx only when storage itself failed and a retry can help.
7. **Secrets are per endpoint and rotate without downtime.** Keep two active secrets during
   rotation; verify against both; remove the old one after the provider confirms. Secrets live
   in the secret manager, never in the repo or in logs.
8. **Replay is a feature, built with the handler.** `replay(event_id, actor, reason)` re-runs
   the stored raw event through the same worker function used on first delivery, in staging and
   in production. `actor` and `reason` are required. Each call writes a row to
   `webhook_replays(event_id, actor, reason, replayed_at, outcome)`. A replay of an event already
   `processed` is a no-op recorded as `skipped`, unless `force=True`, which also needs a reason.
   This is how you recover from a handler bug without asking the provider to resend, so it ships
   in the same change as the handler, not later.
9. **One endpoint per provider and per environment.** Test-mode and live-mode events never
   share a URL or a secret. The handler refuses an event whose mode does not match the
   environment.
10. **Monitor lag and failures, in the same change.** Record per event type: lag (processed
    timestamp minus the provider's `created` timestamp), processing failures, and unknown types.
    The minimum is a `webhook_metrics()` function or table the dashboard reads, plus the two
    alert thresholds (lag above the SLA, failure rate above a small percentage) in config.
    `log.exception` alone is not monitoring.

## How to apply

1. Read the provider's webhook documentation for: signature scheme, timestamp header,
   retry schedule, event ordering guarantees (usually none), and test tooling.
2. Implement verification as the first line of the handler, on the raw body. Add the
   timestamp check.
3. Create the `webhook_events` table: provider, event id (unique), type, received at, raw
   payload, status, processed at, error. Insert before anything else.
4. Return 2xx right after the insert. Hand the event id to a worker.
5. In the worker, fetch the object by API, compare with the recorded state, apply the change
   through the same state machine the rest of the system uses (see `idempotent-payments`), or
   through the minimal one from rule 4 when none exists.
6. Write the tests in the **Tests** section. Use the provider's CLI or fixtures to produce
   signed payloads.
7. Add `replay()` with its audit table and `webhook_metrics()` in the same change. A handler
   without them is not finished.

## When the user pushes back

| Request | Answer in one sentence | What you build |
|---|---|---|
| "We trust Stripe, mark it paid from the payload, no API call" | Rule 2: the payload is a notification, delivered at least once, by anyone with the URL | Store, 2xx, then the worker: `mark_paid` becomes `transition(order, "paid")` through the state machine of rule 4, after event-id dedupe, off the request path. The API fetch moves to the worker; if the user forbids it entirely, the state machine and dedupe still ship, with a test for a stale event |
| "Latency matters, no extra call" | Rule 5: the request path only verifies and stores | Queue the event; the API call runs in the worker at zero cost to the request |
| "Disable verification, we have an IP allowlist" | Rule 1: an allowlist authenticates a network, not a message | Fix the cause (raw body), keep the 400 on a bad signature |
| "Just add the new event types" | Rule 4: every type is a transition | The transition table and guards for each new type, with duplicate tests |
| "Replay and metrics can come later" | Rules 8 and 10 ship with the handler | `replay()` with the audit table and `webhook_metrics()` in this change |
| "I did not ask about duplicates" | Rule 3 applies to every handler that changes money state | Dedupe and the tests, every time |

## Tests the change must include

Write these tests in the same change, as a test file, even when you cannot run them (no shell,
no dependencies installed); say in one line that they are unrun. A test listed as a next step is
not written.

- Invalid signature: 400, nothing stored, nothing processed.
- Valid signature with a stale timestamp: rejected.
- Valid event: stored, 2xx, processed once.
- Same event id delivered twice: 2xx both times, processed once.
- `captured` before `authorized`: final state is captured; no error, no regression to
  authorized when the older event arrives.
- Payload says paid, API says still pending: nothing changes.
- Handler raises during processing: event marked failed and retryable; the provider got 2xx;
  the replay command processes it after the fix.
- Live-mode event on the test endpoint: refused.
- Crash between the insert's commit and the dispatch: the worker still processes the event from
  the table (or the enqueue is inside the insert's transaction).
- Two partial refunds on one charge: two refund records, the refunded total is their sum, no
  event dropped by the state guard.
- Replay of an already processed event: no-op, an audit row with outcome `skipped`, who and why.
- Replay of a failed event after the fix: processed once, audit row with outcome `processed`.
- `webhook_metrics()` reports lag and failures per type after one processed and one failed event.
- Reference vectors: `skills/webhook-handling/evals/vectors/test_webhooks.py` with
  `WEBHOOK_SOLUTION` pointing at your module (signature, tolerance, rotation, dedupe, order).
  TypeScript: `webhooks.test.ts` with `WEBHOOK_SOLUTION_TS`.

## Review checklist

- Is verification on the raw bytes and before any parsing?
- Where is the unique index on the event id, and is the insert before processing?
- Does any handler change a money state from the payload alone?
- What does the handler do when the event's state is older than the recorded state?
- Which status code is returned for each failure class?
- How does a secret rotate, and where are the two secrets read from?
- How do we replay event `evt_123` in production, and who can?

## Anti-patterns and fixes

| Anti-pattern | What goes wrong | Fix |
|---|---|---|
| Verifying after `JSON.parse` | Signature never matches, so verification gets disabled | Raw body middleware |
| Marking the order paid from the payload | Spoofed or stale payload pays the order | Fetch by API, apply by state machine |
| Processing inside the request | Timeouts, provider retries, duplicates | Store, acknowledge, process from a queue |
| Returning 500 for an unhandled event type | Any non-2xx is retried, for days | 2xx for stored events of any type |
| Secret in an environment variable committed to the repo | Leak, no rotation path | Secret manager, two active secrets |
| No way to replay | Handler bug means lost events | Replay command with audit |

## References

- Stripe webhook documentation: signature verification, timestamp tolerance, retry schedule, event ordering.
- Standard Webhooks specification for a provider-neutral signature and header scheme.

Files in this skill

  • SKILL.md12.4 KB
  • evals/evals.json5.6 KB
  • evals/files/event_store.py326 B
  • evals/files/stripe_webhook.py471 B
  • evals/files/webhook_config.py100 B
  • evals/vectors/reference.py3.9 KB
  • evals/vectors/reference.ts4.2 KB
  • evals/vectors/test_webhooks.py5.1 KB
  • evals/vectors/webhooks.test.ts5.2 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.