Installation
Next.js Setup
Use Commet SDK in Next.js applications
Install
pnpm add @commet/node @commet/nextnpm install @commet/node @commet/nextyarn add @commet/node @commet/nextbun add @commet/node @commet/nextConfigure
COMMET_API_KEY=ck_sandbox_xxx
COMMET_WEBHOOK_SECRET=whsec_xxxInitialize
import { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: process.env.COMMET_API_KEY!,
environment: 'sandbox',
})Create Subscription
'use server'
import { redirect } from 'next/navigation'
import { commet } from '@/lib/commet'
export async function subscribe(externalId: string) {
await commet.customers.create({
email: 'user@example.com',
externalId,
})
const subscription = await commet.subscriptions.create({
externalId,
planCode: 'pro',
})
redirect(subscription.data.checkoutUrl!)
}Handle Payment
import { Webhooks } from '@commet/next'
export const POST = Webhooks({
webhookSecret: process.env.COMMET_WEBHOOK_SECRET!,
onSubscriptionActivated: async (payload) => {
// Grant access to payload.data.externalId
},
})Check Access
const subscription = await commet.subscriptions.get({ externalId: 'user_123' })
if (subscription.data?.status === 'active') {
// User has paid
}Customer Portal
import { CustomerPortal } from '@commet/next'
export const GET = CustomerPortal({
apiKey: process.env.COMMET_API_KEY!,
getCustomerId: async (req) => {
// Return user's externalId from your auth
return 'user_123'
},
})<a href="/api/commet/portal">Manage Billing</a>Examples
Full working examples to get you started:
Fixed SaaS
Billing integration for a fixed-price SaaS. Perfect for simple subscription plans.
Team SaaS
Seat-based billing with workspace management. Ideal for team collaboration products.
Related
How is this guide?