DEV Community

Cover image for I Found a Better Way to Build APIs (And It Actually Makes Sense)
AdarshGzz...
AdarshGzz...

Posted on

I Found a Better Way to Build APIs (And It Actually Makes Sense)

so i was just reading some posts on dev.to and came across this idea called contract-driven development

looked interesting… so sharing what i understood

the idea (simple)

instead of writing backend first

you define your api structure first

like:
• endpoints
• request
• response

basically a clear blueprint

then you build backend based on that

what is this “contract”

it’s just like a fixed structure of your api

people usually use OpenAPI

it basically creates a JSON file of your API which can be used for:
• docs
• frontend usage
• testing

setup (what i found people are using)

this is the stack i saw most commonly:

install packages

npm install express zod @trpc/server @trpc/server/adapters/express trpc-openapi

if using typescript:

npm install -D typescript ts-node @types/node @types/express

docs (if you want to explore more)
• Express → https://expressjs.com/
• Zod → https://zod.dev/
• tRPC → https://trpc.io/
• tRPC OpenAPI → https://github.com/trpc/trpc-openapi
• OpenAPI → https://swagger.io/specification/

something i liked

they use Zod for validation

so instead of writing checks again and again

you define schema once and it validates automatically

best part

docs are auto generated 🤯

like you don’t manually write docs

it creates something like openapi.json

and you can plug it into tools like Swagger

why this feels useful
• clear structure from start
• frontend + backend stay in sync
• no outdated docs
• less confusion

thinking to try this

haven’t implemented fully yet

but feels like a clean way to build things

if anyone using this

is it actually good in real projects?

or gets complicated later?

would like to know 👍

Top comments (4)

Collapse
 
apibuilderhq profile image
APIBuilderHQ

Contract-driven development clicked for me when I started building an API client tool — once you define the contract first, testing becomes so much cleaner because you know exactly what shape the response should be.

On your question about whether it gets complicated in real projects — the honest answer is the contract discipline pays off most when you have multiple consumers of the same API (frontend, mobile, third parties). For solo projects it can feel like overhead at first. But the auto-generated docs alone are worth it — I've spent way too long reading outdated documentation for APIs that didn't match what was actually being returned.

tRPC + Zod is a solid combo. The type safety flowing end-to-end is the part that actually changes how you debug.

Collapse
 
adarshgzz profile image
AdarshGzz... • Edited

yeah this actually makes sense now 😅

i was thinking it might be too much for small projects
but what you said about multiple consumers is true

like if same api is used by frontend + mobile
then having a clear contract from start sounds really helpful

also +1 on outdated docs 😂
faced this many times… docs say something else, api returns something else

tRPC + zod combo looks interesting mainly because of type safety
feels like it can reduce silly bugs

Using it in my current project 👍

Collapse
 
apibuilderhq profile image
APIBuilderHQ

tRPC + Zod is worth the setup cost once you have that frontend + mobile scenario. The end-to-end type safety catches the exact mismatches you're describing — where the docs say one thing and the API returns another. Good luck with the project.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.