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 SvelteKit

Add billing and payments to your SvelteKit application.

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

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/server/commet.ts
import { Commet } from '@commet/node'
import { COMMET_API_KEY } from '$env/static/private'

export const commet = new Commet({
  apiKey: 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/routes/api/billing/subscribe/+server.ts
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 })
}

Check Access

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

Track Usage

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

Usage is aggregated and billed at end of period.

Customer Portal

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

Related

  • Subscriptions
  • Track Usage
  • Customer Portal
  • SDK Reference

How is this guide?

Integrate with Nuxt

Add billing and payments to your Nuxt application.

Integrate with Astro

Add billing and payments to your Astro application.

On this page

Install
Configure
Subscribe
Check Access
Track Usage
Customer Portal
Related