The Problem
Every time MACHBANK’s SDK needed a UI change — a new step in a payment flow, a reordered screen, different button copy — the change required a full iOS/Android/Web release cycle. App store reviews, version fragmentation, and adoption lag meant even trivial product experiments took 4–6 weeks to reach users.
The root cause was architectural: business logic lived in mobile clients. If the server decided what data to show and clients decided how to render it, changing the “what” required changing the clients.
The fix was to invert the model: the server sends a description of the complete UI, and clients are generic renderers that never change for business reasons.
Phase 1 (2025): Two Layers
The first version of the framework was split into two published npm packages, each with a distinct responsibility.
Layer 1: The Component Library
This is the TypeScript library that BFF services use to compose UI responses. It defines the full vocabulary of MACHBANK’s UI: every component, layout, action, and interaction, as strongly-typed factory functions that produce plain JSON objects.
Component system
The library defines 26 UI components organized as molecules and organisms — Button, Alert, CardDetailBox, ListItemSelect, BottomSheetDetail, ReceiptHeader, and more. Every component is created via a factory function, never a class instance:
// A button with a remote navigation action
Components.Button({
text: 'Confirm payment',
class: ButtonClassName.Primary,
action: Actions.NavigateRemote({
source: 'POST_/payments/confirm',
flowName: 'paymentFlow',
loader: Loader({ type: LoaderType.Spinner }),
onComplete: Actions.NavigateLocal({ flowName: 'receiptFlow' }),
onError: Actions.RetryFlow(),
}),
})
Factory functions return plain objects — fully JSON-serializable, with no prototype chain or class overhead. Clients don’t need to know how components were created; they just render what they receive.
Layouts, sections, and screens
UI is composed in three layers:
- Sections (
ButtonSection,HeroSection,DetailSection, etc.) group related components into named containers with auto-generated UUIDs - Layouts (8 types:
LayoutComplete,LayoutWithoutNav,LayoutBottomSheetComplete, etc.) slot sections intonav,main, andfooterplacements - Screens hold a layout plus optional navigation behavior and analytics events
Actions and chaining
13 action types handle every user interaction: NavigateRemote, NavigateLocal, CallableRemote, OpenModal, OpenBottomSheet, UpdateContext, Close, RetryFlow, and more. Actions are composable — any action can chain to a next action on completion or error:
Actions.CallableRemote({
source: 'POST_/payments/authorize',
data: { amount: stateRefs.amount },
onComplete: Actions.NavigateRemote({ source: 'GET_/payments/receipt', ... }),
onError: Actions.OpenBottomSheet({ flowName: 'errorSheet' }),
})
State binding
Dynamic content is bound through state refs — typed string literals that clients interpolate at render time:
const { state, stateRefs } = State({ amount: 15000, currency: 'CLP' })
Components.Button({ text: `Pay ${stateRefs.amount} ${stateRefs.currency}` })
// Serialized → { text: "Pay {state.amount} {state.currency}" }
// Client renders → "Pay 15000 CLP"
The IStateRef type is defined as `{state.${string}}`, so TypeScript enforces valid state references at compile time.
Build-time validation
The build() function is the final step before serialization. It:
- Auto-extracts all sections from layout placements — no manual
sectionsarray - Asserts all section IDs are unique (throws on collision)
- Cross-validates that every referenced section ID exists, and every defined section is used
This means malformed SDUI responses are a compile + runtime error caught locally, never a client-side crash in production.
Layer 2: The BFF SDK
This is the Fastify-based SDK that BFF services use to serve SDUI responses. It handles the protocol layer: version negotiation, request parsing, schema validation, trace propagation, and analytics.
Header-based protocol versioning
Clients send X-SDUI-Protocol-Min and X-SDUI-Protocol-Max headers with each request. The SDK negotiates the richest supported version within that range:
Client sends: X-SDUI-Protocol-Min: 1.0 / X-SDUI-Protocol-Max: 2.0
Server supports: ['1.0', '2.0', '3.0']
Negotiated version: 2.0 (highest in range)
If no version matches, the SDK returns 406 Not Acceptable. This decouples mobile app versions from backend deployments — old clients continue receiving their supported protocol version while new features ship to newer clients, without any routing changes.
Type-safe route definition
Routes are declared explicitly via defineRoutes, which enforces the METHOD_/path naming convention at compile time:
const routes = defineRoutes({
'POST_/payments/create': {
handler: { '1.0': createPaymentPresenter },
schemas: { input: { body: paymentSchema } },
},
'GET_/payments/receipt': {
handler: { '1.0': receiptPresenter },
schemas: { input: {} },
},
})
The type system ensures handlers declare which protocol versions they support ({ '1.0': fn }), making it impossible to accidentally omit a version. Path extraction is also type-safe — routes.path('GET_/payments/receipt') produces the correct string and errors at compile time on any typo.
Automatic common fields
Every endpoint automatically inherits documentNumber (Chilean RUT), channelCode, machId, and platform in its query schema. Consumer services don’t redeclare them — the SDK merges them, with consumer-defined properties taking precedence on conflict.
Phase 2 (2026): From a Library to a Platform
Success created the next problem. By 2026 the SDUI contract lived in four independent implementations — the backend library, the Android renderer, the iOS renderer, and a web playground. Every new component meant four hand-written copies, coordinated in meetings. Drift was inevitable, and it surfaced as crashes in production apps.
The team consolidated everything into a single monorepo: one contract, written in TypeScript, that generates JSON Schema, Kotlin, and Swift, plus one kit per platform — a server kit for TypeScript backends and client kits for React, Angular, Android (Compose), and iOS (SwiftUI). All client kits run the same cross-platform YAML scenarios, so “renders the same everywhere” is a test, not a hope. The platform went from 1.0 to 2.7 between July and October 2026 — 19 releases. I was one of its core contributors; my part:
Migrating production services onto the new kit — and the incident that shaped 2.0
The first migration of a payment BFF to the server kit was merged and reverted the same day: older Android builds crash-rendered any component carrying a renamed styling field the kit had started emitting. Rather than keep a dual-track workaround, I traced it to the root cause and drove the fix into the contract itself — a single styling-variant field, the legacy fallbacks deleted across every kit, and a lockstep 2.0.0 major release, recorded as a numbered design decision.
Before cutting over, I ran the legacy and kit-based implementations of all four payment screens (create, authorize, execute, receipt) against identical inputs and deep-compared their output in a parity suite. Only once they were proven structurally identical did the legacy library get removed. The remaining backend services followed.
A 93% smaller generated contract
Every generated schema file inlined the entire type graph, so the generated output was 64% of the repo’s lines — and adding one component touched 32 files and 6,000+ lines of diff. I restructured generation so each schema keeps only its own root and references a shared definitions file:
| Files | Lines | |
|---|---|---|
| Before | 228 | 315,297 |
| After | 231 | 21,009 |
A new component’s PR went from +6,132 lines across 32 files to +192 across 3. Because it was a generator change, I verified it was semantically neutral three independent ways: every non-schema artifact (Kotlin, Swift, manifests) byte-identical, all goldens and mutation-fuzzing baselines reproduced exactly, and a schema diff against the last published release reporting zero breaking changes.
Accessibility as part of the contract
I added a backend-authored accessibility description to every component (24 that had none), with one shared resolution rule implemented in all three client engines — TypeScript, Kotlin, and Swift — and pinned by a 19-case cross-platform golden corpus. The rule refuses to apply an override that would hide a nested interactive control from a screen reader. I paired it with VoiceOver fixes in the iOS app so that chips announce their selection state and sections announce their group label.
BFF SDK 3.x
- Replaced the hand-rolled JSON Schema builder with Zod, chosen over TypeBox because Zod was already used directly in four other services across the org.
- Removed the min/max protocol headers from Phase 1. Version negotiation now lives in the platform’s contract version, so the SDK no longer duplicated it.
- Added versioned routes on Fastify’s native router constraints: each version gets its own route, schema, validation log, and docs entry. Range matching no longer depends on the order the versions are declared in.
- Fixed a trace-correlation bug where the request ID was stripped before it reached the trace context, which had broken gateway correlation on every route.
Impact
The framework eliminated mobile release cycles for all UI-driven features in the MACHBANK SDK. A UI change that previously required a 4–6 week mobile release cycle now ships with a backend deployment.
The project won MACHBANK’s EPIC Project Award and the ‘Achieving Ambitious Goals’ award.
In 2026 the contract stopped being a convention four teams kept in sync and became a single versioned artifact. Adding a component is now a small, reviewable PR, and accessibility is part of the contract.