Use when implementing payment flows, subscriptions, license keys, or customer portals with Polar. MUST load before writing any checkout, monetization, or billing code using Polar platform.
npm install @polar-sh/sdk
import { Polar } from "@polar-sh/sdk";
const polar = new Polar({
accessToken: process.env.POLAR_ACCESS_TOKEN!,
server: "production", // or "sandbox"
});
| API | Purpose |
|---|---|
polar.products.* | Create/manage products and prices |
polar.checkouts.* | Create checkout sessions |
polar.subscriptions.* | Manage subscriptions |
polar.orders.* | View order history |
polar.customers.* | Customer management |
polar.licenseKeys.* | Issue and validate licenses |
| Variable | Description |
|---|---|
POLAR_ACCESS_TOKEN | Organization Access Token (OAT) |
POLAR_WEBHOOK_SECRET | Webhook signing secret |
const checkout = await polar.checkouts.create({
productId: "prod_xxx",
successUrl: "https://myapp.com/success",
});
// Redirect to checkout.url
const result = await polar.licenseKeys.validate({
key: "XXXX-XXXX-XXXX-XXXX",
organizationId: "org_xxx",
});
import { validateEvent } from "@polar-sh/sdk/webhooks";
const event = validateEvent(body, signature, process.env.POLAR_WEBHOOK_SECRET!);
// event.type: "subscription.created", "order.created", etc.