Integrar con Astro
Agrega cobros y pagos a tu aplicación de Astro.
Usa este prompt prearmado para integrar Commet más rápido con asistentes de IA.
Astro requiere un adaptador SSR para las rutas API. Configura output: 'server' o output: 'hybrid' en tu astro.config.mjs.
Instalar
pnpm add @commet/nodenpm install @commet/nodeyarn add @commet/nodebun add @commet/nodeConfigurar
COMMET_API_KEY=ck_sandbox_xxximport { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: import.meta.env.COMMET_API_KEY,
})Suscribir
customers.create es idempotente — si ya existe un cliente con el mismo id, retorna el registro existente.
import type { APIRoute } from 'astro'
import { commet } from '../../../lib/commet'
export const POST: APIRoute = 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 Response.json({ checkoutUrl: subscription.data.checkoutUrl })
}Verificar Acceso
import type { APIRoute } from 'astro'
import { commet } from '../../../../lib/commet'
export const GET: APIRoute = 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 Response.json({
status: subscription.status,
allowed: feature.allowed,
})
}Trackear Uso
import type { APIRoute } from 'astro'
import { commet } from '../../../lib/commet'
export const POST: APIRoute = async ({ request }) => {
const { customerId } = await request.json()
await commet.usage.track({
customerId,
feature: 'api_calls',
value: 1,
})
return Response.json({ tracked: true })
}El uso se agrega y se cobra al final del período.
Portal del Cliente
import type { APIRoute } from 'astro'
import { commet } from '../../../lib/commet'
export const GET: APIRoute = async ({ redirect }) => {
const customerId = 'user_123'
const { data } = await commet.portal.getUrl({ customerId })
return redirect(data.portalUrl)
}Relacionado
¿Cómo está esta guía?