Integrate with Remix
Add billing and payments to your Remix application.
Use this pre-built prompt to integrate Commet faster with AI assistants.
Install
pnpm add @commet/node @remix-run/nodenpm install @commet/node @remix-run/nodeyarn add @commet/node @remix-run/nodebun add @commet/node @remix-run/nodeConfigure
COMMET_API_KEY=ck_sandbox_xxxThe .server.ts suffix ensures this module is never bundled into client code.
import { Commet } from '@commet/node'
export const commet = new Commet({
apiKey: process.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.
import { redirect, type ActionFunctionArgs } from '@remix-run/node'
import { commet } from '~/lib/commet.server'
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData()
const customerId = String(formData.get('customerId'))
const email = String(formData.get('email'))
await commet.customers.create({ email, id: customerId })
const subscription = await commet.subscriptions.create({
customerId,
planCode: 'pro',
})
return redirect(subscription.data.checkoutUrl!)
}Check Access
import { json, type LoaderFunctionArgs } from '@remix-run/node'
import { commet } from '~/lib/commet.server'
export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url)
const customerId = url.searchParams.get('customerId')!
const { data: subscription } = await commet.subscriptions.get(customerId)
const { data: feature } = await commet.features.check({
code: 'api_calls',
customerId,
})
return json({
status: subscription.status,
allowed: feature.allowed,
})
}Track Usage
import { json, type ActionFunctionArgs } from '@remix-run/node'
import { commet } from '~/lib/commet.server'
export async function action({ request }: ActionFunctionArgs) {
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
import { redirect, type LoaderFunctionArgs } from '@remix-run/node'
import { commet } from '~/lib/commet.server'
export async function loader({ request }: LoaderFunctionArgs) {
const customerId = 'user_123'
const { data } = await commet.portal.getUrl({ customerId })
return redirect(data.portalUrl)
}Related
How is this guide?