nuxt-actions
nuxt-actions
Type-safe server actions for Nuxt.
Validate input with Zod, Valibot, or ArkType, then call the action from any component with types inferred end to end.
Documentation |Getting started |Examples |Live demo |Changelog
Why nuxt-actions
Nuxt server routes are flexible, but every endpoint repeats the same work: parse the body, validate it, shape errors, and keep the client's types in sync by hand. nuxt-actions turns that into one file per action:
- One definition, two sides. Write the action once in
server/actions/. The client imports a typed reference from#actions, so input and output types follow the server code with no manual generics. - Any validation library. Zod, Valibot, ArkType, or any Standard Schema library, with field-level errors in one consistent shape.
- Built for production. Middleware with typed context, retries, deduplication, optimistic updates, SSR queries, streaming, a server cache, idempotency, rate limiting, and CSRF protection are part of the module, not something you assemble.
Quick start
Add the module and a validation library:
npx nuxi module add nuxt-actions
pnpm add zodCreate an action. The file name becomes the endpoint. Actions use POST unless the file name ends in a method suffix such as .get:
// server/actions/create-todo.ts
import { z } from 'zod'
export default defineAction({
input: z.object({
title: z.string().min(1, 'Title is required'),
}),
handler: async ({ input }) => {
return { id: Date.now(), title: input.title, done: false }
},
})Call it from a component. data is typed as { id: number, title: string, done: boolean }:
<script setup lang="ts">
import { createTodo } from '#actions'
const { execute, data, error, isExecuting } = useAction(createTodo)
</script>
<template>
<form @submit.prevent="execute({ title: 'Buy milk' })">
<button :disabled="isExecuting">Add todo</button>
<p v-if="data">Created {{ data.title }}</p>
<p v-if="error">{{ error.fieldErrors?.title?.[0] ?? error.message }}</p>
</form>
</template>defineAction, useAction, and the other utilities are auto-imported. The getting started guide walks through the full setup.
What is included
| Area | Highlights |
|---|---|
| Server | defineAction, the createActionClient builder, middleware with typed context, output validation, streaming, file uploads, idempotency, a server cache for GET actions, callAction |
| Client | useAction, SSR queries, cache invalidation, optimistic updates, infinite queries, form actions, batch actions, upload progress |
| Production | Retry with backoff and Retry-After, request deduplication and cancellation, rate limiting with shared stores, origin checks, and CSRF tokens, an auth preset, global hooks |
| Tooling | OpenAPI 3.1 and Swagger UI, a Nuxt DevTools tab, npx nuxt-actions add scaffolding, type updates on save, unit tests with plain Vitest |
Compatibility
| Supported | |
|---|---|
| Nuxt | 3.7 and later, every Nuxt 4 release |
| Server runtime | Nitro v2, and Nitro v3 on Nuxt 4.7+ (details) |
| Validation | Zod 3.24+ and 4, Valibot 1.0+, ArkType 2.1+, any Standard Schema library |
| Node.js | The version your Nuxt release requires (22 or 24 for Nuxt 4.7) |
Every release runs the same end-to-end suite on Nitro v2, Nitro v3, Nitro v3 without nitroLegacy, and a Nuxt 5 preview.
How it compares
| nuxt-actions | trpc-nuxt | next-safe-action | |
|---|---|---|---|
| Framework | Nuxt | Nuxt | Next.js |
| Validation | Any Standard Schema library | Zod | Zod, Yup, Valibot |
| End-to-end type inference | Yes | Yes | Yes |
| Builder pattern | Yes | No | Yes |
| Middleware with typed context | Yes | Yes | Yes |
| Optimistic updates | Yes | No | Yes |
| SSR queries | Yes | Yes | No |
| Streaming (SSE) | Yes | No | No |
| Retry and deduplication | Yes | No | No |
| Uses plain Nuxt server routes | Yes | No, tRPC protocol | Not applicable |
Contributing
pnpm install
pnpm run dev:prepare # build the module stub and generate types
pnpm run dev # start the playground
pnpm run lint
pnpm run test:unit
pnpm run test:e2e # Nitro v2, Nitro v3, and Nuxt 5 preview fixtures
pnpm run prepack # build the moduleBug reports and pull requests are welcome. Read CONTRIBUTING.md before opening one.
Sponsors
If nuxt-actions saves you time, you can support its development on GitHub Sponsors.