Overview
Featul integrates with Polar for subscription billing and payment processing. Polar provides:- Subscription management
- Checkout flows
- Customer portal
- Usage-based billing
- Webhook handling
@polar-sh/better-auth plugin for seamless authentication integration.
Polar Setup
1. Create Polar Account
- Sign up at Polar
- Create an organization
- Choose between sandbox (testing) or production mode
2. Create Products
Featul supports multiple subscription tiers:- Go to Polar Dashboard > Products
-
Create subscription products:
- Starter Monthly
- Starter Yearly
- Professional Monthly
- Professional Yearly
- Note the Product IDs for each subscription
You can create custom pricing tiers based on your needs. The product names are flexible.
3. Generate Access Token
- Go to Polar Dashboard > Settings > API
- Create a new API token with permissions:
- Read customers
- Write customers
- Read subscriptions
- Write subscriptions
- Copy the access token
4. Configure Webhook
- Go to Polar Dashboard > Settings > Webhooks
- Add a new webhook endpoint:
- URL:
https://yourdomain.com/api/auth/polar/webhook - Events: Select all subscription events
- URL:
- Copy the webhook secret
Environment Variables
Add these to your.env file:
.env
Variable Details
POLAR_ACCESS_TOKEN: Your Polar API access tokenPOLAR_WEBHOOK_SECRET: Webhook signing secretPOLAR_SERVER: EithersandboxorproductionPOLAR_PRODUCT_ID_*: Product IDs for each subscription tier
packages/auth/src/auth.ts:64-90
For backward compatibility, you can use legacy environment variables
POLAR_PRODUCT_ID_MONTHLY and POLAR_PRODUCT_ID_YEARLY which will be treated as Starter plan products.Polar Plugin Configuration
The Polar plugin is configured with:packages/auth/src/auth.ts:136-150
Checkout Flow
The checkout plugin configuration:- Maps product IDs to slugs (
starter-monthly,starter-yearly, etc.) - Redirects to
/start?checkout_id={CHECKOUT_ID}after successful checkout - Requires authentication before checkout
packages/auth/src/auth.ts:77-105
To create a checkout session in your app:
Customer Portal
The portal plugin provides a customer self-service portal where users can:- View subscription details
- Update payment methods
- Cancel or modify subscriptions
- View invoices
/api/auth/polar/portal
Code reference: packages/auth/src/auth.ts:106
Webhook Events
Featul handles these Polar webhook events:subscription.created: New subscription createdsubscription.updated: Subscription details changedsubscription.active: Subscription became activesubscription.canceled: Subscription was canceledsubscription.uncanceled: Canceled subscription was reactivatedsubscription.revoked: Subscription was revoked
syncPolarSubscription function to update the local database.
Code reference: packages/auth/src/auth.ts:110-134
Webhook handling requires both
POLAR_ACCESS_TOKEN and POLAR_WEBHOOK_SECRET to be configured.Subscription Sync
ThesyncPolarSubscription function:
- Receives webhook payload from Polar
- Extracts subscription and customer data
- Updates user subscription status in the database
- Manages subscription lifecycle
packages/auth/src/polar.ts
Usage-Based Billing
The usage plugin enables tracking feature usage for billing:packages/auth/src/auth.ts:107
This allows you to:
- Report usage metrics to Polar
- Implement metered billing
- Track feature limits per plan
Customer Creation
When enabled, the Polar plugin automatically creates a Polar customer record when users sign up:packages/auth/src/auth.ts:138
This links authentication with billing from the start.
Sandbox vs Production
Use sandbox mode for testing:.env
.env
packages/auth/src/auth.ts:66-67
Dependencies
Polar integration requires these packages:@featul/auth package.
Disabling Billing
If you don’t need billing functionality:- Remove or leave empty the Polar environment variables
- The Polar plugin will be automatically disabled
packages/auth/src/auth.ts:136-150,248
Without Polar configuration, billing features will be disabled but the rest of the application will function normally.
Testing Webhooks Locally
To test webhooks during development:-
Use a tool like ngrok to expose your local server:
-
Update your Polar webhook URL to the ngrok URL:
- Test subscription flows and watch webhook events in real-time
Alternative Payment Providers
To use a different payment provider (Stripe, Paddle, etc.):- Remove the Polar plugin from
packages/auth/src/auth.ts - Implement your own billing integration
- Create webhook handlers for subscription events
- Update the database schema if needed to store subscription data