Examples
Installation
Install the adapter:- npm
- yarn
- pnpm
- bun
Terminal
Register the module
Add the module to yournuxt.config.ts, along with the runtime config the handlers below read from:
Checkout, CustomerPortal and Webhooks in your server/ code.
Checkout
Create a checkout route. It creates a Checkout Session from the query parameters and redirects the customer to it.server/routes/api/checkout.get.ts
successUrl and returnUrl must be absolute URLs. The handler appends checkout_id={CHECKOUT_ID} to successUrl; pass includeCheckoutId: false to turn this off.
Query parameters
The route accepts these query parameters:- products
?products=123- Separate multiple products with commas:?products=123,456 - customer_id (optional)
?products=123&customer_id=xxx - external_customer_id (optional)
?products=123&external_customer_id=xxx - customer_email (optional)
?products=123&customer_email=janedoe@gmail.com - customer_name (optional)
?products=123&customer_name=Jane - customer_billing_address (optional)
URL-Encoded JSON string - customer_tax_id (optional)
?products=123&customer_tax_id=xxx - customer_ip_address (optional)
?products=123&customer_ip_address=xxx - customer_metadata (optional)
URL-Encoded JSON string - allow_discount_codes (optional)
?products=123&allow_discount_codes=false - discount_id (optional)
?products=123&discount_id=xxx - discount_code (optional)
?products=123&discount_code=SAVE20- Applied before redirecting.discount_idtakes precedence when both are supplied. - seats (optional)
?products=123&seats=5- Number of seats for seat-based products - metadata (optional)
URL-Encoded JSON string
400 when the query params are invalid, for example when products is missing, and 500 when the checkout can’t be created.
Customer Portal
Create a route that signs the customer in to the Customer Portal, where they can view orders and manage subscriptions.server/routes/api/portal.get.ts
getCustomerId must resolve to a Polar customer ID. The handler returns 400 when it resolves to an empty value.
Webhooks
The webhook handler verifies the signature of each request with your webhook secret, then calls your handlers. Copy the secret from the endpoint in Settings → Webhooks. It acknowledges the request for you. Test it locally withpolar webhook listen. See Webhooks on your machine.
server/routes/webhook/polar.post.ts
Payload handlers
The webhook handler also takes one handler per event type. Every handler is anasync function that receives the full webhook payload ({ type, timestamp, data }). Fields use the SDK’s snake_case names, for example payload.data.customer_id.
onPayload- Called for every incoming webhook event, in addition to the matching handler belowonCheckoutCreated- Triggered when a checkout is createdonCheckoutExpired- Triggered when a checkout expiresonCheckoutUpdated- Triggered when a checkout is updatedonOrderCreated- Triggered when an order is createdonOrderUpdated- Triggered when an order is updatedonOrderPaid- Triggered when an order is paidonOrderRefunded- Triggered when an order is refundedonRefundCreated- Triggered when a refund is createdonRefundUpdated- Triggered when a refund is updatedonSubscriptionCreated- Triggered when a subscription is createdonSubscriptionUpdated- Triggered when a subscription is updatedonSubscriptionActive- Triggered when a subscription becomes activeonSubscriptionCanceled- Triggered when a subscription is canceledonSubscriptionCycled- Triggered when a subscription enters a new billing periodonSubscriptionPastDue- Triggered when a subscription payment fails and it becomes past dueonSubscriptionPaused- Triggered when a subscription is pausedonSubscriptionResumed- Triggered when a paused subscription is resumedonSubscriptionRevoked- Triggered when a subscription is revokedonSubscriptionUncanceled- Triggered when a subscription cancellation is reversedonProductCreated- Triggered when a product is createdonProductUpdated- Triggered when a product is updatedonOrganizationUpdated- Triggered when an organization is updatedonBenefitCreated- Triggered when a benefit is createdonBenefitUpdated- Triggered when a benefit is updatedonBenefitGrantCreated- Triggered when a benefit grant is createdonBenefitGrantCycled- Triggered when a benefit grant renews with its subscriptiononBenefitGrantUpdated- Triggered when a benefit grant is updatedonBenefitGrantRevoked- Triggered when a benefit grant is revokedonCustomerCreated- Triggered when a customer is createdonCustomerUpdated- Triggered when a customer is updatedonCustomerDeleted- Triggered when a customer is deletedonCustomerStateChanged- Triggered when a customer state changesonCustomerSeatAssigned- Triggered when a seat is assigned to a customeronCustomerSeatClaimed- Triggered when a customer claims an assigned seatonCustomerSeatRevoked- Triggered when a seat is revoked from a customeronDiscountCreated- Triggered when a discount is createdonDiscountUpdated- Triggered when a discount is updatedonDiscountDeleted- Triggered when a discount is deletedonMemberCreated- Triggered when a member is added to a team customeronMemberUpdated- Triggered when a member of a team customer is updatedonMemberDeleted- Triggered when a member is removed from a team customer
webhook-id, webhook-timestamp and webhook-signature headers against your webhook secret before calling any handler:
- A missing or invalid signature returns
403. - A malformed payload returns
400. - A signed event type that the installed SDK doesn’t know yet returns
200and is ignored, so new event types don’t cause retries. - If a handler throws, the error propagates and the request fails, so Polar retries the delivery.

