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 respondsWebhooks 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
- User registers: User provides webhook URLs (via dashboard, API, or config)
- Event occurs: Something happens in your system (e.g., new subscription, payment completed)
- Your app sends request: Your API makes HTTP POST request to user’s URL
- 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
- Event types and names
- Request payload structures
- Expected response formats
Documenting Webhooks in FastAPI
FastAPI provides theapp.webhooks attribute to document webhook events.
Basic Webhook Documentation
Key Points
app.webhooksis anAPIRouterinstance- 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:Advanced Webhook Patterns
Webhook with Retry Logic
Webhook Signatures for Security
Viewing Webhook Documentation
Your webhook documentation appears in the OpenAPI docs:- Start your app:
uvicorn main:app --reload - Visit
/docsor/redoc - Look for the “Webhooks” section
- 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.
Best Practices
Design
- Clear event names: Use descriptive names like
order-created, notevent1 - 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
Related Topics
- OpenAPI Callbacks - Document request-specific callbacks
- Background Tasks - Execute webhooks asynchronously
- Extending OpenAPI - Customize OpenAPI schema