Integrar con Nuxt
Agrega cobros y pagos a tu aplicación de Nuxt.
Usa este prompt prearmado para integrar Commet más rápido con asistentes de IA.
Instalar
pnpm add @commet/nodenpm install @commet/nodeyarn add @commet/nodebun add @commet/nodeConfigurar
COMMET_API_KEY=ck_sandbox_xxxexport default defineNuxtConfig({
runtimeConfig: {
commetApiKey: process.env.COMMET_API_KEY,
},
})Nuxt importa automáticamente desde server/utils/, por lo que el cliente está disponible en todas las rutas del servidor.
import { Commet } from '@commet/node'
const config = useRuntimeConfig()
export const commet = new Commet({
apiKey: config.commetApiKey,
})Suscribir
customers.create es idempotente — si ya existe un cliente con el mismo id, retorna el registro existente.
export default defineEventHandler(async (event) => {
const { customerId, email } = await readBody(event)
await commet.customers.create({ email, id: customerId })
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
return { checkoutUrl: subscription.data.checkoutUrl }
})Verificar Acceso
export default defineEventHandler(async (event) => {
const customerId = getRouterParam(event, 'customerId')!
const { data: subscription } = await commet.subscriptions.get(customerId)
const { data: feature } = await commet.features.check({
code: 'api_calls',
customerId,
})
return {
status: subscription.status,
allowed: feature.allowed,
}
})Trackear Uso
export default defineEventHandler(async (event) => {
const { customerId } = await readBody(event)
await commet.usage.track({
customerId,
feature: 'api_calls',
value: 1,
})
return { tracked: true }
})El uso se agrega y se cobra al final del período.
Portal del Cliente
export default defineEventHandler(async (event) => {
const customerId = 'user_123'
const { data } = await commet.portal.getUrl({ customerId })
return sendRedirect(event, data.portalUrl)
})Relacionado
¿Cómo está esta guía?