Route Handler Pattern

Type-safe REST API endpoints using Next.js App Router route handlers with Zod validation, authentication, and structured error handling.

What's Included#

  • Basic CRUD route handlers (GET, POST, PATCH, DELETE) in the app/api directory
  • Dynamic route handlers with parameter extraction
  • Zod schema validation for request bodies
  • Authentication checks before processing requests
  • Error handling wrapper that catches Zod, auth, and unknown errors
  • Proper HTTP status codes and JSON response formatting

Usage#

Via CLI#

Loading code block...

Via AI Assistant#

Ask your AI assistant:

  • "Use the route handler pattern from Bootspring"
  • "Apply the Bootspring route handler pattern to my project"

Key Considerations#

  • Always validate input with Zod or a similar library for both request bodies and query parameters
  • Return consistent error responses with appropriate HTTP status codes
  • Check authentication at the beginning of every protected route handler
  • Keep handlers focused on a single responsibility
  • Log server errors for debugging while returning generic messages to clients
  • Consider adding rate limiting to protect endpoints from abuse