Commet
  • Pricing
Log InTry out
Introduction

Quickstart

Integrate with Next.jsIntegrate with RemixIntegrate with NuxtIntegrate with SvelteKitIntegrate with AstroIntegrate with ExpressIntegrate with HonoIntegrate with Bun

Learn

Resources

SDK ReferenceError HandlingTestingCLI

Plugins

Better Auth
DocumentationKnowledge BaseBuild with AIAPI ReferenceWebhooks

Integrate with Astro

Add billing and payments to your Astro application.

Use this pre-built prompt to integrate Commet faster with AI assistants.

Astro requires an SSR adapter for API routes. Set output: 'server' or output: 'hybrid' in your astro.config.mjs.

Install

pnpm add @commet/node
npm install @commet/node
yarn add @commet/node
bun add @commet/node

Configure

.env
COMMET_API_KEY=ck_sandbox_xxx
src/lib/commet.ts
import { Commet } from '@commet/node'

export const commet = new Commet({
  apiKey: import.meta.env.COMMET_API_KEY,
  environment: 'sandbox',
})

Subscribe

customers.create is idempotent — if a customer with the same id already exists, it returns the existing record.

src/pages/api/billing/subscribe.ts
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 })
}

Check Access

src/pages/api/billing/access/[customerId].ts
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,
  })
}

Track Usage

src/pages/api/billing/usage.ts
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 })
}

Usage is aggregated and billed at end of period.

Customer Portal

src/pages/api/billing/portal.ts
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)
}

Related

  • Subscriptions
  • Track Usage
  • Customer Portal
  • SDK Reference

How is this guide?

Integrate with SvelteKit

Add billing and payments to your SvelteKit application.

Integrate with Express

Add billing and payments to your Express application.

On this page

Install
Configure
Subscribe
Check Access
Track Usage
Customer Portal
Related