@farthershore/business
v5.3.0
Published
Farther Shore Business-as-Code SDK — declare your software business in TypeScript
Maintainers
Readme
@farthershore/business
Status: 5.3.0
Declare a business in TypeScript with immutable, authentic references. The SDK compiles declarations into deterministic Manifest IR. Billing commerce uses schemaVersion 2: plans compose offerings, usage pricing, funding, and an optional recurring fee. The package version is managed separately by the release process.
pnpm add @farthershore/businessimport * as fs from "@farthershore/business";
const usd = fs.usageUnit.usd();
const requests = fs.requests();
const input = fs.measure("input_tokens");
const output = fs.measure("output_tokens");
const tokens = fs.meter("tokens", { measures: [input, output] });
const chat = fs.offering("chat", {
meter: tokens,
work: {
kind: "operation",
delivered: [input, output],
outcome: "per_unit",
lifetimeMs: 120_000,
actor: "personal",
cost: "reported",
unit: usd,
},
});
const pricing = fs.pricing("model_pricing", {
offering: chat,
disclosure: fs.disclosure.transparent,
source: { kind: "free" },
});
const pack = fs.topUp("add_10_dollars", {
price: fs.money.usd("10"),
grants: fs.credits(usd, "10"),
usage: [chat],
});
const product = fs.plan("pay_as_you_go", {
grants: [chat],
usagePricing: [pricing],
funding: { topUps: [pack] },
limits: [requests.perMinute(600)],
});
const api = fs.backend("api");
const route = fs.route("POST /v1/chat", { offering: chat, backend: api });
fs.meterRoutes("chat_usage", route, { reports: [tokens] });
export default fs.business({
billing: { mode: "credits_only", usageUnit: usd },
plans: [product],
});Declare the usage unit once: usageUnit.usd() or
usageUnit.credits("ai_credits"). A credit unit may declare the name customers
see, usageUnit.credits("ai_credits", { display: "AI credits" }); the key stays
the unit, so changing only display never changes a balance. Single costs, grants and top-ups accept at most nine whole and nine fractional digits
(maximum 999999999.999999999), keeping each stored amount within int8 sub-units.
Rates and grants use exact decimal amounts
created with credits(unit, "100"); money.usd is reserved for plan and top-up
prices. Numeric money inputs must be safe integers; decimal prices use strings.
A plan grants offerings and/or frontend integration refs, in any order. Frontend integration grants provide access only: they have no usage, charges, pricing, funding, or membership requirement. Each granted metered offering needs exactly one pricing
family. A family uses a catalog with timing: "billing_period", or explicit
source: { kind: "free" }. Free families have no timing. Usage paid call by
call from a balance, stopping at zero, is the offering's work.cost: declared
(cost: credits(unit, "2")) or reported (cost: "reported", unit, with the
backend reporting unitsConsumed). The platform never derives a call's cost
from catalog rates; an admission-timed catalog is refused
(ADMISSION_PRICING_REQUIRES_OFFERING_COST). A costed offering needs
included grants or top-ups, and its family is free: a plan that also prices it
with a billing_period catalog would charge it twice and is refused
(OFFERING_PRICED_TWICE). rate.graduated is the tiered pricing form.
billing.mode is explicit: periodic allows recurring fees and billing-period
usage, while credits_only has no recurring fee, billing-period usage, trial,
or membership-period grants. An omitted price means no fee. Whether a plan has
a billing period is decided per plan: a Free plan is monthly in either mode, and
a no-fee plan whose usage is paid through per-call costs has none in either
mode.
lifecycle.trialDays defers the recurring fee and therefore requires one.
Use included(amount, { cadence: "membership_period", usage: [offering] })
for an allowance, or cadence once for a one-time grant. A top-up uses grants
and usage; purchased funds have fixed terms. There are no authored monetary
caps, reservation estimates, reporting-grace knobs, or overage policies.
Metering remains measure / dimension / meter → meterRoutes → pricing →
plan grants. Every route names its offering with route(path, { offering, backend }),
including routes for pure access. Use meterRoutes to attach metering and bounds;
the route offering must name the same commercial meter. Backends report measurements
through ctx.report.
Work requires lifetimeMs between 1 and 21,600,000, inclusive.
Declare all components before default-exporting fs.business(). It seals the
registry. Forged references, cross-registry references, duplicate stable keys,
and late declarations reject. The same unchanged program lowers and hashes
identically across runs. canonicalIrJson and hashIr expose that wire boundary.
ManifestIssue reports code, dotted path, message, and correction.
Complete programs and canonical fixture artifacts live under
docs/superpowers/specs/fixtures/billing-components-golden/ in the monorepo.
Pass plans: [yourPlan] to business() to check literal plan, pricing, and usage-unit compatibility in TypeScript. Dynamic configuration and plans discovered through the registry receive the same checks at runtime.
