Skip to main content
Building a reliable webhook handler requires careful attention to response timing, idempotency, error handling, and asynchronous processing. This guide covers best practices to ensure you never miss an event and process each one exactly once.

Quick Response Pattern

The most critical rule for webhook handling: respond immediately, process asynchronously. Lettr waits up to 30 seconds for your endpoint to respond. If processing takes longer, the webhook is considered failed and will be retried.
Never perform long-running operations before responding. Database writes, external API calls, and complex processing should happen after you’ve acknowledged the webhook.

Idempotency

Webhooks may be delivered more than once due to retries, network issues, or edge cases. Your handler must be idempotent—processing the same event twice should have the same effect as processing it once.

Using Event IDs

Every webhook event has a unique id. Store processed event IDs to detect and skip duplicates:

Database-Based Idempotency

For applications without Redis, use your database:

Idempotency with Business Logic

Sometimes you need idempotency at the business level, not just event level:

Queue-Based Processing

For high-volume applications or complex processing, use a message queue to decouple webhook receipt from processing:

Error Handling

Proper error handling ensures you don’t lose events and can debug issues effectively.

Categorizing Errors

Graceful Degradation

When external dependencies fail, don’t block the entire webhook:

Handling Inbound Emails

Inbound email webhooks (relay.relay_delivery) require special consideration for attachments, threading, and routing.

Processing Attachments

Attachment URLs are temporary. Download them promptly:

Routing Inbound Emails

Route emails to different handlers based on recipient address:

Threading Conversations

Use email headers to thread conversations:

Handling Multiple Recipients

When an email is sent to multiple addresses on your domain, handle each appropriately:

Monitoring and Observability

Track webhook processing health with metrics and logging:

Framework-Specific Examples

Next.js App Router

Express with TypeScript

Authorization

Secure your endpoints with authentication

Retries

Understand retry behavior and failure handling

Event Types

Complete reference of all webhook events

Testing

Test webhooks in development and production