Integrate with Hono
Add billing and payments to your Hono application.
Use this pre-built prompt to integrate Commet faster with AI assistants.
Install
pnpm add @commet/node hononpm install @commet/node honobun add @commet/node honoConfigure
COMMET_API_KEY=ck_sandbox_xxximport { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: process.env.COMMET_API_KEY!,
environment: 'sandbox',
})Subscribe
import { Hono } from 'hono'
import { commet } from '../commet'
const billing = new Hono()
billing.post('/subscribe', async (c) => {
const { customerId, email } = await c.req.json()
await commet.customers.create({ email, id: customerId })
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
return c.json({ checkoutUrl: subscription.data.checkoutUrl })
})
export default billingCheck Access
billing.get('/subscription/:customerId', async (c) => {
const { data } = await commet.subscriptions.get(c.req.param('customerId'))
return c.json({ status: data.status })
})
billing.get('/features/:feature/:customerId', async (c) => {
const { data } = await commet.features.check({
code: c.req.param('feature'),
customerId: c.req.param('customerId'),
})
return c.json({ allowed: data.allowed })
})Track Usage
billing.post('/usage', async (c) => {
const { customerId } = await c.req.json()
await commet.usage.track({
customerId,
feature: 'api_calls',
value: 1,
})
return c.json({ tracked: true })
})Usage is aggregated and billed at end of period.
Customer Portal
billing.get('/portal', async (c) => {
const customerId = c.get('customerId')
const { data } = await commet.portal.getUrl({ customerId })
return c.redirect(data.portalUrl)
})Start Server
import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import billing from './routes/billing'
const app = new Hono()
app.route('/billing', billing)
serve({ fetch: app.fetch, port: 3000 })import { Hono } from 'hono'
import billing from './routes/billing'
const app = new Hono()
app.route('/billing', billing)
export default appRelated
How is this guide?