Add billing and payments to your Next.js 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 commetpnpm add @commet/node @commet/nextnpm install @commet/node @commet/nextyarn add @commet/node @commet/nextbun add @commet/node @commet/nextCOMMET_API_KEY=ck_sandbox_xxximport { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: process.env.COMMET_API_KEY!,
})customers.create is idempotent — if a customer with the same id already exists, it returns the existing record.
'use server'
import { redirect } from 'next/navigation'
import { commet } from '@/lib/commet'
export async function subscribe(customerId: string) {
await commet.customers.create({
email: 'user@example.com',
id: customerId,
})
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
const checkoutUrl = subscription.checkoutUrl
if (checkoutUrl) {
redirect(checkoutUrl)
}
}The customer is redirected to checkout to complete payment. When checkoutUrl is null no payment is needed and the subscription is already active.
export async function getSubscription(customerId: string) {
const subscription = await commet.subscriptions.getActive({ customerId })
return subscription
}export async function canUseFeature(customerId: string, feature: string) {
const access = await commet.featureAccess.get({ code: feature, customerId })
return access.allowed
}export async function trackApiCall(customerId: string) {
await commet.usage.track({
customerId,
featureCode: 'api_calls',
value: 1,
})
}Usage is aggregated and billed at end of period.
import { CustomerPortal } from '@commet/next'
export const GET = CustomerPortal({
apiKey: process.env.COMMET_API_KEY!,
getCustomerId: async (req) => {
return 'user_123'
},
})<a href="/api/commet/portal">Manage Billing</a>How is this guide?