Webhooks & Events
Relevant source files
The following files were used as context for generating this wiki page:
The Webhooks and Events system in Polar is a robust infrastructure designed to notify external services and internal components about asynchronous changes in the platform's state. It supports multiple delivery formats including raw JSON, Discord, and Slack, ensuring that developers can integrate Polar events into their existing workflows, whether they are building custom applications or using communication platforms.
The system handles the lifecycle of an event from initial trigger to delivery tracking, featuring automatic retries, failure detection, and a circuit-breaker mechanism that disables failing endpoints to protect system resources.
Architecture & Data Flow
The webhook system is built around three primary entities: Webhook Endpoints, Webhook Events, and Webhook Deliveries. When an action occurs in the system (e.g., an order is paid), the WebhookService identifies all active endpoints subscribed to that event type for the relevant organization and queues them for delivery.
Event Lifecycle
The diagram shows the end-to-end flow of a webhook from the initial trigger to final delivery and failure handling.
Sources: server/polar/webhook/service.py:476-530, server/polar/webhook/service.py:274-338
Component Overview
Core Data Models
| Component | Description |
|---|---|
| WebhookEndpoint | Configuration for a destination URL, including secret, format, and subscribed event types. |
| WebhookEvent | A specific instance of an event intended for a particular endpoint. Contains the generated payload. |
| WebhookDelivery | An individual attempt to deliver a WebhookEvent. Tracks HTTP status codes and response bodies. |
Sources: server/polar/webhook/schemas.py:64-90, server/polar/webhook/schemas.py:155-184, server/polar/webhook/schemas.py:192-207
Payload Generation & Formats
The system utilizes a base payload class BaseWebhookPayload which provides standardized methods for generating output for different platforms.
def get_payload(self, format: WebhookFormat, target: User | Organization) -> str:
match format:
case WebhookFormat.raw:
return self.get_raw_payload()
case WebhookFormat.discord:
return self.get_discord_payload(target)
case WebhookFormat.slack:
return self.get_slack_payload(target)
Sources: server/polar/webhook/webhooks.py:114-122
Available formats are:
- Raw JSON: Standard Pydantic model serialization.
- Discord: Branded embeds with specific fields for products, amounts, and customers.
- Slack: Formatted block kit payloads for high readability in channels.
Sources: server/polar/webhook/webhooks.py:127-167
Service Logic
Failure Protection (Circuit Breaker)
The WebhookService monitors delivery success rates. If an endpoint reaches a consecutive failure threshold (configured via settings.WEBHOOK_FAILURE_THRESHOLD), the system automatically disables the endpoint and sends an email notification to the organization members.
Key functionality:
- Streak Detection: Counts recent failures for a specific endpoint.
- Auto-Disable: Updates
WebhookEndpoint.enabledtoFalse. - FIFO Guard: Ensures events are processed in order by checking for earlier pending events.
Sources: server/polar/webhook/service.py:284-338, server/polar/webhook/service.py:340-343
Security & Validation
Webhook URLs are strictly validated to prevent SSRF (Server-Side Request Forgery). The system blocks URLs pointing to localhost, private IP ranges, or non-global IP addresses.
def is_blocked_webhook_host(host: str) -> bool:
if host.lower() in LOCALHOST_HOSTS:
return True
clean = host.strip("[]")
try:
return not ipaddress.ip_address(clean).is_global
except ValueError:
return False
Sources: server/polar/webhook/schemas.py:18-27
API Endpoints
Public API
The public API allows users and organizations to manage their webhook infrastructure.
| Method | Path | Description |
|---|---|---|
GET | /v1/webhooks/endpoints | List all configured endpoints. |
POST | /v1/webhooks/endpoints | Create a new endpoint with a generated secret. |
PATCH | /v1/webhooks/endpoints/{id} | Update URL, events, or enabled status. |
DELETE | /v1/webhooks/endpoints/{id} | Soft-delete a webhook endpoint. |
POST | /v1/webhooks/events/{id}/redeliver | Manually trigger a redelivery of a specific event. |
Sources: server/polar/webhook/endpoints.py:27-140
Backoffice Management
Admins can monitor and manage webhooks across the entire platform through the backoffice interface. This includes viewing detailed delivery logs and toggling endpoint status when necessary.
Event Types
Polar supports a wide array of event types covering the entire monetization lifecycle:
- Checkouts:
checkout.created,checkout.updated,checkout.expired - Orders:
order.created,order.paid,order.refunded - Subscriptions:
subscription.created,subscription.active,subscription.revoked,subscription.canceled - Customers:
customer.created,customer.state_changed,customer.seat_assigned - Benefits:
benefit.created,benefit_grant.created,benefit_grant.revoked
Sources: server/polar/webhook/webhooks.py:46-81, server/polar/webhook/service.py:345-474
Summary
The Polar Webhooks & Events system provides a reliable, secure, and developer-friendly way to respond to platform activities. By combining granular event types with multi-platform support (JSON, Discord, Slack) and automated failure management, it ensures that external integrations remain synchronized and stable.