{"id":6245,"date":"2026-06-28T14:35:28","date_gmt":"2026-06-28T07:35:28","guid":{"rendered":"https:\/\/daiilynews.cu.ma\/?p=6245"},"modified":"2026-06-28T14:35:28","modified_gmt":"2026-06-28T07:35:28","slug":"building-a-type-safe-api-layer-in-next-js-app-router-with-zod-and-server-actions","status":"publish","type":"post","link":"https:\/\/daiilynews.cu.ma\/?p=6245","title":{"rendered":"Building a Type-Safe API Layer in Next.js App Router With Zod and Server Actions"},"content":{"rendered":"<p> <br \/>\n<br \/>\n                Server Actions in Next.js App Router look deceptively simple \u2014 write an async function, mark it with &#8216;use server&#8217;, call it from a Client Component. The surface area is small.<\/p>\n<p>The problems surface when you start thinking about validation, error handling, and type safety across the client-server boundary. Without a deliberate approach, you end up with untyped form data on the server, error handling that varies across actions, and client code that can&#8217;t trust the shape of what comes back.<\/p>\n<p>Here&#8217;s the pattern I landed on for type-safe Server Actions with Zod validation and consistent error handling, from building the generation pipeline powering the free AI wallpaper maker at pixova.io.<\/p>\n<p>  The Problem With Naive Server Actions<\/p>\n<p>The simplest Server Action works fine for prototypes:<\/p>\n<p>&#8216;use server&#8217;;<\/p>\n<p>export async function submitForm(formData: FormData) {<br \/>\n  const prompt = formData.get(&#8216;prompt&#8217;) as string;<br \/>\n  \/\/ No validation, no type safety, any error handling is ad hoc<br \/>\n  const result = await generateImage(prompt);<br \/>\n  return result;<br \/>\n}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>The issues:<\/p>\n<p>formData.get(&#8216;prompt&#8217;) returns string | null | File \u2014 the as string cast hides a bug waiting to happen<br \/>\nNo validation means invalid input reaches your business logic<br \/>\nError handling is whatever you add ad hoc to each action<\/p>\n<p>  &#8211; The return type isn&#8217;t defined, so the client has no type information<\/p>\n<p>  The Foundation \u2014 A Result Type<\/p>\n<p>Start with a discriminated union for action results:<\/p>\n<p>\/\/ lib\/types\/action.ts<br \/>\nexport type ActionSuccessT> = {<br \/>\n  success: true;<br \/>\n  data: T;<br \/>\n};<\/p>\n<p>export type ActionError = {<br \/>\n  success: false;<br \/>\n  error: string;<br \/>\n  fieldErrors?: Recordstring, string()>;<br \/>\n};<\/p>\n<p>export type ActionResultT> = ActionSuccessT> | ActionError;<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Every Server Action returns Promise>. The client always knows whether the action succeeded and what shape the data has.<\/p>\n<p>  Adding Zod Validation<\/p>\n<p>\/\/ lib\/schemas\/generate.ts<br \/>\nimport { z } from &#8216;zod&#8217;;<\/p>\n<p>export const GenerateSchema = z.object({<br \/>\n  prompt: z<br \/>\n    .string()<br \/>\n    .min(3, &#8216;Prompt must be at least 3 characters&#8217;)<br \/>\n    .max(500, &#8216;Prompt must be under 500 characters&#8217;)<br \/>\n    .trim(),<br \/>\n  aspectRatio: z.enum((&#8216;1:1&#8242;, &#8217;16:9&#8217;, &#8216;9:16&#8217;, &#8216;4:5&#8217;)).default(&#8216;1:1&#8217;),<br \/>\n  style: z.string().optional(),<br \/>\n});<\/p>\n<p>export type GenerateInput = z.infertypeof GenerateSchema>;<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>  The Server Action With Full Type Safety<\/p>\n<p>\/\/ app\/actions\/generate.ts<br \/>\n&#8216;use server&#8217;;<\/p>\n<p>import { z } from &#8216;zod&#8217;;<br \/>\nimport { GenerateSchema, GenerateInput } from &#8216;@\/lib\/schemas\/generate&#8217;;<br \/>\nimport { ActionResult } from &#8216;@\/lib\/types\/action&#8217;;<\/p>\n<p>type GenerateResult = {<br \/>\n  jobId: string;<br \/>\n  estimatedSeconds: number;<br \/>\n};<\/p>\n<p>export async function generateImageAction(<br \/>\n  input: GenerateInput<br \/>\n): PromiseActionResultGenerateResult>> {<br \/>\n  \/\/ Validate \u2014 even though TypeScript already knows the type,<br \/>\n  \/\/ runtime validation catches anything that slips through<br \/>\n  const parsed = GenerateSchema.safeParse(input);<\/p>\n<p>  if (!parsed.success) {<br \/>\n    return {<br \/>\n      success: false,<br \/>\n      error: &#8216;Invalid input&#8217;,<br \/>\n      fieldErrors: parsed.error.flatten().fieldErrors as Recordstring, string()>,<br \/>\n    };<br \/>\n  }<\/p>\n<p>  try {<br \/>\n    const { prompt, aspectRatio, style } = parsed.data;<\/p>\n<p>    \/\/ Your business logic here<br \/>\n    const job = await submitGenerationJob({ prompt, aspectRatio, style });<\/p>\n<p>    return {<br \/>\n      success: true,<br \/>\n      data: {<br \/>\n        jobId: job.id,<br \/>\n        estimatedSeconds: job.estimatedDuration,<br \/>\n      },<br \/>\n    };<br \/>\n  } catch (error) {<br \/>\n    \/\/ Log server-side for debugging<br \/>\n    console.error(&#8216;Generation failed:&#8217;, error);<\/p>\n<p>    \/\/ Return user-friendly error to client<br \/>\n    return {<br \/>\n      success: false,<br \/>\n      error: &#8216;Generation failed. Please try again.&#8217;,<br \/>\n    };<br \/>\n  }<br \/>\n}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>  The Client-Side Hook<\/p>\n<p>\/\/ hooks\/useGenerate.ts<br \/>\n&#8216;use client&#8217;;<\/p>\n<p>import { useState, useTransition } from &#8216;react&#8217;;<br \/>\nimport { generateImageAction } from &#8216;@\/app\/actions\/generate&#8217;;<br \/>\nimport { GenerateInput } from &#8216;@\/lib\/schemas\/generate&#8217;;<\/p>\n<p>export function useGenerate() {<br \/>\n  const (isPending, startTransition) = useTransition();<br \/>\n  const (result, setResult) = useState{ jobId: string } | null>(null);<br \/>\n  const (error, setError) = useStatestring | null>(null);<\/p>\n<p>  const generate = (input: GenerateInput) => {<br \/>\n    setError(null);<br \/>\n    setResult(null);<\/p>\n<p>    startTransition(async () => {<br \/>\n      const response = await generateImageAction(input);<\/p>\n<p>      if (response.success) {<br \/>\n        setResult({ jobId: response.data.jobId });<br \/>\n      } else {<br \/>\n        setError(response.error);<br \/>\n      }<br \/>\n    });<br \/>\n  };<\/p>\n<p>  return { generate, isPending, result, error };<br \/>\n}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>  The Form Component<\/p>\n<p>\/\/ components\/GenerateForm.tsx<br \/>\n&#8216;use client&#8217;;<\/p>\n<p>import { useForm } from &#8216;react-hook-form&#8217;;<br \/>\nimport { zodResolver } from &#8216;@hookform\/resolvers\/zod&#8217;;<br \/>\nimport { GenerateSchema, GenerateInput } from &#8216;@\/lib\/schemas\/generate&#8217;;<br \/>\nimport { useGenerate } from &#8216;@\/hooks\/useGenerate&#8217;;<\/p>\n<p>export function GenerateForm() {<br \/>\n  const { generate, isPending, error } = useGenerate();<\/p>\n<p>  const { register, handleSubmit, formState: { errors } } = useFormGenerateInput>({<br \/>\n    resolver: zodResolver(GenerateSchema),<br \/>\n    defaultValues: {<br \/>\n      aspectRatio: &#8216;1:1&#8217;,<br \/>\n    },<br \/>\n  });<\/p>\n<p>  return (<br \/>\n    form onSubmit={handleSubmit(generate)} className=&#8221;flex flex-col gap-4&#8243;><br \/>\n      div><br \/>\n        textarea<br \/>\n          {&#8230;register(&#8216;prompt&#8217;)}<br \/>\n          placeholder=&#8221;Describe what you want to generate&#8230;&#8221;<br \/>\n          className=&#8221;w-full p-3 rounded-xl border border-border bg-card<br \/>\n            text-foreground resize-none h-24 focus:outline-none<br \/>\n            focus:ring-2 focus:ring-orange-500&#8243;<br \/>\n        \/><br \/>\n        {errors.prompt &#038;&#038; (<br \/>\n          p className=&#8221;text-sm text-red-500 mt-1&#8243;>{errors.prompt.message}p><br \/>\n        )}<br \/>\n      div><\/p>\n<p>      select<br \/>\n        {&#8230;register(&#8216;aspectRatio&#8217;)}<br \/>\n        className=&#8221;p-2 rounded-lg border border-border bg-card text-foreground&#8221;<br \/>\n      ><br \/>\n        option value=&#8221;1:1&#8243;>Square (1:1)option><br \/>\n        option value=&#8221;16:9&#8243;>Landscape (16:9)option><br \/>\n        option value=&#8221;9:16&#8243;>Portrait (9:16)option><br \/>\n        option value=&#8221;4:5&#8243;>Instagram (4:5)option><br \/>\n      select><\/p>\n<p>      {error &#038;&#038; (<br \/>\n        p className=&#8221;text-sm text-red-500&#8243;>{error}p><br \/>\n      )}<\/p>\n<p>      button<br \/>\n        type=&#8221;submit&#8221;<br \/>\n        disabled={isPending}<br \/>\n        className=&#8221;px-6 py-3 bg-orange-500 text-white rounded-full<br \/>\n          font-medium hover:bg-orange-600 transition-colors<br \/>\n          disabled:opacity-50 disabled:cursor-not-allowed&#8221;<br \/>\n      ><br \/>\n        {isPending ? &#8216;Generating&#8230;&#8217; : &#8216;Generate&#8217;}<br \/>\n      button><br \/>\n    form><br \/>\n  );<br \/>\n}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>  A Reusable Action Wrapper<\/p>\n<p>For larger applications with many actions, a wrapper reduces boilerplate:<\/p>\n<p>\/\/ lib\/action-wrapper.ts<br \/>\nimport { z } from &#8216;zod&#8217;;<br \/>\nimport { ActionResult } from &#8216;.\/types\/action&#8217;;<\/p>\n<p>export function createActionTInput, TOutput>(<br \/>\n  schema: z.ZodSchemaTInput>,<br \/>\n  handler: (input: TInput) => PromiseTOutput><br \/>\n) {<br \/>\n  return async (input: unknown): PromiseActionResultTOutput>> => {<br \/>\n    const parsed = schema.safeParse(input);<\/p>\n<p>    if (!parsed.success) {<br \/>\n      return {<br \/>\n        success: false,<br \/>\n        error: &#8216;Validation failed&#8217;,<br \/>\n        fieldErrors: parsed.error.flatten().fieldErrors as Recordstring, string()>,<br \/>\n      };<br \/>\n    }<\/p>\n<p>    try {<br \/>\n      const data = await handler(parsed.data);<br \/>\n      return { success: true, data };<br \/>\n    } catch (error) {<br \/>\n      console.error(&#8216;Action error:&#8217;, error);<br \/>\n      return {<br \/>\n        success: false,<br \/>\n        error: error instanceof Error ? error.message : &#8216;Something went wrong&#8217;<br \/>\n      };<br \/>\n    }<br \/>\n  };<br \/>\n}<\/p>\n<p>\/\/ Usage<br \/>\nexport const generateImageAction = createAction(<br \/>\n  GenerateSchema,<br \/>\n  async (input) => {<br \/>\n    const job = await submitGenerationJob(input);<br \/>\n    return { jobId: job.id };<br \/>\n  }<br \/>\n);<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>  The Payoff<\/p>\n<p>With this pattern in place:<\/p>\n<p>Every action has a consistent interface \u2014 ActionResult<\/p>\n<p>Validation errors surface to the client with field-level detail<br \/>\nTypeScript knows the exact shape of success and error responses<br \/>\nError handling is centralized rather than ad hoc per action<br \/>\nAdding a new action means writing the schema and handler \u2014 the boilerplate is handled<br \/>\nThe upfront investment in the wrapper and types pays off quickly across a codebase with more than a handful of Server Actions.<\/p>\n<p>  Testing Server Actions<\/p>\n<p>Server Actions are async functions \u2014 they&#8217;re straightforward to unit test:<\/p>\n<p>\/\/ __tests__\/actions\/generate.test.ts<br \/>\nimport { generateImageAction } from &#8216;@\/app\/actions\/generate&#8217;;<\/p>\n<p>\/\/ Mock the generation service<br \/>\njest.mock(&#8216;@\/lib\/generation&#8217;, () => ({<br \/>\n  submitGenerationJob: jest.fn(),<br \/>\n}));<\/p>\n<p>import { submitGenerationJob } from &#8216;@\/lib\/generation&#8217;;<br \/>\nconst mockSubmit = submitGenerationJob as jest.Mock;<\/p>\n<p>describe(&#8216;generateImageAction&#8217;, () => {<br \/>\n  it(&#8216;returns success with valid input&#8217;, async () => {<br \/>\n    mockSubmit.mockResolvedValue({ id: &#8216;job-123&#8217;, estimatedDuration: 8 });<\/p>\n<p>    const result = await generateImageAction({<br \/>\n      prompt: &#8216;A sunset over mountains&#8217;,<br \/>\n      aspectRatio: &#8217;16:9&#8242;,<br \/>\n    });<\/p>\n<p>    expect(result.success).toBe(true);<br \/>\n    if (result.success) {<br \/>\n      expect(result.data.jobId).toBe(&#8216;job-123&#8217;);<br \/>\n    }<br \/>\n  });<\/p>\n<p>  it(&#8216;returns validation error for short prompt&#8217;, async () => {<br \/>\n    const result = await generateImageAction({<br \/>\n      prompt: &#8216;hi&#8217;, \/\/ Too short<br \/>\n      aspectRatio: &#8216;1:1&#8217;,<br \/>\n    });<\/p>\n<p>    expect(result.success).toBe(false);<br \/>\n    if (!result.success) {<br \/>\n      expect(result.fieldErrors?.prompt).toBeDefined();<br \/>\n    }<br \/>\n  });<\/p>\n<p>  it(&#8216;returns error when service throws&#8217;, async () => {<br \/>\n    mockSubmit.mockRejectedValue(new Error(&#8216;Service unavailable&#8217;));<\/p>\n<p>    const result = await generateImageAction({<br \/>\n      prompt: &#8216;A valid prompt that is long enough&#8217;,<br \/>\n      aspectRatio: &#8216;1:1&#8217;,<br \/>\n    });<\/p>\n<p>    expect(result.success).toBe(false);<br \/>\n  });<br \/>\n});<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Testing with the ActionResult type makes assertions clean \u2014 the discriminated union means TypeScript narrows the type inside the if (result.success) block, so you get full type checking on both success and error paths.<\/p>\n<p>  Common Pitfalls<\/p>\n<p>Forgetting that Server Actions run on the server. They don&#8217;t have access to window, document, or browser APIs. If you&#8217;re calling a Server Action from a component that also uses browser APIs, make sure the action itself doesn&#8217;t try to use them.<\/p>\n<p>Not handling revalidatePath or revalidateTag after mutations. If an action mutates data and the page should reflect that, you need to explicitly invalidate the cache:<\/p>\n<p>import { revalidatePath } from &#8216;next\/cache&#8217;;<\/p>\n<p>export async function deleteItem(id: string): PromiseActionResultvoid>> {<br \/>\n  try {<br \/>\n    await db.items.delete(id);<br \/>\n    revalidatePath(&#8216;\/items&#8217;); \/\/ Update the cache<br \/>\n    return { success: true, data: undefined };<br \/>\n  } catch {<br \/>\n    return { success: false, error: &#8216;Failed to delete item&#8217; };<br \/>\n  }<br \/>\n}<\/p>\n<p>    Enter fullscreen mode<\/p>\n<p>    Exit fullscreen mode<\/p>\n<p>Passing complex objects when primitives work. Server Actions serialize arguments across the network. Simple types (strings, numbers, plain objects) serialize cleanly. Class instances, functions, and non-serializable objects don&#8217;t. Keep action inputs to JSON-serializable types.<\/p>\n<p><br \/>\n<br \/><a href=\"https:\/\/dev.to\/aon_infotech_3a1b6ff525fc\/building-a-type-safe-api-layer-in-nextjs-app-router-with-zod-and-server-actions-ma2\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Server Actions in Next.js App Router look deceptively simple \u2014 write an async function, mark it with &#8216;use server&#8217;, call it from a Client Component. The surface area is small. The problems surface when you start thinking about validation, error handling, and type safety across the client-server boundary. Without a deliberate approach, you end up [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6246,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[676],"tags":[761,765,762,763,764,1220,1221,760,904,824],"class_list":["post-6245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech-ai","tag-coding","tag-community","tag-development","tag-engineering","tag-inclusive","tag-nextjs","tag-react","tag-software","tag-typescript","tag-webdev"],"_links":{"self":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/posts\/6245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6245"}],"version-history":[{"count":0,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/posts\/6245\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=\/wp\/v2\/media\/6246"}],"wp:attachment":[{"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daiilynews.cu.ma\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}