Skip to content

Generating Documents

fabricadabra’s Orders and Clients features turn your live records into polished PDFs — invoices, receipts, and contracts — by running a Typst template against a structured data payload. This tutorial covers that payload’s shape, how your template reads it, and how to develop against realistic data without a live order to point at.

A document-generation script is an ordinary Typst file with one defining trait: instead of reading loose sys.inputs.<name> params, it reads a single JSON input named data:

#let data = json(bytes(sys.inputs.data))

The generation engine builds that JSON from the order/client, its line items, and the account’s business profile, then passes it as --input data=<json>. Your template’s whole job is to lay that structure out.

An order-scoped document (invoice or receipt) receives:

business: { name, address, email, website, abn, colours: { primary, accent }, payment }
document: { id, issued, due, currency, tax_inclusive }
client: { name, address }
subject: string
items: [{ index, description, note, quantity, unit_price, discount, tax, total }]
payments_received: [{ label, amount }] // empty for an invoice; the paid amount for a receipt
notes: { customer_facing: string | none }
custom: { … } // your frontmatter params, keyed by name

Notes on the fields:

  • business.colours.primary is your brand colour as a hex string — feed it to rgb(...) for a themed accent, with a fallback for when it’s unset.
  • items[].total is the fully-computed per-line amount (after discount and tax); discount and tax are the absolute dollar components if you want a breakdown. Don’t recompute the maths yourself — the engine already agrees with the order UI and the Stripe invoice.
  • A receipt is the same script as an invoice. The only difference is that payments_received is populated, so the balance-due block resolves to zero. Render both from one template.
  • custom holds the values entered for your frontmatter params. A base field (business / client / items / document) can never be shadowed by a custom param — custom values always live under data.custom.

A client-scoped contract aggregates line items across possibly-many orders, so its shape differs in two places — read them carefully:

business: { name, address, email, website, abn, colours, payment }
document: { id, issued, title? } // `title` present only when it covers orders
client: { names: [string] } // a LIST of parties — not a single `name`
items: [{ index, description, quantity, unit_price, total }] // may be empty
notes: { customer_facing: string | none }
custom: { … }

The key trap: a contract has client.names (an array of party names), whereas an invoice has client.name (one string). Use data.client.names.join(", ").

Fields can be absent or none, so guard access with .at(key, default: …) and in checks rather than assuming presence:

#let theme = if "colours" in data.business and data.business.colours != none and data.business.colours.primary != none {
rgb(data.business.colours.primary)
} else {
rgb("#2b6cb0")
}
#let schedule = data.custom.at("payment_schedule", default: ())
#for row in schedule [ #row.label — #row.due_date #linebreak() ]

You don’t need a real order to build a template. The sample-data endpoint returns exactly the shaped data structure described above, over canned fixtures:

GET /accounts/:accountId/documents/sample-data?kind=order&type=invoice
GET /accounts/:accountId/documents/sample-data?kind=order&type=receipt
GET /accounts/:accountId/documents/sample-data?kind=client&type=contract

The fixtures deliberately exercise the awkward corners a template must handle — multiple line items, per-line discount and tax, a muted note under a description, a due date, customer-facing notes, and a non-USD currency — so you’re not just testing a trivial happy path. The sample business is the fictional Fabrikam Florals (a .example domain), never a real tenant’s data; the endpoint executes nothing and spends no quota.

Drop that JSON into a local file and compile against it to iterate on layout:

Terminal window
typst compile template.typ out.pdf --input data="$(cat sample.json)"

The starter templates import their common helpers rather than each carrying a copy:

#import "/.fabricadabra/doc/0.1.0.typ": doc-data, custom-param, doc-theme, fmt-money, fmt-address
Helper Does
doc-data(placeholder: …) Reads the payload, falling back to your skeleton when there is none — so the template still runs in the editor
custom-param(data, name, default: …) Reads one of your frontmatter params from either route: nested under data.custom during generation, or a top-level input on a plain Run
doc-theme(data, fallback: …) The business’s brand colour, with a fallback
fmt-money, fmt-address Formatting only — no rescaling, and absent address parts are skipped

The platform injects this file into every server-side compile, so it is not in your project and there is nothing extra in your file tree. That does mean a typst compile on your own machine can’t see it — fetch it first, into the path the import expects:

Terminal window
mkdir -p .fabricadabra/doc
curl -o .fabricadabra/doc/0.1.0.typ https://<your-host>/api/reference/typst-library/0.1.0

GET /api/reference/typst-library lists the published versions.

Versions are pinned and never change. A template importing 0.1.0.typ keeps rendering exactly as it does today; a later platform release adds a new file beside it rather than altering what you have already sent to a client. Moving to a newer version is you editing that one import line.

You are not obliged to use any of it — a template that defines its own helpers works exactly as well, and drops the import.

  • The Invoice and Contract examples are complete, working templates built on exactly this convention — start from one and adapt.
  • Parameters and inputs covers how your custom data.custom.* fields are declared in frontmatter.