This guide explains how to configure and use the billing system using the @polar-sh/better-auth plugin.
-
Environment Variables: Copy
.env.exampleto.envand set the following:POLAR_ACCESS_TOKEN="your_polar_token" POLAR_WEBHOOK_SECRET="your_webhook_secret" POLAR_SERVER="sandbox" # or "production"
-
Plan Configuration: Update
config/plans.tswith your Polar Product IDs.export const PLANS = [ { id: "pro-monthly", // ... polarProductId: "polar_prod_...", }, ];
-
Webhooks:
- Polar: Point your Polar webhook to
https://your-app.com/api/auth/webhook/polar. - Local Dev: Use a tunnel (e.g., ngrok) to expose
http://localhost:3000/api/auth/webhook/polar. - Note: The Better Auth plugin handles the webhook route automatically.
- Polar: Point your Polar webhook to
- Plugin: We use
@polar-sh/better-authwhich integrates Polar directly into Better Auth. - Client:
authClientexposes methods for checkout, portal, and subscription management. - Config:
lib/auth.tsconfigures the server-side plugin,lib/auth-client.tsconfigures the client-side plugin.
- Use
POLAR_SERVER="sandbox"in.env. - Use Polar Sandbox dashboard to create products and view test data.