Catalogue / webhook-setup
webhook-setup
MITSignedSet up webhook receivers with signature verification, idempotent event processing, retry handling, and dead letter queues for reliable event-driven integrations. Use when the user requests webhook setup or provides relevant inputs for this workflow.--- name: webhook-setup description: Set up webhook receivers with signature verification, idempotent event processing, retry handling, and dead letter queues for reliable event-driven integrations. Use when the user requests webhook setup or provides relevant inputs for this workflow. license: MIT metadata: author: awesome-ai-agent-skills version: 1.0.0 ---
Webhook Setup
This skill enables an AI agent to build production-grade webhook receivers and configure webhook producers. The agent implements HTTP endpoints that accept event payloads, verify cryptographic signatures to authenticate senders, process events idempotently to handle retries safely, and route events by type to appropriate handlers. The result is a reliable event-driven integration that handles real-world failure modes including replay attacks, out-of-order delivery, and provider timeouts.
Workflow
- Design the webhook endpoint: Create an HTTP POST endpoint at a stable, non-guessable URL path (e.g.,
/webhooks/stripe,/webhooks/github). The endpoint must return a200 OKresponse quickly (within 5 seconds for most providers) to acknowledge receipt—long processing should be done asynchronously via a job queue. Use HTTPS exclusively; most providers reject plain HTTP endpoints.
- Implement signature verification: Every webhook provider signs payloads using HMAC-SHA256, RSA, or a similar scheme. Before processing any event, verify the signature using the provider's signing secret. Compare signatures using a constant-time comparison function to prevent timing attacks. Reject requests with missing or invalid signatures immediately with a
401 Unauthorizedresponse. Read the raw request body for verification—parsed JSON may differ from the signed bytes.
- Parse and route events by type: Parse the verified payload and extract the event type (e.g.,
payment_intent.succeeded,push). Route each event type to a dedicated handler function using a registry or switch statement. Log unrecognized event types at warning level and return200 OKto prevent the provider from retrying unhandled events indefinitely.
- Process events idempotently: Providers retry webhook delivery when they don't receive a timely
200response, which means your handler may receive the same event multiple times. Store processed event IDs in a database table and check for duplicates before processing. Use database transactions to atomically mark an event as processed and perform its side effects.
- Add async processing and dead letter queues: For events that require heavy processing (sending emails, updating multiple records), acknowledge the webhook immediately and enqueue the event for background processing. Failed events that exhaust retries should be moved to a dead letter queue (DLQ) for manual inspection. Set up monitoring and alerts on DLQ depth.
- Configure the webhook on the provider side: Register your endpoint URL with the webhook provider, select the event types you need (subscribe to the minimum set), and note the signing secret. Test the webhook using the provider's test/ping functionality. Set up monitoring for delivery failures on the provider dashboard.
Supported Technologies
- Web frameworks: Express.js, Fastify, Flask, FastAPI, Django, Rails, Spring Boot
- Queue systems: Bull/BullMQ (Redis), Celery (Python), Sidekiq (Ruby), SQS, RabbitMQ
- Providers: Stripe, GitHub, Slack, Twilio, SendGrid, Shopify, PayPal, Paddle
- Monitoring: Svix (webhook infrastructure), Hookdeck, ngrok (local development)
- Databases: PostgreSQL, MySQL, Redis (for idempotency tracking)
Usage
Provide the agent with the webhook provider (Stripe, GitHub, etc.), the events you want to handle, and your server framework. The agent will produce a complete webhook receiver with signature verification, event routing, idempotent processing, and error handling. For local development, the agent can set up ngrok or a similar tunnel for testing.
Examples
Example 1: Stripe Webhook Receiver (Express.js)
const express = require("express");
const crypto = require("crypto");
const { Queue } = require("bullmq");
const app = express();
const eventQueue = new Queue("webhook-events", { connection: { host: "localhost" } });
// CRITICAL: Use raw body for signature verification — must be before json parser
app.post("/webhooks/stripe", express.raw({ type: "application/json" }), async (req, res) => {
const signature = req.headers["stripe-signature"];
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
// Step 1: Verify signature
let event;
try {
event = verifyStripeSignature(req.body, signature, webhookSecret);
} catch (err) {
console.error(`Signature verification failed: ${err.message}`);
return res.status(401).json({ error: "Invalid signature" });
}
// Step 2: Idempotency check — skip if already processed
const alreadyProcessed = await checkIdempotency(event.id);
if (alreadyProcessed) {
console.log(`Event ${event.id} already processed, skipping`);
return res.status(200).json({ received: true, deduplicated: true });
}
// Step 3: Acknowledge immediately, process async
try {
await eventQueue.add(event.type, {
eventId: event.id,
type: event.type,
data: event.data.object,
created: event.created,
});
await markEventReceived(event.id);
res.status(200).json({ received: true });
} catch (err) {
console.error(`Failed to enqueue event ${event.id}:`, err);
res.status(500).json({ error: "Processing failed" });
}
});
// Stripe signature verification (manual implementation)
function verifyStripeSignature(payload, signatureHeader, secret) {
const elements = signatureHeader.split(",").reduce((acc, part) => {
const [key, value] = part.split("=");
acc[key.trim()] = value;
return acc;
}, {});
const timestamp = elements["t"];
const expectedSig = elements["v1"];
// Protect against replay attacks: reject events older than 5 minutes
const tolerance = 300; // 5 minutes
const currentTime = Math.floor(Date.now() / 1000);
if (currentTime - parseInt(timestamp) > tolerance) {
throw new Error("Webhook timestamp too old — possible replay attack");
}
// Compute expected signature
const signedPayload = `${timestamp}.${payload}`;
const computedSig = crypto
.createHmac("sha256", secret)
.update(signedPayload)
.digest("hex");
// Constant-time comparison to prevent timing attacks
if (!crypto.timingSafeEqual(Buffer.from(expectedSig), Buffer.from(computedSig))) {
throw new Error("Signature mismatch");
}
return JSON.parse(payload);
}
// Idempotency tracking with PostgreSQL
const { Pool } = require("pg");
const db = new Pool();
async function checkIdempotency(eventId) {
const result = await db.query(
"SELECT 1 FROM processed_webhook_events WHERE event_id = $1",
[eventId]
);
return result.rows.length > 0;
}
async function markEventReceived(eventId) {
await db.query(
"INSERT INTO processed_webhook_events (event_id, received_at) VALUES ($1, NOW()) ON CONFLICT (event_id) DO NOTHING",
[eventId]
);
}
// Event handler workers (BullMQ)
const { Worker } = require("bullmq");
const worker = new Worker("webhook-events", async (job) => {
const { eventId, type, data } = job.data;
const handlers = {
"payment_intent.succeeded": handlePaymentSucceeded,
"payment_intent.payment_failed": handlePaymentFailed,
"customer.subscription.deleted": handleSubscriptionCanceled,
"invoice.payment_failed": handleInvoicePaymentFailed,
};
const handler = handlers[type];
if (!handler) {
console.log(`Unhandled event type: ${type}`);
return;
}
await handler(data, eventId);
await db.query("UPDATE processed_webhook_events SET processed_at = NOW() WHERE event_id = $1", [eventId]);
}, { connection: { host: "localhost" } });
async function handlePaymentSucceeded(paymentIntent, eventId) {
const orderId = paymentIntent.metadata.order_id;
await db.query("UPDATE orders SET status = 'paid', payment_id = $1 WHERE id = $2", [
paymentIntent.id, orderId,
]);
console.log(`Order ${orderId} marked as paid (event: ${eventId})`);
}
async function handlePaymentFailed(paymentIntent, eventId) {
const orderId = paymentIntent.metadata.order_id;
await db.query("UPDATE orders SET status = 'payment_failed' WHERE id = $1", [orderId]);
// Trigger customer notification
console.log(`Payment failed for order ${orderId} (event: ${eventId})`);
}
async function handleSubscriptionCanceled(subscription, eventId) {
await db.query("UPDATE users SET subscription_status = 'canceled' WHERE stripe_customer_id = $1", [
subscription.customer,
]);
}
async function handleInvoicePaymentFailed(invoice, eventId) {
console.log(`Invoice ${invoice.id} payment failed, attempt ${invoice.attempt_count}`);
}
app.listen(3000, () => console.log("Webhook receiver listening on port 3000"));Example 2: GitHub Webhook for CI Triggers (Python/FastAPI)
import hashlib
import hmac
import os
import logging
from datetime import datetime, timezone
from fastapi import FastAPI, Request, HTTPException, BackgroundTasks
from pydantic import BaseModel
app = FastAPI()
logger = logging.getLogger(__name__)
GITHUB_WEBHOOK_SECRET = os.environ["GITHUB_WEBHOOK_SECRET"]
# In-memory idempotency store (use Redis or PostgreSQL in production)
processed_deliveries: set[str] = set()
def verify_github_signature(payload_body: bytes, signature_header: str | None) -> None:
"""Verify the GitHub webhook signature using HMAC-SHA256."""
if not signature_header:
raise HTTPException(status_code=401, detail="Missing X-Hub-Signature-256 header")
expected_signature = "sha256=" + hmac.new(
GITHUB_WEBHOOK_SECRET.encode(),
payload_body,
hashlib.sha256,
).hexdigest()
if not hmac.compare_digest(expected_signature, signature_header):
raise HTTPException(status_code=401, detail="Invalid webhook signature")
@app.post("/webhooks/github")
async def github_webhook(request: Request, background_tasks: BackgroundTasks):
# Step 1: Read raw body and verify signature
body = await request.body()
signature = request.headers.get("X-Hub-Signature-256")
verify_github_signature(body, signature)
# Step 2: Idempotency check using delivery ID
delivery_id = request.headers.get("X-GitHub-Delivery")
if delivery_id in processed_deliveries:
logger.info(f"Duplicate delivery {delivery_id}, skipping")
return {"status": "duplicate", "delivery_id": delivery_id}
# Step 3: Parse event and route
event_type = request.headers.get("X-GitHub-Event")
payload = await request.json()
logger.info(f"Received GitHub event: {event_type} (delivery: {delivery_id})")
# Acknowledge immediately, process in background
processed_deliveries.add(delivery_id)
background_tasks.add_task(process_github_event, event_type, payload, delivery_id)
return {"status": "accepted", "event": event_type, "delivery_id": delivery_id}
async def process_github_event(event_type: str, payload: dict, delivery_id: str):
"""Route and process GitHub webhook events."""
handlers = {
"push": handle_push,
"pull_request": handle_pull_request,
"issues": handle_issue,
"ping": handle_ping,
}
handler = handlers.get(event_type)
if not handler:
logger.warning(f"Unhandled GitHub event type: {event_type}")
return
try:
await handler(payload)
logger.info(f"Successfully processed {event_type} (delivery: {delivery_id})")
except Exception as e:
logger.error(f"Failed to process {event_type} (delivery: {delivery_id}): {e}")
# In production, move to dead letter queue for retry
raise
async def handle_push(payload: dict):
"""Trigger CI pipeline on push to main branch."""Preview shortened. The full SKILL.md is in the pack.
| File | Size | SHA-256 |
|---|---|---|
| SKILL.md | 16.9 kB | bd3dde17c92d |
| SKILVAULT_SOURCE.json | 1.1 kB | c7c1fdfc5197 |
| THIRD_PARTY_NOTICES.md | 1.1 kB | 86c3c1e924e9 |
Published verbatim from the pinned commit. Nothing is rewritten.
| Version | Pinned commit | Signed | |
|---|---|---|---|
| 0.1.0 Current | 75865a5d037a | 29 Nov 2026 |
Locking a skill to one version needs Pro.