Billing SDK

Getting started

Install @faurya/billing, add your API key and record your first payment in a few minutes.

Getting started

This guide takes you from nothing to a payment showing up in your Faurya dashboard, attributed to the visitor who made it.

Before you start

  • A Faurya site with the tracking script installed. The script is what sets the faurya_visitor_id cookie that links revenue to traffic.
  • A server: Node.js 18 or newer, Bun, Deno, or an edge runtime.
  • Your site's API key, generated from your Faurya dashboard. It is the same key the rest of the Faurya API uses, and it decides which site the data belongs to — there is no site id to pass.

1. Install the package

npm install @faurya/billing

Or with your package manager of choice:

pnpm add @faurya/billing
yarn add @faurya/billing
bun add @faurya/billing

2. Add your API key

Put the key in your server environment file:

.env
FAURYA_API_KEY=your_api_key

Never prefix it with NEXT_PUBLIC_, VITE_, PUBLIC_ or anything else that ships variables to the browser. Anyone with this key can write revenue data to your site.

3. Create the client once

Create one client and reuse it everywhere. It reads FAURYA_API_KEY on its own.

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

export const faurya = createFauryaBilling();

If you keep the key somewhere else, pass it in:

export const faurya = createFauryaBilling({ apiKey: mySecrets.fauryaKey });

A missing or malformed key fails straight away with a clear message, rather than on your first request:

[Faurya] Missing FAURYA_API_KEY. Add it to your server env file.

4. Read the visitor id

Faurya attributes revenue through the visitor id its tracking script stores in the browser. Your server reads it from the request cookies:

  • faurya_visitor_id — who the visitor is. Required for payments and goals.
  • faurya_session_id — the current visit. Optional, but improves attribution.

Read them where the visitor is still in the request — usually when they start checkout — and save them with the user or pass them to your payment provider as checkout metadata. Webhooks arrive without the visitor's cookies, so they need the value you saved.

5. Record your first payment

Here it is in a Next.js route handler. The SDK call is the same in any framework.

app/api/checkout/complete/route.ts
import { cookies } from "next/headers";
import { faurya } from "@/lib/faurya.server";

export async function POST(request: Request) {
  const { orderId, total } = await request.json();
  const cookieStore = await cookies();

  await faurya.payments.create({
    transaction_id: orderId,
    amount: total,      // 29.99 means $29.99
    currency: "USD",
    faurya_visitor_id: cookieStore.get("faurya_visitor_id")?.value ?? "",
    faurya_session_id: cookieStore.get("faurya_session_id")?.value,
  });

  return Response.json({ ok: true });
}

The same thing in Express:

server.ts
import express from "express";
import cookieParser from "cookie-parser";
import { faurya } from "./lib/faurya.server";

const app = express();
app.use(express.json(), cookieParser());

app.post("/checkout/complete", async (req, res) => {
  await faurya.payments.create({
    transaction_id: req.body.orderId,
    amount: req.body.total,
    currency: "USD",
    faurya_visitor_id: req.cookies.faurya_visitor_id,
    faurya_session_id: req.cookies.faurya_session_id,
  });
  res.json({ ok: true });
});

Open your Faurya dashboard: the payment appears in your revenue, linked to the visitor's traffic source, pages and journey.

The visitor does not need to exist yet. A payment made moments after a first page view is recorded immediately and linked as soon as the visit is processed. Goals are stricter: the visitor must already exist.

Using the faurya package and CLI

If you already use the faurya npm package for browser analytics, the same client is also available from its faurya/billing entry point:

import { createFauryaBilling } from "faurya/billing";

And npx faurya init --with-api generates a ready-made lib/faurya-api.ts for you, with:

  • getFaurya() — the shared billing client;
  • createFauryaGoal(visitorId, name, metadata?);
  • createFauryaPayment(amount, currency, transactionId, { fauryaVisitorId, ... });
  • deleteFauryaPayment(filters).

Both use the exact client described in these docs, so everything here applies.

Next steps