Integrate with Nuxt
Add billing and payments to your Nuxt application.
Install the Commet Skill so your coding agent can integrate the current SDK and verify its work against the live API contract.
npx skills add commet-labs/skills --skill commetInstall
pnpm add @commet/nodenpm install @commet/nodeyarn add @commet/nodebun add @commet/nodeConfigure
COMMET_API_KEY=ck_sandbox_xxxexport default defineNuxtConfig({
runtimeConfig: {
commetApiKey: process.env.COMMET_API_KEY,
},
})Nuxt auto-imports from server/utils/, so the client is available in all server routes.
import { Commet } from '@commet/node'
const config = useRuntimeConfig()
export const commet = new Commet({
apiKey: config.commetApiKey,
})Subscribe
customers.create is idempotent — if a customer with the same id already exists, it returns the existing record.
export default defineEventHandler(async (event) => {
const { customerId, email } = await readBody(event)
await commet.customers.create({ email, id: customerId })
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
return { checkoutUrl: subscription.checkoutUrl ?? null }
})Check Access
export default defineEventHandler(async (event) => {
const customerId = getRouterParam(event, 'customerId')!
const subscription = await commet.subscriptions.getActive({ customerId })
if (!subscription) {
throw createError({ statusCode: 404, statusMessage: 'No active subscription' })
}
const feature = await commet.featureAccess.get({
code: 'api_calls',
customerId,
})
return {
status: subscription.status,
allowed: feature.allowed,
}
})Track Usage
export default defineEventHandler(async (event) => {
const { customerId } = await readBody(event)
await commet.usage.track({
customerId,
featureCode: 'api_calls',
value: 1,
})
return { tracked: true }
})Usage is aggregated and billed at end of period.
Customer Portal
export default defineEventHandler(async (event) => {
const customerId = 'user_123'
const portal = await commet.portal.getUrl({ customerId })
return sendRedirect(event, portal.portalUrl)
})Related
How is this guide?