@cubis/cambill
v0.4.0
Published
CamBill SDK for JavaScript and TypeScript: payments, customers, products, invoices and subscriptions, with retries, idempotency and webhook verification.
Readme
@cubis/cambill
CamBill for JavaScript and TypeScript: payments, customers, products, invoices and subscriptions. Zero dependencies; Node 20+, Bun, Deno and Workers.
npm install @cubis/cambillimport { CamBill } from "@cubis/cambill"
const cambill = new CamBill({ apiKey: process.env.CAMBILL_API_KEY! }) // khpay_test_… or khpay_live_…
const customer = await cambill.customers.create({ external_id: "user_42", name: "Sok Dara", email: "[email protected]" })
const invoice = await cambill.invoices.create({
customer: customer.id,
currency: "USD",
lines: [{ description: "Consulting", quantity: 2, unit_amount: 5000 }], // $50.00 each, in cents
})
const issued = await cambill.invoices.finalize(invoice.id)
console.log(issued.hosted_invoice_url) // send this to your customer- Server only. The key is a secret. Never put it in a browser or an app.
- Safe to retry. Every POST carries an
Idempotency-Key(yours, or a random one reused on each retry). 429s, 5xx and network errors are retried with backoff. - Typed errors. Failures throw
CamBillErrorwithstatus,title,traceIdandretryable. - Webhooks.
await cambill.webhooks.constructEvent({ payload: rawBody, signature: req.headers["x-cambill-signature"], secret })verifies and parses. Pass the body exactly as received.
Docs: https://docs.cambill.cubis.tech/docs
Next.js, Remix, TanStack Start
// app/api/cambill/checkout/route.ts: you decide the amount, on your server
import { checkout, webhook } from "@cubis/cambill/next" // or /remix, /tanstack-start, /server
export const POST = checkout(async (req) => {
const order = await db.orders.find((await req.json()).orderId)
return { amount: order.total, currency: "USD", external_id: order.id }
})
// app/api/cambill/webhook/route.ts: verified, typed, retried when your handler throws
export const POST = webhook({ "payment_intent.succeeded": (e) => db.orders.markPaid(e.data.external_id!) })<CamBillCheckout endpoint="/api/cambill/checkout" body={{ orderId }} />Keys come from CAMBILL_API_KEY and CAMBILL_WEBHOOK_SECRET. Guide: https://docs.cambill.cubis.tech/docs/frameworks
Checkout widget in the browser
import { CamBillCheckout } from "@cubis/cambill/react" // or /vue, /svelte
<CamBillCheckout intent={pi.id} secret={pi.client_secret} onSuccess={confirmOnServer} />Pass the intent id and client_secret from your server, never an API key. The loader is always the current release.
