Payments and Checkouts made dead simple with Nuxt.
Choose your preferred package manager to install the module:
pnpm add @polar-sh/nuxt zod
The module requires Nuxt 4 and Node.js 22 or later.
Add the module to your nuxt.config.ts, along with the runtime config the handlers below read from:
export default defineNuxtConfig({
modules: ['@polar-sh/nuxt'],
runtimeConfig: {
private: {
polarAccessToken: '', // NUXT_PRIVATE_POLAR_ACCESS_TOKEN
polarServer: '', // NUXT_PRIVATE_POLAR_SERVER - "sandbox" or "production"
polarCheckoutSuccessUrl: '', // NUXT_PRIVATE_POLAR_CHECKOUT_SUCCESS_URL
polarWebhookSecret: '', // NUXT_PRIVATE_POLAR_WEBHOOK_SECRET
},
},
})
The module auto-imports Checkout, CustomerPortal and Webhooks in your server/ code.
Create a Checkout handler which takes care of redirections.
// server/routes/api/checkout.get.ts
export default defineEventHandler((event) => {
const {
private: { polarAccessToken, polarCheckoutSuccessUrl, polarServer },
} = useRuntimeConfig()
const checkoutHandler = Checkout({
accessToken: polarAccessToken,
successUrl: polarCheckoutSuccessUrl,
returnUrl: 'https://myapp.com', // Optional Return URL, which renders a Back-button in the Checkout
environment: polarServer as 'sandbox' | 'production',
theme: 'dark', // Enforces the theme - System-preferred theme will be set if left omitted
})
return checkoutHandler(event)
})
Pass query params to this route.
?products=123 - Separate multiple products with commas: ?products=123,456?products=123&customer_id=xxx?products=123&external_customer_id=xxx?products=123&customer_email=janedoe@gmail.com?products=123&customer_name=Jane?products=123&discount_code=SAVE20 - Apply a discount code before redirecting. Discount codes must be enabled; discount_id takes precedence when both are supplied.?products=123&seats=5 - Number of seats for seat-based productsURL-Encoded JSON stringUnless customer_ip_address is passed, the customer's IP address is taken from the x-forwarded-for, x-real-ip or cf-connecting-ip header and sent to Polar to show local currency. Set customerIpAddress to a function (request) => ip to resolve it yourself, or to false to not send it.
Create a customer portal where your customer can view orders and subscriptions.
// server/routes/api/portal.get.ts
export default defineEventHandler((event) => {
const {
private: { polarAccessToken, polarServer },
} = useRuntimeConfig()
const customerPortalHandler = CustomerPortal({
accessToken: polarAccessToken,
environment: polarServer as 'sandbox' | 'production',
getCustomerId: (event) => {
// Use your own logic to get the Polar customer ID - from a database, session, etc.
return Promise.resolve('9d89909b-216d-475e-8005-053dba7cff07')
},
returnUrl: 'https://myapp.com', // Optional Return URL, which renders a Back-button in the Customer Portal
})
return customerPortalHandler(event)
})
A simple utility which resolves incoming webhook payloads by verifying their signature with your webhook secret.
// server/routes/webhook/polar.post.ts
export default defineEventHandler((event) => {
const {
private: { polarWebhookSecret },
} = useRuntimeConfig()
const webhooksHandler = Webhooks({
webhookSecret: polarWebhookSecret,
onPayload: async (payload) => {
// Handle the payload
// No need to return an acknowledge response
},
})
return webhooksHandler(event)
})
The Webhook handler also supports granular handlers for easy integration.
Handlers are async functions. Webhook payloads use the generated SDK's snake_case fields. Signed events unknown to the installed SDK version are acknowledged and ignored so newly introduced event types do not cause retries.