Polar Integration
Polar is a payment platform for digital products: subscriptions, one-time purchases, licenses, checkout, and webhooks. This skill provides complete integration guidance.
Integration Workflow
1. Determine the Stack
Identify the project's framework. Polar has first-class support for:
- Next.js via
@polar-sh/nextjs — see references/nextjs.md
- SvelteKit via
@polar-sh/sveltekit — see references/sveltekit.md
- Supabase via
@polar-sh/supabase — see references/supabase.md
- Any JS/TS framework via
@polar-sh/sdk — see references/typescript-sdk.md
- Python via
polar-sdk — see references/python-sdk.md
- Other framework adapters: Astro, Express, Hono, Fastify, Remix, Nuxt, Elysia, Deno, TanStack Start, Better Auth
2. Install Dependencies
npm install @polar-sh/sdk
# For Next.js projects:
npm install zod @polar-sh/nextjs
# For SvelteKit projects:
npm install zod @polar-sh/sveltekit
# For Supabase projects:
npm install zod @polar-sh/supabase
# For embedded checkout:
npm install @polar-sh/checkout
# For Python projects:
pip install polar-sdk
Note: The Polar SDK is in active development. Pin versions to avoid breaking changes:
npm install @polar-sh/sdk@^0.46 / pip install polar-sdk~=0.28
3. Configure Authentication
Set up environment variables:
POLAR_ACCESS_TOKEN=polar_at_xxx # Organization Access Token from Polar dashboard
POLAR_WEBHOOK_SECRET=xxx # Webhook signing secret
Create Organization Access Tokens from organization settings in Polar dashboard. See references/authentication.md for details.
For sandbox/testing, use server: "sandbox" in SDK config. For production, use server: "production" or omit. See references/sandbox.md for sandbox details (test cards, API URLs, limitations).
4. Set Up Checkout
Choose approach based on needs:
| Approach |
When to Use |
| Checkout Links |
No-code, shareable URLs |
| Checkout API |
Programmatic control, dynamic pricing |
| Embedded Checkout |
Inline on your site, no redirect |
- Checkout Links: references/checkout-links.md
- Checkout API: references/checkout-api.md
- Embedded Checkout: references/checkout-embedded.md
Quick start (Next.js):
// app/checkout/route.ts
import { Checkout } from "@polar-sh/nextjs";
export const GET = Checkout({
accessToken: process.env.POLAR_ACCESS_TOKEN,
successUrl: process.env.SUCCESS_URL,
server: "sandbox",
});
// Use: GET /checkout?products=PRODUCT_ID
Quick start (SDK):
import { Polar } from "@polar-sh/sdk";
const polar = new Polar({
accessToken: process.env.POLAR_ACCESS_TOKEN,
server: "sandbox",
});
const checkout = await polar.checkouts.create({
products: ["PRODUCT_ID"],
});
// Redirect user to checkout.url
5. Set Up Webhooks
Webhooks notify your app about payment events (order created, subscription canceled, etc.).
Quick start (Next.js):
// app/api/webhook/polar/route.ts
import { Webhooks } from "@polar-sh/nextjs";
export const POST = Webhooks({
webhookSecret: process.env.POLAR_WEBHOOK_SECRET!,
onPayload: async (payload) => {
// Handle any event
},
onOrderCreated: async (payload) => {
// Handle new order
},
onSubscriptionCreated: async (payload) => {
// Handle new subscription
},
});
Quick start (Express):
import express from "express";
import { validateEvent, WebhookVerificationError } from "@polar-sh/sdk/webhooks";
app.post("/webhook", express.raw({ type: "application/json" }), (req, res) => {
try {
const event = validateEvent(req.body, req.headers, process.env.POLAR_WEBHOOK_SECRET ?? "");
// Process event
res.status(202).send("");
} catch (error) {
if (error instanceof WebhookVerificationError) res.status(403).send("");
throw error;
}
});
For webhook setup in Polar dashboard: references/webhooks-setup.md
For local development with polar listen: references/webhooks-local.md
For delivery handling, retries, troubleshooting: references/webhooks-delivery.md
6. Set Up Customer Portal (Optional)
Give customers access to their orders and subscriptions.
Next.js:
// app/portal/route.ts
import { CustomerPortal } from "@polar-sh/nextjs";
export const GET = CustomerPortal({
accessToken: process.env.POLAR_ACCESS_TOKEN,
getCustomerId: async (req) => "CUSTOMER_ID", // resolve from your auth
server: "sandbox",
});
SDK:
const session = await polar.customerSessions.create({ customerId: "CUSTOMER_ID" });
// Redirect to session.customerPortalUrl
See references/customer-portal.md.
Reference Index
Load these files when you need detailed information on a specific topic:
SDK & Frameworks
- references/typescript-sdk.md — TS/JS SDK installation, quickstart, framework adapters list
- references/python-sdk.md — Python SDK installation, sync/async usage, sandbox config
- references/nextjs.md — Next.js route handlers for checkout, portal, webhooks, all webhook handler names
- references/sveltekit.md — SvelteKit integration with
@polar-sh/sveltekit, file paths, examples
- references/supabase.md — Supabase Edge Functions integration with
@polar-sh/supabase
Authentication
- references/authentication.md — OAT tokens, security, GitHub secret scanning
- references/oauth2.md — OAuth 2.0 flow, authorization code exchange, PKCE, org vs user tokens
Products & Pricing
- references/products.md — Creating products, pricing models, billing cycles, benefits, variants, archiving
- references/trials.md — Free trial periods, abuse prevention, managing trials on existing subscriptions
- references/seat-based-pricing.md — Team/B2B pricing, seat management, claim flow, API examples
- references/discounts.md — Percentage/fixed discounts, codes, restrictions, recurring options
- references/custom-fields.md — Custom checkout fields (text, number, date, checkbox, select)
Checkout
- references/checkout-links.md — No-code checkout links, query params, UTM tracking
- references/checkout-api.md — Programmatic checkout sessions, ad-hoc prices, multiple products
- references/checkout-embedded.md — Embedded checkout, JS library, React integration, events, wallet payments
Webhooks
- references/webhooks-setup.md — Creating endpoints, delivery format, secrets, event subscription
- references/webhooks-local.md — Local dev with Polar CLI (
polar listen)
- references/webhooks-delivery.md — Validation, SDK examples, IP allowlist, retry logic, troubleshooting
Benefits & License Keys
- references/benefits.md — Benefit types (Credits, License Keys, File Downloads, GitHub, Discord, Custom), grant lifecycle, webhooks
- references/license-keys.md — License key validation, activation/deactivation, limits, brandable prefixes
Events & Metering
- references/events-metering.md — Usage-based billing, event ingestion, meters, cost tracking, credits
Orders & Customers
- references/orders-subscriptions.md — Sales overview, checkout statuses, payment attempts
- references/customer-portal.md — Customer portal URL, authenticated links via API
- references/refunds.md — Refund processing, webhook events, benefit revocation
Webhook Events
- references/webhook-events.md — Complete list of all webhook events, framework handler names
Metrics & Analytics
- references/metrics.md — Revenue, MRR, churn, conversion, AOV, cost insights
Customer State & Sandbox
- references/customer-state.md — Single API call for customer's subscriptions, benefits, meters;
customer.state_changed webhook
- references/sandbox.md — Sandbox environment, test card numbers, API URLs, SDK config, limitations
API Reference
- references/api-reference.md — Complete list of all API endpoints, rate limits, pagination, scopes
AI Integration
- references/mcp.md — Polar MCP server for AI agents (Cursor, Claude, ChatGPT, etc.)
1---2name: polar-integration3description: Integrate Polar payments, subscriptions, and checkout into web projects. Use when asked to add payments via Polar, set up Polar checkout, configure Polar webhooks, create Polar products, integrate Polar SDK, set up customer portal, add subscription billing with Polar, or any task involving polar.sh payment platform. Triggers on mentions of Polar payments, Polar checkout, Polar webhooks, Polar subscriptions, @polar-sh/sdk, @polar-sh/nextjs, @polar-sh/checkout.4---56# Polar Integration78Polar is a payment platform for digital products: subscriptions, one-time purchases, licenses, checkout, and webhooks. This skill provides complete integration guidance.910## Integration Workflow1112### 1. Determine the Stack1314Identify the project's framework. Polar has first-class support for:15- **Next.js** via `@polar-sh/nextjs` — see [references/nextjs.md](references/nextjs.md)16- **SvelteKit** via `@polar-sh/sveltekit` — see [references/sveltekit.md](references/sveltekit.md)17- **Supabase** via `@polar-sh/supabase` — see [references/supabase.md](references/supabase.md)18- **Any JS/TS framework** via `@polar-sh/sdk` — see [references/typescript-sdk.md](references/typescript-sdk.md)19- **Python** via `polar-sdk` — see [references/python-sdk.md](references/python-sdk.md)20- Other framework adapters: Astro, Express, Hono, Fastify, Remix, Nuxt, Elysia, Deno, TanStack Start, Better Auth2122### 2. Install Dependencies2324```bash25npm install @polar-sh/sdk26# For Next.js projects:27npm install zod @polar-sh/nextjs28# For SvelteKit projects:29npm install zod @polar-sh/sveltekit30# For Supabase projects:31npm install zod @polar-sh/supabase32# For embedded checkout:33npm install @polar-sh/checkout34# For Python projects:35pip install polar-sdk36```3738> **Note:** The Polar SDK is in active development. Pin versions to avoid breaking changes:39> `npm install @polar-sh/sdk@^0.46` / `pip install polar-sdk~=0.28`4041### 3. Configure Authentication4243Set up environment variables:4445```bash46POLAR_ACCESS_TOKEN=polar_at_xxx # Organization Access Token from Polar dashboard47POLAR_WEBHOOK_SECRET=xxx # Webhook signing secret48```4950Create Organization Access Tokens from organization settings in Polar dashboard. See [references/authentication.md](references/authentication.md) for details.5152For sandbox/testing, use `server: "sandbox"` in SDK config. For production, use `server: "production"` or omit. See [references/sandbox.md](references/sandbox.md) for sandbox details (test cards, API URLs, limitations).5354### 4. Set Up Checkout5556Choose approach based on needs:5758| Approach | When to Use |59|----------|-------------|60| **Checkout Links** | No-code, shareable URLs |61| **Checkout API** | Programmatic control, dynamic pricing |62| **Embedded Checkout** | Inline on your site, no redirect |6364- Checkout Links: [references/checkout-links.md](references/checkout-links.md)65- Checkout API: [references/checkout-api.md](references/checkout-api.md)66- Embedded Checkout: [references/checkout-embedded.md](references/checkout-embedded.md)6768**Quick start (Next.js):**6970```typescript71// app/checkout/route.ts72import { Checkout } from "@polar-sh/nextjs";7374export const GET = Checkout({75 accessToken: process.env.POLAR_ACCESS_TOKEN,76 successUrl: process.env.SUCCESS_URL,77 server: "sandbox",78});79// Use: GET /checkout?products=PRODUCT_ID80```8182**Quick start (SDK):**8384```typescript85import { Polar } from "@polar-sh/sdk";8687const polar = new Polar({88 accessToken: process.env.POLAR_ACCESS_TOKEN,89 server: "sandbox",90});9192const checkout = await polar.checkouts.create({93 products: ["PRODUCT_ID"],94});95// Redirect user to checkout.url96```9798### 5. Set Up Webhooks99100Webhooks notify your app about payment events (order created, subscription canceled, etc.).101102**Quick start (Next.js):**103104```typescript105// app/api/webhook/polar/route.ts106import { Webhooks } from "@polar-sh/nextjs";107108export const POST = Webhooks({109 webhookSecret: process.env.POLAR_WEBHOOK_SECRET!,110 onPayload: async (payload) => {111 // Handle any event112 },113 onOrderCreated: async (payload) => {114 // Handle new order115 },116 onSubscriptionCreated: async (payload) => {117 // Handle new subscription118 },119});120```121122**Quick start (Express):**123124```typescript125import express from "express";126import { validateEvent, WebhookVerificationError } from "@polar-sh/sdk/webhooks";127128app.post("/webhook", express.raw({ type: "application/json" }), (req, res) => {129 try {130 const event = validateEvent(req.body, req.headers, process.env.POLAR_WEBHOOK_SECRET ?? "");131 // Process event132 res.status(202).send("");133 } catch (error) {134 if (error instanceof WebhookVerificationError) res.status(403).send("");135 throw error;136 }137});138```139140For webhook setup in Polar dashboard: [references/webhooks-setup.md](references/webhooks-setup.md)141For local development with `polar listen`: [references/webhooks-local.md](references/webhooks-local.md)142For delivery handling, retries, troubleshooting: [references/webhooks-delivery.md](references/webhooks-delivery.md)143144### 6. Set Up Customer Portal (Optional)145146Give customers access to their orders and subscriptions.147148**Next.js:**149150```typescript151// app/portal/route.ts152import { CustomerPortal } from "@polar-sh/nextjs";153154export const GET = CustomerPortal({155 accessToken: process.env.POLAR_ACCESS_TOKEN,156 getCustomerId: async (req) => "CUSTOMER_ID", // resolve from your auth157 server: "sandbox",158});159```160161**SDK:**162163```typescript164const session = await polar.customerSessions.create({ customerId: "CUSTOMER_ID" });165// Redirect to session.customerPortalUrl166```167168See [references/customer-portal.md](references/customer-portal.md).169170## Reference Index171172Load these files when you need detailed information on a specific topic:173174### SDK & Frameworks175- [references/typescript-sdk.md](references/typescript-sdk.md) — TS/JS SDK installation, quickstart, framework adapters list176- [references/python-sdk.md](references/python-sdk.md) — Python SDK installation, sync/async usage, sandbox config177- [references/nextjs.md](references/nextjs.md) — Next.js route handlers for checkout, portal, webhooks, all webhook handler names178- [references/sveltekit.md](references/sveltekit.md) — SvelteKit integration with `@polar-sh/sveltekit`, file paths, examples179- [references/supabase.md](references/supabase.md) — Supabase Edge Functions integration with `@polar-sh/supabase`180181### Authentication182- [references/authentication.md](references/authentication.md) — OAT tokens, security, GitHub secret scanning183- [references/oauth2.md](references/oauth2.md) — OAuth 2.0 flow, authorization code exchange, PKCE, org vs user tokens184185### Products & Pricing186- [references/products.md](references/products.md) — Creating products, pricing models, billing cycles, benefits, variants, archiving187- [references/trials.md](references/trials.md) — Free trial periods, abuse prevention, managing trials on existing subscriptions188- [references/seat-based-pricing.md](references/seat-based-pricing.md) — Team/B2B pricing, seat management, claim flow, API examples189- [references/discounts.md](references/discounts.md) — Percentage/fixed discounts, codes, restrictions, recurring options190- [references/custom-fields.md](references/custom-fields.md) — Custom checkout fields (text, number, date, checkbox, select)191192### Checkout193- [references/checkout-links.md](references/checkout-links.md) — No-code checkout links, query params, UTM tracking194- [references/checkout-api.md](references/checkout-api.md) — Programmatic checkout sessions, ad-hoc prices, multiple products195- [references/checkout-embedded.md](references/checkout-embedded.md) — Embedded checkout, JS library, React integration, events, wallet payments196197### Webhooks198- [references/webhooks-setup.md](references/webhooks-setup.md) — Creating endpoints, delivery format, secrets, event subscription199- [references/webhooks-local.md](references/webhooks-local.md) — Local dev with Polar CLI (`polar listen`)200- [references/webhooks-delivery.md](references/webhooks-delivery.md) — Validation, SDK examples, IP allowlist, retry logic, troubleshooting201202### Benefits & License Keys203- [references/benefits.md](references/benefits.md) — Benefit types (Credits, License Keys, File Downloads, GitHub, Discord, Custom), grant lifecycle, webhooks204- [references/license-keys.md](references/license-keys.md) — License key validation, activation/deactivation, limits, brandable prefixes205206### Events & Metering207- [references/events-metering.md](references/events-metering.md) — Usage-based billing, event ingestion, meters, cost tracking, credits208209### Orders & Customers210- [references/orders-subscriptions.md](references/orders-subscriptions.md) — Sales overview, checkout statuses, payment attempts211- [references/customer-portal.md](references/customer-portal.md) — Customer portal URL, authenticated links via API212- [references/refunds.md](references/refunds.md) — Refund processing, webhook events, benefit revocation213214### Webhook Events215- [references/webhook-events.md](references/webhook-events.md) — Complete list of all webhook events, framework handler names216217### Metrics & Analytics218- [references/metrics.md](references/metrics.md) — Revenue, MRR, churn, conversion, AOV, cost insights219220### Customer State & Sandbox221- [references/customer-state.md](references/customer-state.md) — Single API call for customer's subscriptions, benefits, meters; `customer.state_changed` webhook222- [references/sandbox.md](references/sandbox.md) — Sandbox environment, test card numbers, API URLs, SDK config, limitations223224### API Reference225- [references/api-reference.md](references/api-reference.md) — Complete list of all API endpoints, rate limits, pagination, scopes226227### AI Integration228- [references/mcp.md](references/mcp.md) — Polar MCP server for AI agents (Cursor, Claude, ChatGPT, etc.)