Getting Started

Usage

Define a validated server action and submit a working typed Vue form.

Your first action

Create a shared client. Unexpected errors are logged on the server and receive a generic public message.

server/utils/action-client.ts
import { createSafeActionClient } from '#safe-action'

export const actionClient = createSafeActionClient({
  handleServerError(error) {
    console.error('Action failed:', error)
    return 'Something went wrong. Please try again.'
  },
})
server/actions/greet.ts
import { z } from 'zod'
import { actionClient } from '../utils/action-client'

export default actionClient
  .schema(z.object({ name: z.string().trim().min(1, 'Name is required') }))
  .outputSchema(z.object({ greeting: z.string() }))
  .action(({ parsedInput }) => ({ greeting: `Hello, ${parsedInput.name}!` }))

The module generates a POST route and a typed client reference. No server implementation is imported at runtime into this component.

app/components/GreetForm.vue
<script setup lang="ts">
import { greet } from '#safe-action/actions'

const { values, submit, data, fieldErrors, rootErrors, serverError, isExecuting, reset } =
  useFormAction(greet, { initialValues: { name: '' } })
</script>

<template>
  <form @submit.prevent="submit()">
    <label for="name">Name</label>
    <input id="name" v-model="values.name" :aria-invalid="Boolean(fieldErrors.name)" />
    <p v-for="message in fieldErrors.name" :key="message">{{ message }}</p>
    <p v-for="message in rootErrors" :key="message" role="alert">{{ message }}</p>
    <p v-if="serverError" role="alert">{{ serverError }}</p>
    <button :disabled="isExecuting">{{ isExecuting ? 'Sending…' : 'Greet' }}</button>
    <button type="button" @click="reset()">Reset</button>
    <p v-if="data">{{ data.greeting }}</p>
  </form>
</template>

For application-managed state, use useAction. execute() returns void; executeAsync() and form submit() resolve the full result envelope. Check serverError !== undefined and validationErrors !== undefined before consuming data; a successful void action may have no data property.

Copyright © 2026