Recommended patterns·/docs/versioned-contracts

Versioned contracts at the edge

Your backend is probably Go, TypeScript, Python, Rust, or Rails, and it speaks JSON. Every response your frontend decodes has a shape, and sooner or later that shape changes.

For Lakeport apps, keep every response shape the backend may send in a single union in src/api/, and deploy the frontend first.

Why not /v1 and /v2 URLs?

Standard API advice is to version your URLs: keep /v1 additive, put breaking changes behind /v2, and run both until every client moves over. That works when a human checks every field change for compatibility.

We don't recommend that model for most Lakeport apps. Coding agents make breaking schema changes freely: ask for currency support and you may get a renamed field and a new nested object in the same diff. Small teams usually optimise for moving fast, and when an agent writes most of the code, nobody is checking that every /v1 change stayed additive and only /v2 breaks.

So express every forward version in your app instead, and release the app first. Match on the union without wildcard arms; Lean then rejects any match that misses a shape. It does not decide which screens need the union, or check that a decoder agrees with your API.

1. Model the shapes in src/api/

Suppose your backend is changing GET /api/orders/:id from a flat object (total_cents, status) to a nested object (amount.cents, amount.currency, refundable).

Keep the existing decoder and ask your agent to add the new shape beside it in one inductive type, decoded with LakeportJson:

leansrc/api/Orders.lean
import LakeportJson.Codec
open LakeportJson
structure OrderV1 where
id : String
totalCents : Nat
status : String
deriving DecidableEq, Repr
structure OrderV2 where
id : String
cents : Nat
currency : String
refundable : Bool
deriving DecidableEq, Repr
inductive OrderSchema where
| v1 (order : OrderV1)
| v2 (order : OrderV2)
deriving DecidableEq, Repr
def decodeOrderSchema (rawJson : String) : Option OrderSchema :=
match Json.parse rawJson with
| .error _ => none
| .ok j =>
match (do
let id ← j.fieldAs String "id"
let amt ← j.field "amount"
let cents ← amt.fieldAs Nat "cents"
let currency ← amt.fieldAs String "currency"
let refundable ← j.fieldAs Bool "refundable"
pure (OrderSchema.v2 { id, cents, currency, refundable })) with
| .ok v2Order => some v2Order
| .error _ =>
match (do
let id ← j.fieldAs String "id"
let totalCents ← j.fieldAs Nat "total_cents"
let status ← j.fieldAs String "status"
pure (OrderSchema.v1 { id, totalCents, status })) with
| .ok v1Order => some v1Order
| .error _ => none

2. Handle every case

In your reducers and .lp views, match on OrderSchema without a wildcard _ => arm. When you add a third shape later, every function that missed it fails compilation, and lakeport mcp gives your agent the exact file and line list.

This formatting sketch omits fractional cents; use a reviewed currency formatter in an app.

leanExhaustive handling in your frontend
def formatOrderSummary : OrderSchema → String
| .v1 o => "Order " ++ o.id ++ ": $" ++ toString (o.totalCents / 100) ++ " (" ++ o.status ++ ")"
| .v2 o => "Order " ++ o.id ++ ": $" ++ toString (o.cents / 100) ++ " " ++ o.currency

Here is how decodeOrderSchema and formatOrderSummary classify three responses from GET /api/orders/ord-42:

Backend response JSON
Matched branch
Rendered summary
{"id":"ord-42","total_cents":4900,"status":"paid"}
some (.v1 ...)
Order ord-42: $49 (paid)
{"id":"ord-42","amount":{"cents":4900,"currency":"USD"},"refundable":true}
some (.v2 ...)
Order ord-42: $49 USD
{"id":"ord-42","total":"$49"}
none
Decode error branch; no partial state committed

3. Release the frontend first

1.
Add OrderSchema.v2 beside OrderSchema.v1 in src/api/Orders.lean and run lakeport check so every screen handles both.
2.
Deploy the frontend while your backend still returns v1.
3.
Deploy the backend. During a rolling deploy, replicas may return v1 on one request and v2 on the next. The new frontend decodes both, and keeps working if you roll the backend back to v1.
4.
Delete OrderSchema.v1 once the backend can no longer send v1, including after a rollback. Remove obsolete handlers and run lakeport check . again; review the diff for code that no longer has callers.

Do backends need to advertise their version?

For reads, no. If a response decodes as OrderSchema.v2, your frontend already has the parsed v2 record.

For new writes, advertising capability helps. Before showing a new Request instant refund button (POST /api/orders/:id/refund), the frontend needs to know the backend accepts that route. A response header such as X-Capabilities: instant-refund, or a boolean field in the JSON body, is enough. Read it from EffectResp.httpResponse, store it in your model, and check it in your reducer as well as hiding the button. shows that reducer check.

Services you own

The same rule extends across multiple services you own if their dependency graph is a DAG. Order the graph from left to right, with your frontend on the far left and services that depend on no other owned service on the far right. Release from left to right so each consumer ships support for the new shape before any service it calls starts sending it.

If the dependency graph has a cycle, no such release order exists. Break the cycle first.

Where this stops working

In a larger company, independent teams may own services you depend on and deploy on their own schedules. That boundary behaves like an external product, similar to calling Stripe's API. The app must follow that product’s compatibility and versioning contract.

In our view that isn't the situation for most Lakeport users and most apps, which is why frontend-first unions are the default we recommend. Use them for response shapes whose rollout you control, and follow the published contract of anything you don't.

What the union does not cover

A versioned union lets the new frontend you deploy decode every response shape your backend may send during its rollout and after a rollback. It does nothing for frontends already shipped before your release, which only know the shapes compiled into them. Whether your backend keeps serving older shapes for those clients is a backend decision; Lakeport makes no recommendation there.

Continue to for rolling deploys, write gating in reducers, stored client data, and asset caching.