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 (0)