Integrate with Astro
Add billing and payments to your Astro application.
Install the Commet Skill so your coding agent can integrate the current SDK and verify its work against the live API contract.
npx skills add commet-labs/skills --skill commetAstro requires an SSR adapter for API routes. Set output: 'server' or output: 'hybrid' in your astro.config.mjs.
Install
pnpm add @commet/nodenpm install @commet/nodeyarn add @commet/nodebun add @commet/nodeConfigure
COMMET_API_KEY=ck_sandbox_xxximport { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: import.meta.env.COMMET_API_KEY,
})Subscribe
customers.create is idempotent — if a customer with the same id already exists, it returns the existing record.
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.checkoutUrl ?? null })
}Check Access
import type { APIRoute } from 'astro'
import { commet } from '../../../../lib/commet'
export const GET: APIRoute = async ({ params }) => {
const subscription = await commet.subscriptions.getActive({ customerId: params.customerId! })
if (!subscription) {
return Response.json({ error: 'no_active_subscription' }, { status: 404 })
}
const feature = await commet.featureAccess.get({
code: 'api_calls',
customerId: params.customerId!,
})
return Response.json({
status: subscription.status,
allowed: feature.allowed,
})
}Track Usage
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,
featureCode: 'api_calls',
value: 1,
})
return Response.json({ tracked: true })
}Usage is aggregated and billed at end of period.
Customer Portal
import type { APIRoute } from 'astro'
import { commet } from '../../../lib/commet'
export const GET: APIRoute = async ({ redirect }) => {
const customerId = 'user_123'
const portal = await commet.portal.getUrl({ customerId })
return redirect(portal.portalUrl)
}Related
How is this guide?