webhook-handlers
Webhook event handling patterns for email tracking (sent, delivered, bounced, opened, clicked). Use when implementing email event webhooks, signature verification, processing delivery events, logging email analytics, or building real-time email status tracking.
What this skill does
# Webhook Handlers Skill
Comprehensive patterns and templates for implementing secure webhook handlers with Resend, covering event types, signature verification, and event processing strategies.
## Use When
- Implementing webhook endpoints for email events (sent, delivered, bounced, opened, clicked)
- Setting up signature verification for webhook authenticity
- Building email tracking and analytics systems
- Processing bounce and complaint events for list management
- Creating real-time email status dashboards
- Logging delivery events to database
- Implementing retry logic for webhook processing
- Handling multiple webhook events in parallel
## Webhook Event Types
### Resend Webhook Events
Resend sends webhooks for the following email events:
#### 1. Email Sent
Triggered when email is accepted by Resend.
```json
{
"type": "email.sent",
"created_at": "2024-01-15T10:30:00Z",
"data": {
"email_id": "123e4567-e89b-12d3-a456-426614174000",
"from": "[email protected]",
"to": "[email protected]",
"subject": "Welcome to Example",
"created_at": "2024-01-15T10:30:00Z"
}
}
```
#### 2. Email Delivered
Triggered when email reaches recipient's mail server.
```json
{
"type": "email.delivered",
"created_at": "2024-01-15T10:35:00Z",
"data": {
"email_id": "123e4567-e89b-12d3-a456-426614174000",
"from": "[email protected]",
"to": "[email protected]",
"created_at": "2024-01-15T10:35:00Z"
}
}
```
#### 3. Email Bounced
Triggered when email cannot be delivered (hard bounce).
```json
{
"type": "email.bounced",
"created_at": "2024-01-15T10:40:00Z",
"data": {
"email_id": "123e4567-e89b-12d3-a456-426614174000",
"from": "[email protected]",
"to": "[email protected]",
"reason": "Mailbox does not exist",
"created_at": "2024-01-15T10:40:00Z"
}
}
```
#### 4. Email Opened
Triggered when recipient opens the email (requires pixel tracking).
```json
{
"type": "email.opened",
"created_at": "2024-01-15T11:00:00Z",
"data": {
"email_id": "123e4567-e89b-12d3-a456-426614174000",
"from": "[email protected]",
"to": "[email protected]",
"user_agent": "Mozilla/5.0...",
"ip_address": "192.168.1.1",
"created_at": "2024-01-15T11:00:00Z"
}
}
```
#### 5. Email Clicked
Triggered when recipient clicks a link in the email.
```json
{
"type": "email.clicked",
"created_at": "2024-01-15T11:05:00Z",
"data": {
"email_id": "123e4567-e89b-12d3-a456-426614174000",
"from": "[email protected]",
"to": "[email protected]",
"link": "https://example.com/promo",
"user_agent": "Mozilla/5.0...",
"ip_address": "192.168.1.1",
"created_at": "2024-01-15T11:05:00Z"
}
}
```
#### 6. Email Complained
Triggered when recipient marks email as spam.
```json
{
"type": "email.complained",
"created_at": "2024-01-15T11:10:00Z",
"data": {
"email_id": "123e4567-e89b-12d3-a456-426614174000",
"from": "[email protected]",
"to": "[email protected]",
"created_at": "2024-01-15T11:10:00Z"
}
}
```
## Signature Verification Patterns
### TypeScript Signature Verification
Verify webhook authenticity using HMAC-SHA256:
```typescript
import crypto from 'crypto';
interface WebhookEvent {
type: string;
created_at: string;
data: Record<string, any>;
}
function verifyWebhookSignature(
payload: string,
signature: string,
signingSecret: string
): boolean {
const expectedSignature = crypto
.createHmac('sha256', signingSecret)
.update(payload)
.digest('hex');
return crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expectedSignature)
);
}
// Usage in Express middleware
import express from 'express';
const webhookRouter = express.Router();
webhookRouter.post('/webhooks/resend', express.raw({ type: 'application/json' }), (req, res) => {
const signature = req.headers['x-resend-signature'] as string;
const payload = req.body.toString();
if (!verifyWebhookSignature(payload, signature, process.env.RESEND_WEBHOOK_SECRET!)) {
return res.status(401).json({ error: 'Invalid signature' });
}
const event: WebhookEvent = JSON.parse(payload);
handleWebhookEvent(event);
res.json({ success: true });
});
export default webhookRouter;
```
### Python Signature Verification
```python
import hmac
import hashlib
import json
from typing import Tuple
def verify_webhook_signature(
payload: str,
signature: str,
signing_secret: str
) -> bool:
"""Verify Resend webhook signature using HMAC-SHA256."""
expected_signature = hmac.new(
signing_secret.encode(),
payload.encode(),
hashlib.sha256
).hexdigest()
return hmac.compare_digest(signature, expected_signature)
def get_signature_from_headers(headers: dict) -> str:
"""Extract signature from request headers."""
return headers.get('x-resend-signature', '')
```
## Event Processing Strategies
### TypeScript Event Handler
```typescript
interface EmailEvent {
type: 'sent' | 'delivered' | 'bounced' | 'opened' | 'clicked' | 'complained';
created_at: string;
data: {
email_id: string;
from: string;
to: string;
[key: string]: any;
};
}
async function handleWebhookEvent(event: EmailEvent): Promise<void> {
try {
// Log the event
console.log(`Processing webhook: ${event.type}`, {
email_id: event.data.email_id,
timestamp: event.created_at,
});
// Route to specific handler
switch (event.type) {
case 'email.sent':
await handleEmailSent(event.data);
break;
case 'email.delivered':
await handleEmailDelivered(event.data);
break;
case 'email.bounced':
await handleEmailBounced(event.data);
break;
case 'email.opened':
await handleEmailOpened(event.data);
break;
case 'email.clicked':
await handleEmailClicked(event.data);
break;
case 'email.complained':
await handleEmailComplained(event.data);
break;
default:
console.warn(`Unknown event type: ${event.type}`);
}
} catch (error) {
console.error('Error handling webhook event:', error);
throw error;
}
}
// Individual event handlers
async function handleEmailSent(data: any): Promise<void> {
// Update email status in database
await db.emails.update(
{ id: data.email_id },
{
status: 'sent',
sent_at: new Date(data.created_at),
updated_at: new Date(),
}
);
}
async function handleEmailDelivered(data: any): Promise<void> {
await db.emails.update(
{ id: data.email_id },
{
status: 'delivered',
delivered_at: new Date(data.created_at),
updated_at: new Date(),
}
);
}
async function handleEmailBounced(data: any): Promise<void> {
// Update status and mark recipient as invalid
await db.emails.update(
{ id: data.email_id },
{
status: 'bounced',
bounce_reason: data.reason,
bounced_at: new Date(data.created_at),
updated_at: new Date(),
}
);
// Add to bounce list
await db.bounced_emails.create({
email: data.to,
reason: data.reason,
bounced_at: new Date(data.created_at),
});
}
async function handleEmailOpened(data: any): Promise<void> {
await db.email_events.create({
email_id: data.email_id,
event_type: 'opened',
user_agent: data.user_agent,
ip_address: data.ip_address,
created_at: new Date(data.created_at),
});
}
async function handleEmailClicked(data: any): Promise<void> {
await db.email_events.create({
email_id: data.email_id,
event_type: 'clicked',
link: data.link,
user_agent: data.user_agent,
ip_address: data.ip_address,
created_at: new Date(data.created_at),
});
}
async function handleEmailComplained(data: any): Promise<void> {
await db.emails.update(
{ id: data.email_id },
{
status: 'complained',
Related in Productivity
gitea-workflow
IncludedOrchestrate agile development workflows for Gitea repositories using the tea CLI. Use when working with Gitea-hosted repos and asking to 'run the workflow', 'continue working', 'what's next', 'complete the task cycle', 'start my day', 'end the sprint', 'implement the next task', or wanting guided step-by-step development assistance. Keywords: workflow, orchestrate, agile, task cycle, sprint, daily, implement, review, PR, standup, retrospective, gitea, tea.
microsoft-graph-gateway
IncludedRoute Microsoft Graph work in this workspace. Use when users want to read or write Outlook mail, calendar events, contacts, OneDrive or SharePoint files, Teams, Planner, To Do, users, groups, directory data, or arbitrary Microsoft Graph endpoints from VS Code. Prefer WorkIQ for common read scenarios. Use Microsoft Graph for write actions and gap-read scenarios that need exact Graph properties, filters, permissions, or endpoints.
copilotkit
IncludedUse when building with CopilotKit — setup, development, integrations, debugging, upgrading, or contributing. Routes to the appropriate specialized skill based on the task.
wordly-wisdom
IncludedProvides calibrated decision analysis using Charlie Munger-style multiple mental models, inversion, incentive mapping, circle-of-competence checks, misjudgment audits, second-order effects, and forecast updates. Use when the user asks for an oracle take, a hard call, a decision memo, a premortem, an outside view, a red-team, a sanity-check, what am I missing, think this through, or wants a strategy, hire, investment, plan, product, partnership, or major life choice analysed. Avoid for simple factual lookups or time-sensitive legal, medical, or market questions without fresh evidence.
swain-session
IncludedSession management and project status dashboard. Owns the full session lifecycle (start/work/close/resume), focus lane, bookmarks, worktree detection, and tab naming. Also serves as the project status dashboard — shows active epics, progress, actionable next steps, blocked items, tasks, GitHub issues, and recommendations. Worktree creation is deferred to swain-do task dispatch (SPEC-195). Triggers on: 'session', 'status', 'what's next', 'dashboard', 'overview', 'where are we', 'what should I work on', 'show me priorities', 'bookmark', 'focus on', 'session info'.
gandi
IncludedComprehensive Gandi domain registrar integration for domain and DNS management. Register and manage domains, create/update/delete DNS records (A, AAAA, CNAME, MX, TXT, SRV, and more), configure email forwarding and aliases, check SSL certificate status, create DNS snapshots for safe rollback, bulk update zone files, and monitor domain expiration. Supports multi-domain management, zone file import/export, and automated DNS backups. Includes both read-only and destructive operations with safety controls.