Skip to content

Type-safe server actions for Nuxt

Write the action once on the server. Call it from any component with validated input and typed results, without writing a single generic.

npx nuxi module add nuxt-actions
create-todo.tsserver/actions
import { z } from 'zod'export default defineAction({  input: z.object({ title: z.string() }),  handler: async ({ input }) => ({    id: Date.now(),    title: input.title,  }),})
index.vueapp/pages
<script setup lang="ts">import { createTodo } from '#actions'const { execute, data } = useAction(createTodo)const data: Ref<{
  id: number
  title: string
} | null>await execute({ title: 'Buy milk' })</script>
Types inferred from server/actions/create-todo.ts0 problems

Works with the tools you already use

  • Nuxt 3.7+ and 4
  • Nitro v2 and v3
  • Zod
  • Valibot
  • ArkType
  • Standard Schema

Every release runs the same end-to-end suite on Nitro v2, Nitro v3, and a Nuxt 5 preview. How the server runtime is chosen

What is included

Everything an action needs, from input to production

Quick start

Two files. No glue code.

The file name becomes the endpoint, and #actions hands the component a typed reference to it.

1. Create server/actions/create-todo.ts

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 }
  },
})

2. Call it from app/pages/todos.vue

vue
<script setup lang="ts">
import { createTodo } from '#actions'

const { execute, data, error } = useAction(createTodo)
</script>

<template>
  <button @click="execute({ title: 'Buy milk' })">
    Add todo
  </button>
  <p v-if="data">Created {{ data.title }}</p>
  <p v-if="error">{{ error.message }}</p>
</template>

Ship your first action in five minutes

Install the module, add a file to server/actions, and call it from a page.

npx nuxi module add nuxt-actions