TypeScript reference consumer for the AI Software Factory Contract.
A real Fastify API demonstrating the factory's core promise: an engineering stage that passes its local tests can still be blocked from merging by an independently owned acceptance check.
Live demo: PR #2 - attempt 1 (required param on existing endpoint) →
factory-contract/acceptance: FAILURE❌ · attempt 2 (compatible new route) →factory-contract/acceptance: SUCCESS✅
The demo · Quick start · API reference · Tests · Factory contract · Contributing
| Repo | Role |
|---|---|
ai-software-factory-contract |
Factory engine - runs WorkOrders through 5 stages |
ai-factory-trusted-verifier |
Posts factory-contract/acceptance check - not the factory, not CI |
| This repo | TypeScript billing API - the factory's primary reference consumer |
factory-reference-notification-api |
Python notification API - same factory release, different domain |
The reference WorkOrder adds GET /v1/invoices/export - a tenant-scoped CSV export.
The compatibility constraint: GET /v1/invoices already exists and has clients. Any PR that changes its parameters is a breaking change.
What fails: A PR that adds tenant_id as a required query parameter to the existing GET /v1/invoices endpoint.
oasdiff breaking baseline.yaml proposed.yaml
→ 1 breaking change: required request parameter added: 'tenant_id' in query
The trusted verifier posts factory-contract/acceptance: failure on that commit. Branch protection requires the check from the verifier App. The PR cannot merge.
What passes: The corrected PR adds only the new GET /v1/invoices/export route. No changes to the existing endpoint.
oasdiff breaking baseline.yaml proposed.yaml
→ 0 breaking changes
The verifier posts factory-contract/acceptance: success. The PR merges.
Prerequisites: Node.js ≥ 22, pnpm ≥ 9, Docker (for the database)
git clone https://github.com/Darshitpandya/factory-reference-billing-api.git
cd factory-reference-billing-api
pnpm installdocker compose -f deploy/docker-compose.yaml up -d billing-db
# PostgreSQL on :5432; schema + test fixtures loaded automatically# No .env needed for unit tests
# For integration tests and running the server:
export DATABASE_URL=postgresql://billing:billing@localhost:5432/billing
export JWT_SECRET=local-dev-secret-min-256-bitspnpm test
# 16 tests: invoices route, CSV export, tenant isolationpnpm start
# Fastify on :3000All endpoints require a valid JWT with tenantId and sub claims. Tenant identity is derived from JWT claims only - never from query parameters.
Returns paginated invoices for the authenticated tenant.
This endpoint is the compatibility baseline. Do not add required parameters.
GET /v1/invoices
GET /v1/invoices?page=2&pageSize=10
GET /v1/invoices?status=paid
| Parameter | Type | Required | Default |
|---|---|---|---|
page |
integer | No | 1 |
pageSize |
integer (max 100) | No | 20 |
status |
enum | No | - |
Response:
{
"invoices": [...],
"total": 42,
"page": 1,
"pageSize": 20
}Returns tenant-scoped invoices as a CSV file. Max 10,000 rows per request.
GET /v1/invoices/export
GET /v1/invoices/export?limit=500&status=paid&fromDate=2026-01-01&toDate=2026-06-30
| Parameter | Type | Required | Default |
|---|---|---|---|
limit |
integer (max 10000) | No | 10000 |
status |
enum | No | - |
fromDate |
date string | No | - |
toDate |
date string | No | - |
Response: text/csv with headers invoice_id, invoice_number, customer_id, amount_cents, currency, status, issued_at, due_date
CSV cells starting with =, +, -, or @ are prefixed with a tab (formula injection prevention).
No authentication required.
{"status": "ok", "db": "ok", "version": "1.0.0"}pnpm test # unit tests (no live database needed)
pnpm test:e2e # integration tests (requires running PostgreSQL)
pnpm typecheck
pnpm openapi:validate| File | What it covers |
|---|---|
test/invoices.test.ts |
401 without JWT, pagination params, invalid status |
test/export.test.ts |
CSV headers, formula injection prevention, empty list, row count |
test/tenant-isolation.test.ts |
SQL always includes WHERE tenant_id = $1; export limit enforced at EXPORT_MAX_ROWS |
Tenant isolation is a hard requirement. test/tenant-isolation.test.ts captures queries and asserts the SQL never issues a SELECT without a tenant filter. Any PR that weakens this will fail in CI.
This repository consumes factory release 1.4.0 and declares its acceptance requirements in .factory/repository-contract.yaml:
consumedRelease: "1.4.0"
acceptance:
requiredChecks:
- factory-contract/acceptance # from trusted verifier App - not from GitHub Actions
compatibilityBaselinePath: openapi/openapi.yaml
tenantIsolationRequired: trueThe OpenAPI baseline (openapi/openapi.yaml) is used by the verifier at the base SHA to detect breaking changes. It is not the same as openapi/compatibility-baseline.yaml (which is a local reference fixture only).
The reference WorkOrder is in .factory/work-orders/invoice-export.yaml. To submit it via Port:
- Go to
app.getport.io→ Catalog → Work Orders → Create - Set
targetServicetofactory-reference-billing-api - Set
releaseVersionto1.4.0 - Set
scopeto the invoice export description
- Tenant identity comes exclusively from validated JWT claims
- Cross-tenant queries are forbidden; every database query filters by
tenant_id = $1 - Raw invoice data is never logged
- Do not modify
.factory/repository-contract.yamlor the OpenAPI baseline in the same PR as code changes - See SECURITY.md
A provisioning script automates PostgreSQL setup and Railway deployment:
# Prerequisites: Railway CLI installed, logged in, project linked
railway login
./scripts/provision-railway-db.shThe script adds a PostgreSQL plugin, injects DATABASE_URL, and triggers a redeployment. See scripts/provision-railway-db.sh for details.
See CONTRIBUTING.md.
MIT - see LICENSE.

