DEV Community

Cover image for Stop Writing OpenAPI Specs by Hand
Jbee - codehooks.io
Jbee - codehooks.io

Posted on

Stop Writing OpenAPI Specs by Hand

Writing API documentation sucks. You build an endpoint, then spend 20 minutes writing YAML that's outdated by your next commit.

What if your docs wrote themselves?

The Problem

OpenAPI/Swagger is great for API consumers. But maintaining specs manually means:

  • Duplicating schema definitions
  • Docs drifting out of sync with code
  • Tedious YAML editing for every endpoint

The Solution: Generate from Code

With Codehooks.io, define your schema once and get full OpenAPI 3.0 docs automatically:

import { app } from 'codehooks-js';
import { z } from 'zod';

const todoSchema = z.object({
  title: z.string().min(1).max(200),
  completed: z.boolean().default(false),
  priority: z.enum(['low', 'medium', 'high'])
});

// Enable OpenAPI + Swagger UI
app.openapi({
  info: { title: 'Todo API', version: '1.0.0' }
});

// Auto-generate CRUD endpoints
app.crudlify({ todos: todoSchema });

export default app.init();
Enter fullscreen mode Exit fullscreen mode

Deploy, then visit /docs — you get a fully interactive Swagger UI with all 8 CRUD endpoints documented.

Custom Routes? No Problem

For endpoints beyond CRUD, use the openapi() middleware:

import { app, openapi } from 'codehooks-js';

app.get('/stats',
  openapi({
    summary: 'Get statistics',
    tags: ['Analytics'],
    responses: {
      200: { description: 'Stats retrieved' }
    }
  }),
  async (req, res) => {
    res.json({ total: 42 });
  }
);
Enter fullscreen mode Exit fullscreen mode

You can even pass Zod schemas directly — they auto-convert to JSON Schema:

app.post('/users',
  openapi({
    summary: 'Create user',
    requestBody: {
      content: {
        'application/json': { schema: UserSchema }
      }
    }
  }),
  handler
);
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Zod, Yup, JSON Schema — all supported
  • Swagger UI at /docs — with authentication support
  • Filter endpoints — hide internal routes from public docs
  • Zero duplication — one schema for validation AND documentation

Try It

npm install codehooks-js@latest
Enter fullscreen mode Exit fullscreen mode

Add app.openapi() before app.crudlify(), deploy with coho deploy, and your docs are live.

No more YAML. No more drift. Just code.


Full docs: codehooks.io/docs/openapi-swagger-docs


What's your current approach to API documentation? Do you maintain OpenAPI specs manually or generate them? Let me know in the comments!

Top comments (1)

Collapse
 
canuto profile image
Knut Martin Tornes

Useful!