{"canonical_url":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/","content_markdown":"# Generated the API contract from the database out\n\nGenerated the API contract outward from the database — OpenAPI, a 44,076‑line typed TypeScript client, 61 mock handlers and the limits the UI enforces — with a guard at every hop that fails on drift.\n\n2025\n\n**Situation.** Frontend and backend move at their own pace, and their assumptions about a request payload can drift apart without anyone noticing. The way you usually find out is a 422 in the browser — after the mismatch has already shipped, which is the most expensive moment to learn about it. Writing the two sides by hand from the same document does not fix it; it just moves the drift to whoever forgot to reread the document.\n\n**Task.** The two sides had to be generated from one source rather than agreed between two, with every step of the generation checked instead of trusted.\n\n**Action.** The chain starts at the database and runs outward. The schema and its functions define the shapes; the Go types define the API; Huma emits the OpenAPI description from those; a typed TypeScript client — 44,076 lines of it — is generated from that description; 61 mock handlers are generated alongside it so the frontend's own tests run against the real contract rather than a hand‑written fixture; and the limits the UI enforces on a form come from the same place instead of being retyped into a validator. Every hop has a guard. A contract check in CI compares what the frontend sends against what the API expects and fails the build on divergence, with a schema probe underneath it that checks the real shapes rather than a description of them. It deliberately covers where drift likes to hide: optional body fields, where \"missing\" and \"null\" get confused, and query‑parameter enums, where the two sides can quietly disagree on the allowed values.\n\n**Result.** A field cannot change on one side only — it fails at the first hop that notices, in a build, minutes after the change. That took out a recurring and genuinely annoying class of bug, the kind invisible in code review that only shows up at runtime. The cost is a generation step in the middle of everything: regenerating is a chore, and the chain is only as trustworthy as its least‑guarded link, which is why each hop got one.\n\n---\n\n- Role: Backend Engineer\n- Categories: [Backend Engineering](https://engineer.company/categories/backend/), [APIs \u0026 Integration](https://engineer.company/categories/api/), [DevOps](https://engineer.company/categories/devops/), [Automation \u0026 CI/CD](https://engineer.company/categories/automation/), [Reliability \u0026 Backups](https://engineer.company/categories/reliability/), [Testing \u0026 QA](https://engineer.company/categories/testing/)\n- Services: [Backend \u0026 API Development](https://engineer.company/services/backend-development/), [AI Agent Tools \u0026 MCP Servers](https://engineer.company/services/ai-tools/), [DevOps \u0026 CI/CD Automation](https://engineer.company/services/devops-cicd/)\n\n\u003chttps://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/\u003e\n","date_modified":"2026-10-21T16:38:05Z","description":"Generated the API contract from the database outward — OpenAPI, a typed TypeScript client, mock handlers and UI limits — with a guard at every hop.","formats":{"gemtext":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/index.gmi","gopher":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/gophermap","html":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/","json":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/index.json","markdown":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/index.md","pdf":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/index.pdf"},"kind":"page","language":"en","title":"Generated the API contract from the database out","url":"https://engineer.company/portfolio/generated-the-api-contract-from-the-database-out/","version":1}