Billing SDK

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

OptionDefaultWhat it does
apiKeyprocess.env.FAURYA_API_KEYYour site API key.
timeoutMs30000How long each request may take, in milliseconds (up to 10 minutes).
maxRetries2How many times to retry failures that are safe to retry, 0–10. 0 turns retries off. See Errors and retries.
baseUrlhttps://api.faurya.com/api/v1Where requests go. You only need this for a proxy. Must be https:// (http:// is allowed for localhost only).
fetchthe built-in fetchYour own fetch function, for proxies or tests.
dangerouslyAllowBrowserfalseLets 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:

lib/faurya.server.ts
import { createFauryaBilling } from "@faurya/billing";

export const faurya = createFauryaBilling();

new FauryaBilling(options) does the same thing as createFauryaBilling(options), if you prefer classes.

Runtimes

RuntimeSupported
Node.js 18+Yes
BunYes
DenoYes
Vercel Edge, Cloudflare Workers and other edge runtimesYes
Next.js route handlers, server actions, server componentsYes
Browsers, React client components, mobile appsNo — 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 FauryaConfigError instead of sending your key. Don't work around this with dangerouslyAllowBrowser — call your own backend from the browser, and the SDK from there.
  • The key is only ever sent to Faurya, over HTTPS, in the Authorization header.
  • It never appears in logs. It's removed from error messages, and console.log(faurya) or JSON.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_KEY in your server environment only — no NEXT_PUBLIC_, VITE_ or PUBLIC_ 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.