Installation

Next.js Setup

Use Commet SDK in Next.js applications

Install

pnpm add @commet/node @commet/next
npm install @commet/node @commet/next
yarn add @commet/node @commet/next
bun add @commet/node @commet/next

Configure

.env.local
COMMET_API_KEY=ck_sandbox_xxx
COMMET_WEBHOOK_SECRET=whsec_xxx

Initialize

lib/commet.ts
import { Commet } from '@commet/node'

export const commet = new Commet({
  apiKey: process.env.COMMET_API_KEY!,
  environment: 'sandbox',
})

Create Subscription

app/actions/billing.ts
'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

app/api/webhooks/commet/route.ts
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

app/api/commet/portal/route.ts
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:

How is this guide?