Integrar con Next.js
Agrega cobros y pagos a tu aplicación de Next.js con Commet.
Usa este prompt prearmado para integrar Commet más rápido con asistentes de IA.
Instalar
pnpm add @commet/node @commet/nextnpm install @commet/node @commet/nextyarn add @commet/node @commet/nextbun add @commet/node @commet/nextConfigurar
COMMET_API_KEY=ck_sandbox_xxximport { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: process.env.COMMET_API_KEY!,
})Crear Cliente y Suscribir
customers.create es idempotente — si ya existe un cliente con el mismo id, retorna el registro existente.
'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',
})
redirect(subscription.data.checkoutUrl!)
}El cliente es redirigido al checkout para completar el pago.
Verificar Acceso
export async function getSubscription(customerId: string) {
const { data } = await commet.subscriptions.get(customerId)
return data
}export async function canUseFeature(customerId: string, feature: string) {
const { data } = await commet.features.check({ code: feature, customerId })
return data.allowed
}Trackear Uso
export async function trackApiCall(customerId: string) {
await commet.usage.track({
customerId,
feature: 'api_calls',
value: 1,
})
}El uso se agrega y se cobra al final del período.
Portal del Cliente
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">Gestionar Cobros y Pagos</a>Relacionado
¿Cómo está esta guía?