Benefits System Overview
Relevant source files
The following files were used as context for generating this wiki page:
- server/polar/models/benefit.py
- server/polar/benefit/grant/service.py
- server/polar/benefit/registry.py
- server/polar/benefit/schemas.py
- clients/apps/web/src/components/Products/Benefits/Benefits.tsx
- clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/new/ai/chat/route.ts/dashboard/[organization]/(header)/products/new/ai/chat/route.ts)
- server/emails/src/components/Benefits.tsx
The Polar Benefits System is a core architectural component designed to manage and deliver value to customers upon the purchase of products. It acts as an orchestration layer that translates a successful transaction into tangible "benefits," such as software license keys, GitHub repository access, or Discord server roles. The system is modular, allowing for various benefit types to be defined with specific fulfillment logic while maintaining a unified interface for grant management and user interaction.
Architecture and Core Components
The system is built on a modular strategy pattern where each benefit type (e.g., discord, license_keys) has its own schema, properties, and fulfillment logic. This is managed via a central registry and a specialized grant service that handles the lifecycle of a benefit being assigned to a customer.
Benefit Types and Fulfillment
The system supports multiple automated fulfillment methods. Each type is defined in the BenefitType enumeration and mapped to specific Pydantic schemas for API requests and responses.
| Benefit Type | Description | fulfillment Method |
|---|---|---|
custom | A catch-all type allowing for Markdown notes to be shared with customers. | Text/Manual |
discord | Automatically assigns roles and provides server invites. | Integration-based |
github_repository | Invites subscribers to private GitHub repositories. | Integration-based |
license_keys | Generates and manages customizable software license keys. | Automated Generation |
downloadables | Provides access to downloadable files (up to 10GB). | File Hosting |
feature_flag | Grants access that can be queried via API to verify entitlements. | API/Entitlement |
meter_credit | Credits a customer's usage meter balance for usage-based billing. | Internal Ledger |
slack_shared_channel | Manages access to shared Slack channels. | Integration-based |
Sources: server/polar/benefit/schemas.py:108-117, clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/new/ai/chat/route.ts:85-98
System Workflow
The workflow generally follows a sequence from product purchase to benefit grant. When a product is purchased, the benefits linked to that product are identified, and the BenefitGrantService handles the creation or update of the grant record.
This diagram shows the flow from frontend benefit selection to backend fulfillment strategies.
Sources: server/polar/models/benefit.py, server/polar/benefit/grant/service.py, clients/apps/web/src/components/Products/Benefits/Benefits.tsx
Data Models and Schemas
Benefit Model
The Benefit model (defined in server/polar/models/benefit.py) stores the configuration of a specific offering. It includes details such as the organization ID, the benefit type, and a JSON block for type-specific properties.
Benefit Grant
A BenefitGrant represents the specific instance of a benefit assigned to a customer. It tracks the status of the fulfillment and links the customer to the specific benefit configuration.
The entity relationship between Benefits, Organizations, Customers, and Grants.
Sources: server/polar/models/benefit.py, server/polar/benefit/schemas.py:126-131
Implementation Details
Benefit Fulfillment Strategy
The system uses a strategy pattern to handle different benefit types. The benefit_schema_map links BenefitType enums to their corresponding Pydantic classes, ensuring type-safe validation for each unique fulfillment logic (e.g., validating GitHub repository names vs. Discord role IDs).
benefit_schema_map: dict[BenefitType, type[Benefit]] = {
BenefitType.discord: BenefitDiscord,
BenefitType.custom: BenefitCustom,
BenefitType.github_repository: BenefitGitHubRepository,
BenefitType.downloadables: BenefitDownloadables,
BenefitType.license_keys: BenefitLicenseKeys,
BenefitType.meter_credit: BenefitMeterCredit,
BenefitType.feature_flag: BenefitFeatureFlag,
BenefitType.slack_shared_channel: BenefitSlackSharedChannel,
}
Sources: server/polar/benefit/schemas.py:118-127
AI-Assisted Configuration
Polar includes an AI-onboarding assistant that helps users configure products and benefits. The assistant is instructed to translate user requirements into specific benefit types. For example, software subscriptions are mapped to feature_flag benefits by default to allow for API-based entitlement verification.
User Interface and Email Rendering
The frontend allows organizations to manage benefits through a "Master-Detail" index or a simplified search view if the benefit count is low (threshold of 20 benefits). Benefits are also rendered in transactional emails using specific icons for each type (GitHub, Discord, License Key, etc.).
Sources: clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/benefits/page.tsx, clients/apps/web/src/components/Products/Benefits/Benefits.tsx:10-25, server/emails/src/components/Benefits.tsx:5-25
Summary
The Polar Benefits System provides a flexible, scalable infrastructure for digital product fulfillment. By decoupling benefit definitions from products and utilizing a strategy-based grant service, Polar allows developers to automate complex delivery workflows (like access control and license management) while maintaining a consistent experience for both the merchant and the end customer.