Configuration
Options for createFauryaBilling, per-request overrides, supported runtimes and how the SDK keeps your API key safe.
Configuration
The defaults work for most apps: set FAURYA_API_KEY and call createFauryaBilling(). Everything below is optional.
import { createFauryaBilling } from "@faurya/billing";
const faurya = createFauryaBilling({
apiKey: process.env.FAURYA_API_KEY,
timeoutMs: 30_000,
maxRetries: 2,
});Client options
| Option | Default | What it does |
|---|---|---|
apiKey | process.env.FAURYA_API_KEY | Your site API key. |
timeoutMs | 30000 | How long each request may take, in milliseconds (up to 10 minutes). |
maxRetries | 2 | How many times to retry failures that are safe to retry, 0–10. 0 turns retries off. See Errors and retries. |
baseUrl | https://api.faurya.com/api/v1 | Where requests go. You only need this for a proxy. Must be https:// (http:// is allowed for localhost only). |
fetch | the built-in fetch | Your own fetch function, for proxies or tests. |
dangerouslyAllowBrowser | false | Lets the client run in a browser. Don't — see below. |
An invalid option fails immediately with a FauryaConfigError that says what's wrong, so mistakes show up at startup rather than on the first request.
Per-request options
Every method takes an optional last argument to override settings for that one call:
await faurya.billing.invoices.upsert("inv_1042", invoice, {
timeoutMs: 60_000, // longer timeout for this call
maxRetries: 0, // no retries for this call
signal: controller.signal, // cancel it with an AbortController
});Create the client once
The client is cheap, but it's best to create it once and import it wherever you need it:
import { createFauryaBilling } from "@faurya/billing";
export const faurya = createFauryaBilling();new FauryaBilling(options) does the same thing as createFauryaBilling(options), if you prefer classes.
Runtimes
| Runtime | Supported |
|---|---|
| Node.js 18+ | Yes |
| Bun | Yes |
| Deno | Yes |
| Vercel Edge, Cloudflare Workers and other edge runtimes | Yes |
| Next.js route handlers, server actions, server components | Yes |
| Browsers, React client components, mobile apps | No — server only |
The package ships both ESM (import) and CommonJS (require), with TypeScript types included. It has no dependencies.
On runtimes without process.env (such as Cloudflare Workers), pass the key yourself:
export default {
async fetch(request: Request, env: { FAURYA_API_KEY: string }) {
const faurya = createFauryaBilling({ apiKey: env.FAURYA_API_KEY });
// ...
},
};Keeping your API key safe
Your API key can write revenue data to your site, so the SDK guards it:
- It refuses to run in a browser. If it detects a browser it throws a
FauryaConfigErrorinstead of sending your key. Don't work around this withdangerouslyAllowBrowser— call your own backend from the browser, and the SDK from there. - The key is only ever sent to Faurya, over HTTPS, in the
Authorizationheader. - It never appears in logs. It's removed from error messages, and
console.log(faurya)orJSON.stringify(faurya)don't print it. - Malformed keys are rejected, such as a key with a stray space or newline copied in from a dashboard.
And on your side:
- keep
FAURYA_API_KEYin your server environment only — noNEXT_PUBLIC_,VITE_orPUBLIC_prefix; - don't commit it to git;
- if it leaks, generate a new key in your Faurya dashboard.
Ids
Every id you pass (customer, plan, invoice, transaction, event) may be 1–255 characters of letters, digits and _ - . : @. Ids are case-sensitive and never trimmed. An id outside these rules is rejected by the SDK before anything is sent, with a FauryaInputError.
Most ids from payment providers and databases (cus_9Xk2, inv-2026-0042, UUIDs) already fit.
TypeScript
All request and response types are exported, so you can type your own helpers:
import type {
InvoiceUpsertParams,
TransactionUpsertParams,
Subscription,
ImportItem,
} from "@faurya/billing";
function toFauryaInvoice(order: Order): InvoiceUpsertParams {
return {
customer_external_id: order.customerId,
currency: order.currency,
invoiced_at: order.createdAt.toISOString(),
line_items: order.lines.map((l) => ({ external_id: l.id, type: "one_time", amount: l.total })),
};
}Field names are snake_case, exactly as in the Billing API reference, so you can copy examples between the two.