@commet/next
v0.3.3
Published
Next.js integration for Commet billing platform
Maintainers
Readme
Ready-to-use Next.js route handlers for Commet webhooks and customer portal. Drop them into your App Router and you're set.
Installation
npm install @commet/next @commet/nodeGetting Started
1. Set up your environment
Get your API key and webhook secret from your Commet dashboard.
# .env
COMMET_API_KEY=ck_...
COMMET_WEBHOOK_SECRET=whsec_...2. Add webhook handler
Create a route handler to receive billing events from Commet:
// 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 your product
},
onSubscriptionCanceled: async (payload) => {
// Revoke access
},
onSubscriptionCreated: async (payload) => {
// Handle new subscriptions
},
onSubscriptionUpdated: async (payload) => {
// Handle plan changes
},
onPayload: async (payload) => {
// Catch-all for any event
},
});Then register the URL https://yourapp.com/api/webhooks/commet in your Commet dashboard under Settings → Webhooks.
3. Add customer portal
Let your customers manage their billing (update payment method, view invoices, cancel) without leaving your app:
// app/api/commet/portal/route.ts
import { CustomerPortal } from "@commet/next";
import { auth } from "@/lib/auth";
export const GET = CustomerPortal({
apiKey: process.env.COMMET_API_KEY!,
getCustomerId: async (req) => {
const session = await auth.api.getSession({ headers: req.headers });
return session?.user.id ?? null;
},
});Then link to it from your dashboard:
<Button asChild>
<Link href="/api/commet/portal">Manage Billing</Link>
</Button>Note About Webhooks
Webhooks are optional in Commet. You can always query the current state directly using @commet/node:
const subscription = await commet.subscriptions.get({ externalId: userId });
const features = await commet.features.list({ externalId: userId });Webhooks are useful when you want to react immediately to changes — send emails, update your database, revoke access, etc.
Documentation
Visit commet.co/docs for the full guide on webhooks, portal customization, and more.
License
MIT
