Skip to main content
Webhooks allow your API to notify users when specific events occur by sending HTTP requests to URLs they provide. OpenAPI 3.1.0+ includes native webhook documentation support.

Understanding Webhooks

Webhooks reverse the typical API flow: Normal API: User sends request → Your API responds Webhooks: Event occurs → Your API sends request → User’s API responds
Webhooks are event-driven notifications where your application pushes data to user-defined endpoints.

Webhooks vs Callbacks

While similar, webhooks and callbacks have key differences:
Use callbacks when notifying about a specific request. Use webhooks for general event notifications.

How Webhooks Work

The Webhook Flow

  1. User registers: User provides webhook URLs (via dashboard, API, or config)
  2. Event occurs: Something happens in your system (e.g., new subscription, payment completed)
  3. Your app sends request: Your API makes HTTP POST request to user’s URL
  4. User’s app processes: User’s endpoint receives and processes the webhook

Implementation Responsibilities

You implement:
  • Webhook registration system
  • Event triggering logic
  • HTTP request sending code
  • Retry and failure handling
You document:
  • Event types and names
  • Request payload structures
  • Expected response formats

Documenting Webhooks in FastAPI

FastAPI provides the app.webhooks attribute to document webhook events.

Basic Webhook Documentation

The webhook “path” (e.g., "new-subscription") is just an identifier/event name, not an actual URL path. Users define the actual URLs.

Key Points

  • app.webhooks is an APIRouter instance
  • Webhook names identify events, not URL paths
  • Users configure actual webhook URLs separately
  • Documentation shows payload structure, not implementation

Multiple Webhook Events

Document different events your API might trigger:

Webhook with Response Models

Document expected responses from user endpoints:
Defining response models helps users understand what your API expects in return.

Implementing Webhook Delivery

While FastAPI documents webhooks, you implement the delivery mechanism:
This example uses in-memory storage. In production, store webhook URLs in a database with proper security measures.

Advanced Webhook Patterns

Webhook with Retry Logic

Webhook Signatures for Security

Always implement webhook signatures in production to prevent unauthorized webhook injection.

Viewing Webhook Documentation

Your webhook documentation appears in the OpenAPI docs:
  1. Start your app: uvicorn main:app --reload
  2. Visit /docs or /redoc
  3. Look for the “Webhooks” section
Users can see:
  • Event names
  • Payload structures
  • Expected responses
  • Event descriptions

OpenAPI Requirements

Webhooks require OpenAPI 3.1.0+, supported in FastAPI 0.99.0+. Earlier versions don’t support webhook documentation.
Check your FastAPI version:
Upgrade if needed:

Best Practices

Design

  • Clear event names: Use descriptive names like order-created, not event1
  • Consistent payloads: Keep webhook payload structures consistent across versions
  • Include metadata: Add timestamps, event IDs, and version information

Implementation

  • Use background tasks: Never block API responses while sending webhooks
  • Implement retries: Handle transient failures with exponential backoff
  • Add timeouts: Set reasonable timeouts (5-10 seconds)
  • Sign webhooks: Use HMAC signatures for security
  • Version your webhooks: Allow users to specify webhook format versions

User Experience

  • Provide testing tools: Offer webhook testing endpoints or tools
  • Document security: Explain signature verification clearly
  • Show examples: Provide complete request/response examples
  • Log deliveries: Let users see webhook delivery history and failures

Complete Production Example