Skip to main content
Receive webhook events from external services and route them to dedicated agent instances. Each webhook source (repository, customer, device) can have its own agent with isolated state, persistent storage, and real-time client connections.

Quick Start

Use Cases

Webhooks combined with agents enable powerful patterns where each external entity gets its own isolated, stateful agent instance.

Developer Tools

E-commerce & Payments

Communication & Notifications

IoT & Infrastructure

SaaS Integrations

Routing Webhooks to Agents

The key pattern is extracting an entity identifier from the webhook and using getAgentByName() to route to a dedicated agent instance.

Extract Entity from Payload

Most webhooks include an identifier in the payload:

Extract Entity from URL

Alternatively, include the entity ID in the webhook URL:

Extract Entity from Headers

Some services include identifiers in headers:

Signature Verification

Always verify webhook signatures to ensure requests are authentic. Most providers use HMAC-SHA256.

HMAC-SHA256 Pattern

Provider-Specific Headers

Processing Webhooks

The onRequest Handler

Use onRequest() to handle incoming webhooks in your agent:

Storing Webhook Events

Use SQLite to persist webhook events for history and replay.

Event Table Schema

Cleanup Old Events

Prevent unbounded growth by keeping only recent events:

Query Events

Real-time Broadcasting

When a webhook arrives, update agent state to automatically broadcast to connected WebSocket clients.
On the client side:

Patterns

Event Deduplication

Prevent processing duplicate events using event IDs:

Respond Quickly, Process Asynchronously

Webhook providers expect fast responses. Use the queue for heavy processing:

Multi-Provider Routing

Handle webhooks from multiple services in one worker:

Sending Outgoing Webhooks

Agents can also send webhooks to external services:

Security Best Practices

  1. Always verify signatures - Never trust unverified webhooks
  2. Use environment secrets - Store secrets with wrangler secret put, not in code
  3. Respond quickly - Return 200/202 within seconds to avoid retries
  4. Validate payloads - Check required fields before processing
  5. Log rejections - Track invalid signatures for security monitoring
  6. Use HTTPS - Webhook URLs should always use TLS

Common Webhook Providers