Integrar con SvelteKit
Agrega cobros y pagos a tu aplicación de SvelteKit.
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_xxximport { Commet } from '@commet/node'
import { COMMET_API_KEY } from '$env/static/private'
export const commet = new Commet({
apiKey: COMMET_API_KEY,
})Suscribir
customers.create es idempotente — si ya existe un cliente con el mismo id, retorna el registro existente.
import { json } from '@sveltejs/kit'
import { commet } from '$lib/server/commet'
import type { RequestHandler } from './$types'
export const POST: RequestHandler = async ({ request }) => {
const { customerId, email } = await request.json()
await commet.customers.create({ email, id: customerId })
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
return json({ checkoutUrl: subscription.data.checkoutUrl })
}Verificar Acceso
import { json } from '@sveltejs/kit'
import { commet } from '$lib/server/commet'
import type { RequestHandler } from './$types'
export const GET: RequestHandler = async ({ params }) => {
const { data: subscription } = await commet.subscriptions.get(params.customerId)
const { data: feature } = await commet.features.check({
code: 'api_calls',
customerId: params.customerId,
})
return json({ status: subscription.status, allowed: feature.allowed })
}Trackear Uso
import { json } from '@sveltejs/kit'
import { commet } from '$lib/server/commet'
import type { RequestHandler } from './$types'
export const POST: RequestHandler = async ({ request }) => {
const { customerId } = await request.json()
await commet.usage.track({
customerId,
feature: 'api_calls',
value: 1,
})
return json({ tracked: true })
}El uso se agrega y se cobra al final del período.
Portal del Cliente
import { redirect } from '@sveltejs/kit'
import { commet } from '$lib/server/commet'
import type { RequestHandler } from './$types'
export const GET: RequestHandler = async ({ locals }) => {
const { data } = await commet.portal.getUrl({
customerId: locals.user.customerId,
})
redirect(303, data.portalUrl)
}Relacionado
¿Cómo está esta guía?