Docs

Polar

Polar is a developer first payment infrastructure. Out of the box it provides a lot of developer first integrations for payments, checkouts and more. This plugin helps you integrate Polar with Better Auth to make your auth + payments flow seamless.

This plugin is maintained by Polar team. For bugs, issues or feature requests, please visit the Polar GitHub repo.

Installation

Install the plugin

First, install the plugin:

npm install @polar-sh/better-auth

If you're using a separate client and server setup, make sure to install the plugin in both parts of your project.

Install the Polar SDK

Next, install the Polar SDK on your server:

npm install @polar-sh/sdk

Add the plugin to your auth config

auth.ts
import { betterAuth } from "better-auth";
import { polar } from "@polar-sh/better-auth";
import { Polar } from "@polar-sh/sdk";
 
const client = new Polar({
    accessToken: process.env.POLAR_ACCESS_TOKEN,
    // Use 'sandbox' if you're using the Polar Sandbox environment
    // Remember that access tokens, products, etc. are completely separated between environments.
    // Access tokens obtained in Production are for instance not usable in the Sandbox environment.
    server: 'production'
});
 
const auth = betterAuth({
    // ... Better Auth config
    plugins: [
        polar({
            client,
            // Enable automatic Polar Customer creation on signup
            createCustomerOnSignUp: true,
            // Enable customer portal
            enableCustomerPortal: true, // Deployed under /portal for authenticated users
            // Configure checkout
            checkout: {
                enabled: true,
                products: [
                    {
                        productId: "123-456-789", // ID of Product from Polar Dashboard
                        slug: "pro" // Custom slug for easy reference in Checkout URL, e.g. /checkout/pro
                    }
                ],
                successUrl: "/success?checkout_id={CHECKOUT_ID}"
            },
            // Incoming Webhooks handler will be installed at /polar/webhooks
            webhooks: {
                secret: process.env.POLAR_WEBHOOK_SECRET,
                onPayload: ...,
            }
        })
    ]
});

For configuration options and more information, check out Polar's Better Auth docs.

On this page