Bulk import
Backfill your billing history into Faurya with billing.imports.run — stream straight from your database, any order, one call.
Bulk import
When you first connect your billing to Faurya, you'll want your history too — every customer, invoice and payment from before today. Sending those one by one would take tens of thousands of requests and run into the rate limit. billing.imports.run() sends them as one import job instead.
import { createFauryaBilling } from "@faurya/billing";
const { billing } = createFauryaBilling();
const result = await billing.imports.run([
{ type: "customer", external_id: "cus_42", email: "ada@example.com" },
{ type: "plan", external_id: "plan_pro_monthly", name: "Pro", interval_unit: "month", interval_count: 1 },
{
type: "invoice",
external_id: "inv_1042",
customer_external_id: "cus_42",
currency: "USD",
invoiced_at: "2026-03-01T00:00:00Z",
line_items: [
{
external_id: "line_1",
type: "subscription",
amount: 49,
subscription_external_id: "sub_900",
plan_external_id: "plan_pro_monthly",
service_period_start: "2026-03-01T00:00:00Z",
service_period_end: "2026-04-01T00:00:00Z",
},
],
},
{
type: "transaction",
external_id: "txn_7781",
invoice_external_id: "inv_1042",
transaction_type: "payment",
amount: 49,
occurred_at: "2026-03-01T00:00:05Z",
},
]);
console.log(result.import.status); // "succeeded"run() does the whole job for you:
- opens an import;
- uploads your items in batches that fit the size limits;
- commits it;
- waits until Faurya has processed everything, and returns the result.
What an item looks like
Each item is exactly what you would pass to the single-object call, plus two fields:
type—customer,plan,invoice,transactionorsubscription_event;external_id— the id you would otherwise pass as the first argument.
One difference for transactions: inside an import, the transaction kind
(payment, refund…) goes in transaction_type, because type already
says the item is a transaction.
Order doesn't matter. Unlike the single calls, you can send items in any order — invoices before their customers, payments before their invoices. The import sorts everything into the right order when it runs.
Stream large histories
run() accepts arrays, generators and async generators, so you never have to load your whole history into memory. Yield items as you read them from your database:
async function* history() {
for await (const c of db.customers.stream()) {
yield { type: "customer", external_id: c.id, email: c.email, name: c.name } as const;
}
for await (const p of db.plans.stream()) {
yield { type: "plan", external_id: p.id, name: p.name, interval_unit: p.interval, interval_count: 1 } as const;
}
for await (const inv of db.invoices.stream()) {
yield { type: "invoice", external_id: inv.id, ...toFauryaInvoice(inv) } as const;
}
for await (const pay of db.payments.stream()) {
yield {
type: "transaction",
external_id: pay.id,
invoice_external_id: pay.invoiceId,
transaction_type: "payment",
amount: pay.amount,
occurred_at: pay.paidAt.toISOString(),
} as const;
}
}
const result = await billing.imports.run(history(), {
externalId: "initial_backfill",
onBatch: (p) => console.log(`uploaded ${p.uploaded} items`),
onProgress: (imp) => console.log(imp.status, `${imp.items.done}/${imp.items.total}`),
});Read the result
const { import: imp, accepted, rejected } = result;
imp.status; // see the table below
accepted; // items Faurya accepted at upload
rejected; // items refused at upload, with the reason
imp.failures; // items that were accepted but failed while processingThere are two places an item can fail, and you should check both:
Refused at upload — the item itself is invalid (a missing field, too many decimal places…). Each rejection tells you its position in your input and every problem with it:
for (const r of result.rejected) {
console.log(`item #${r.index} (${r.type} ${r.external_id})`);
for (const e of r.errors) console.log(` ${e.field}: ${e.message}`);
}Failed while processing — the item was valid but couldn't be recorded, usually because something it refers to is missing:
for (const f of result.import.failures) {
console.log(`${f.type} ${f.external_id}: ${f.error}`); // e.g. "invoice inv_9: CUSTOMER_NOT_FOUND"
}One bad item never stops the rest. Fix the failed ones and send them again — in another import or with the single calls. Since everything is keyed by your ids, re-sending items that already succeeded is harmless.
status | Meaning |
|---|---|
succeeded | Every item was recorded. |
succeeded_with_errors | Finished, but some items failed — see failures. |
failed | The run could not complete. |
cancelled | The import was cancelled. |
expired | It was left open too long and was cleaned up. |
Your MRR, subscriber and churn numbers update on their own as the import is processed — there's nothing to trigger afterwards.
Options
| Option | Default | What it does |
|---|---|---|
externalId | generated | Your own name for the import, so you can find it later. |
batchSize | 1000 | Items per upload, 1–1,000. |
maxBatchBytes | 4.5 MB | Maximum size of one upload (up to 5 MB). |
wait | true | Wait for processing to finish. false returns as soon as the import is committed. |
waitTimeoutMs | 1 hour | How long to wait before giving up (the import keeps running). |
pollIntervalMs | 2 seconds | How often to check progress. |
cancelOnError | true | Cancel the import if an upload fails part-way. |
onBatch | — | Called after each upload. |
onProgress | — | Called after each progress check. |
signal | — | An AbortSignal to stop waiting. |
Long imports
A large history can take a while to process. Waiting is optional — the import runs on Faurya's side either way:
// Start it and return immediately
const { import: imp } = await billing.imports.run(history(), { wait: false });
await saveImportId(imp.import_id);
// …later, from anywhere
const { data } = await billing.imports.retrieve(imp.import_id);
console.log(data.status, data.items);
// …or wait for it again
const finished = await billing.imports.waitForCompletion(imp.import_id, {
onProgress: (i) => console.log(i.status),
});If waitTimeoutMs runs out, run() throws a FauryaTimeoutError, but the import is not cancelled — it keeps going and you can check on it with retrieve().
If an upload fails before the import is committed (for example your database stream throws), the import is cancelled so you don't leave a half-uploaded job behind. Pass cancelOnError: false to keep it open instead.
Doing it step by step
If you'd rather control each step yourself:
const { data: opened } = await billing.imports.create({ external_id: "backfill_2026_03" });
const id = opened.import_id;
await billing.imports.uploadItems(id, firstThousandItems); // 1–1,000 items per call
await billing.imports.uploadItems(id, nextThousandItems);
await billing.imports.commit(id);
const finished = await billing.imports.waitForCompletion(id);
// Changed your mind before it finished?
await billing.imports.cancel(id);Uploading an item with an id you already sent in the same import replaces the earlier one, so a retried upload never duplicates anything.
Limits
| Open imports per site | 3 at a time |
| How long an import can stay open (before commit) | 7 days |
| Items per upload | 1,000 (handled for you by run()) |
| Size per upload | 5 MB (handled for you by run()) |
If you connected a payment provider, Faurya imports that provider's history itself. Only import data Faurya doesn't already receive, or it will count twice.
For the full rules see the Bulk import API reference.