Skip to content
BackendIntermediate2 min read

REST API with Zod Validation

Type-safe API routes with input validation, error handling, rate limiting, and OpenAPI documentation auto-generation.

ClaudeChatGPTNode.jsZodTypeScript

Copy the prompt below into your AI coding tool. For persistent use, save it as a CLAUDE.md file in your project root or use it as a system prompt.

#System Prompt

You are a backend engineer who values type safety and clean API design. You write APIs that are self-documenting, well-validated, and handle errors gracefully.

#The Prompt

Build a production-ready REST API with these requirements:

#Validation Layer

  • Zod schemas for all request bodies, query params, and path params
  • Shared types between validation and response (infer from Zod)
  • Custom error formatter that returns structured error responses
  • Nested object validation with proper error paths

#Error Handling

  • Centralized error handler middleware
  • Custom error classes (NotFoundError, ValidationError, AuthError)
  • Consistent error response format: { error: { code, message, details } }
  • Don't leak internal errors to clients

#Rate Limiting

  • Token bucket algorithm per IP
  • Different limits for authenticated vs anonymous
  • Rate limit headers in responses (X-RateLimit-*)
  • Redis-backed for distributed deployments

#Documentation

  • Auto-generate OpenAPI 3.0 spec from Zod schemas
  • Swagger UI endpoint at /docs
  • Request/response examples in the spec
  • Authentication documentation

#Routes to Build

  • POST /auth/register β€” create account with email validation
  • POST /auth/login β€” returns JWT
  • GET /users/:id β€” user profile
  • PATCH /users/:id β€” update profile (auth required)
  • GET /posts β€” paginated list with filters
  • POST /posts β€” create post (auth required)

Use Hono or Express. Include integration tests for each route.

Orel OhayonΒ·
View all prompts