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 Express

Add billing and payments to your Express application.

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

Install

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

Configure

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

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

Subscribe

src/routes/billing.ts
import { Router } from 'express'
import { commet } from '../commet'

const router = Router()

router.post('/subscribe', async (req, res) => {
  const { customerId, email } = req.body

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

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

  res.json({ checkoutUrl: subscription.data.checkoutUrl })
})

export default router

Check Access

src/routes/billing.ts
router.get('/subscription/:customerId', async (req, res) => {
  const { data } = await commet.subscriptions.get(req.params.customerId)

  res.json({ status: data.status })
})

router.get('/features/:feature/:customerId', async (req, res) => {
  const { data } = await commet.features.check({
    code: req.params.feature,
    customerId: req.params.customerId,
  })

  res.json({ allowed: data.allowed })
})

Track Usage

src/routes/billing.ts
router.post('/usage', async (req, res) => {
  await commet.usage.track({
    customerId: req.body.customerId,
    feature: 'api_calls',
    value: 1,
  })

  res.json({ tracked: true })
})

Usage is aggregated and billed at end of period.

Customer Portal

src/routes/billing.ts
router.get('/portal', async (req, res) => {
  const { data } = await commet.portal.getUrl({
    customerId: req.user.customerId,
  })

  res.redirect(data.portalUrl)
})

Start Server

src/index.ts
import express from 'express'
import billingRoutes from './routes/billing'

const app = express()

app.use(express.json())
app.use('/billing', billingRoutes)

app.listen(3000)

Related

  • Subscriptions
  • Track Usage
  • Customer Portal
  • SDK Reference

How is this guide?

Integrate with Astro

Add billing and payments to your Astro application.

Integrate with Hono

Add billing and payments to your Hono application.

On this page

Install
Configure
Subscribe
Check Access
Track Usage
Customer Portal
Start Server
Related