nuxt-actions
nuxt-actions

Type-safe server actions with Standard Schema validation, middleware, optimistic updates, streaming, and SSR queries

nuxt-actions logo

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.

npm versionnpm downloadsCoverageLicenseNuxt 3.7+ and 4

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 zod

Create 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

Compatibility

Supported
Nuxt3.7 and later, every Nuxt 4 release
Server runtimeNitro v2, and Nitro v3 on Nuxt 4.7+ (details)
ValidationZod 3.24+ and 4, Valibot 1.0+, ArkType 2.1+, any Standard Schema library
Node.jsThe 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-actionstrpc-nuxtnext-safe-action
FrameworkNuxtNuxtNext.js
ValidationAny Standard Schema libraryZodZod, Yup, Valibot
End-to-end type inferenceYesYesYes
Builder patternYesNoYes
Middleware with typed contextYesYesYes
Optimistic updatesYesNoYes
SSR queriesYesYesNo
Streaming (SSE)YesNoNo
Retry and deduplicationYesNoNo
Uses plain Nuxt server routesYesNo, tRPC protocolNot 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 module

Bug 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.

License

MIT