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=xxxHow it works
The adapter uses the Polar API to:
- Get entitlements - Fetches subscriptions by customer email or ID
- Parse webhooks - Verifies signatures and normalizes Polar events
Webhook events
| Polar Event | SDK Event |
|---|---|
subscription.created | subscription.created |
subscription.updated | subscription.updated |
subscription.canceled | subscription.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
- Go to your organization settings in Polar
- Navigate to Webhooks
- Add endpoint:
https://yourapp.com/api/webhooks/polar - Copy the signing secret to
POLAR_WEBHOOK_SECRET