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 Bun

Add billing and payments to your Bun application.

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

Install

bun add @commet/node

Configure

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

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

Subscribe

src/index.ts
import { commet } from './commet'

Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url)

    if (url.pathname === '/subscribe' && req.method === 'POST') {
      const { customerId, email } = await req.json()

      await commet.customers.create({ email, id: customerId })

      const subscription = await commet.subscriptions.create({
        customerId,
        planCode: 'pro',
      })

      return Response.json({ checkoutUrl: subscription.data.checkoutUrl })
    }

    return new Response('Not Found', { status: 404 })
  },
})

Check Access

Add these routes to the fetch handler.

src/index.ts
if (url.pathname.startsWith('/subscription/') && req.method === 'GET') {
  const customerId = url.pathname.split('/')[2]

  const { data } = await commet.subscriptions.get(customerId)

  return Response.json({ status: data.status })
}

if (url.pathname.startsWith('/features/') && req.method === 'GET') {
  const [, , feature, customerId] = url.pathname.split('/')

  const { data } = await commet.features.check({ code: feature, customerId })

  return Response.json({ allowed: data.allowed })
}

Track Usage

src/index.ts
if (url.pathname === '/usage' && req.method === 'POST') {
  const { customerId } = await req.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/index.ts
if (url.pathname === '/portal' && req.method === 'GET') {
  const customerId = url.searchParams.get('customerId')!

  const { data } = await commet.portal.getUrl({ customerId })

  return Response.redirect(data.portalUrl)
}

Related

  • Subscriptions
  • Track Usage
  • Customer Portal
  • SDK Reference

How is this guide?

Integrate with Hono

Add billing and payments to your Hono application.

Integrate with Python

Install and configure the Commet Python SDK.

On this page

Install
Configure
Subscribe
Check Access
Track Usage
Customer Portal
Related