Polar

Polar.sh adapter for open-source friendly billing.

Setup

lib/billing.ts
import { createEnhancedClient } from "@fuime/billing-sdk";
import { polar } from "@fuime/billing-sdk/polar";

export const billing = createEnhancedClient({
  provider: polar({
    accessToken: process.env.POLAR_ACCESS_TOKEN!,
    webhookSecret: process.env.POLAR_WEBHOOK_SECRET,
  }),
  cache: { ttlMs: 60_000 },
  plans: {
    // Use Polar product IDs
    "your-product-id": { features: ["api", "export"] },
  },
});

Environment variables

.env.local
POLAR_ACCESS_TOKEN=polar_xxx
POLAR_WEBHOOK_SECRET=xxx

How it works

The adapter uses the Polar API to:

  1. Get entitlements - Fetches subscriptions by customer email or ID
  2. Parse webhooks - Verifies signatures and normalizes Polar events

Webhook events

Polar EventSDK Event
subscription.createdsubscription.created
subscription.updatedsubscription.updated
subscription.canceledsubscription.deleted

Webhook endpoint

app/api/webhooks/polar/route.ts
import { billing } from "@/lib/billing";

export async function POST(request: Request) {
  const body = await request.text();
  const signature = request.headers.get("x-polar-signature")!;

  try {
    const event = await billing.parseWebhook(body, signature);
    console.log(`${event.type} for user ${event.userId}`);
    return new Response("ok");
  } catch (err) {
    return new Response("Invalid signature", { status: 400 });
  }
}

Polar Dashboard setup

  1. Go to your organization settings in Polar
  2. Navigate to Webhooks
  3. Add endpoint: https://yourapp.com/api/webhooks/polar
  4. Copy the signing secret to POLAR_WEBHOOK_SECRET