Examples
Installation
Install the required Polar packages using the following command:- npm
- yarn
- pnpm
- bun
Terminal
Checkout
Create a Checkout handler which takes care of redirections.checkout/route.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 Params
Pass query params to this route.- products
?products=123- Repeat the parameter for multiple products:?products=123&products=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 products is missing.
Customer Portal
Create a customer portal where your customer can view orders and subscriptions.portal/route.ts
getExternalCustomerId instead of getCustomerId. The handler returns 400 when the function resolves to an empty value.
Webhooks
A simple utility which verifies the signature of incoming webhook payloads with your webhook secret.api/webhook/polar/route.ts
Payload Handlers
The Webhook handler also supports granular handlers for easy integration. 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.

