Quickstart
A customer, a product and an invoice your customer can pay, in five minutes.
In test mode, you'll create a customer, sell a product, issue an invoice, watch it get paid, and start a subscription. The code on this page runs against a real stack on every change, so it works as written.
Building on Next.js, Remix or TanStack Start and only need payments? Go to Frameworks: two route files and one component.
Set up
- Sign up at the dashboard and create a business.
- Connectors → Bakong KHQR: add an account ID and turn it on for test mode. This is how your customers will pay.
- Copy your test API key (
khpay_test_…) from API & webhooks. Keep it on your server.
npm install @cubis/cambillCreate a customer, a product and a price
Amounts are whole numbers in the smallest unit: 5000 is $50.00.
import { CamBill } from "@cubis/cambill"
const cambill = new CamBill({ apiKey: process.env.CAMBILL_API_KEY! })
// external_id is your own id for them.
const customer = await cambill.customers.create({ external_id: "user_42", name: "Sok Dara", email: "sok@example.com" })
// external_id on the product is your own id for the plan.
const product = await cambill.products.create({ name: "Consulting day", external_id: "plan_consulting" })
const price = await cambill.prices.create({ product: product.id, currency: "USD", unit_amount: 5000 })Customers and invoices are experimental and switched on per business. If a call answers 403 Feature Not Enabled, ask CamBill to enable it for your business.
Issue an invoice
const draft = await cambill.invoices.create({
customer: customer.id,
currency: "USD",
lines: [{ price: price.id, quantity: 2 }],
due_in_days: 14,
metadata: { order_id: "1001" },
})
const invoice = await cambill.invoices.finalize(draft.id)
console.log(invoice.number, invoice.total, invoice.hosted_invoice_url)
// INV-000001 10000 https://…/invoice/inv_test_…Send hosted_invoice_url to your customer. They see the invoice, pay it, and can download a PDF with a QR code.
Pay it, and know it is paid
Open the link and press Pay. In test mode the checkout has a Simulate payment button.
const paid = await cambill.invoices.retrieve(invoice.id)
paid.status // "paid"In production, don't poll. Handle the invoice.paid webhook instead:
const event = await cambill.webhooks.constructEvent({ payload: rawBody, signature, secret: process.env.CAMBILL_WEBHOOK_SECRET! })
if (event.type === "invoice.paid") console.log("paid:", event.data.id) // event.data is typed by event.typeBill monthly
const monthly = await cambill.prices.create({ product: product.id, currency: "USD", unit_amount: 900, type: "recurring", interval: "month" })
const subscription = await cambill.subscriptions.create({ customer: customer.id, items: [{ price: monthly.id, quantity: 1 }] })
// Test mode only: bill next month now, instead of waiting.
await cambill.subscriptions.advance(subscription.id)Each period is an invoice, and your customer pays each one from its link. Find subscriptions by your own ids, so you never need to store CamBill's:
const mine = await cambill.subscriptions.list({ product_external_id: "plan_consulting", status: "active", expand: "customer,items" })Or start with sample data
In the dashboard, API & webhooks → Add sample data creates a customer, a product with prices, a tax rate and an issued invoice in test mode, so you have something to call straight away.
Next
- Frameworks: Next.js, Remix and TanStack Start.
- Webhooks: get told when something happens.
- Conventions: idempotency, metadata, pagination.
- Going live.