Dev.to
6/28/2026

Building a Type-Safe API Layer in Next.js App Router With Zod and Server Actions
Short summary
Pattern for building type-safe Next.js Server Actions with Zod validation and discriminated unions. Wraps all results in ActionResult<T> (success | error), validates input at runtime despite TypeScript, and handles field errors. Ensures type safety and consistent error handling across the client-server boundary.
- •Use discriminated union pattern (ActionResult<T>) for all Server Action returns
- •Validate input with Zod at runtime even with TypeScript type definitions
- •Integrate field-level errors with React Hook Form for consistent client UX
Generated with AI, which can make mistakes.
Is this a good recommendation for you?



