Api

createSafeActionClient

API reference for createSafeActionClient — configure error handling, middleware, and create reusable type-safe action clients for Nuxt.

createSafeActionClient

Creates a new action client. Call this once and reuse it across actions.

Import

import { createSafeActionClient } from '#safe-action'

Signature

function createSafeActionClient(opts?: CreateSafeActionClientOpts): SafeActionClient

Options

OptionTypeRequiredDescription
handleServerError(error: Error) => stringNoTransform server errors before sending to the client. Receives the thrown error and should return a string message.

Usage

Basic

export const actionClient = createSafeActionClient()

With Error Handling

export const actionClient = createSafeActionClient({
  handleServerError: (error) => {
    console.error('Action error:', error.message)
    return error.message
  },
})

With Error Sanitization

import { ActionError } from '#safe-action'

export const actionClient = createSafeActionClient({
  handleServerError: (error) => {
    // Only expose intentional error messages
    if (error instanceof ActionError) {
      return error.message
    }
    // Hide internal details from the client
    return 'An unexpected error occurred'
  },
})

Returns

A SafeActionClient instance with the following methods:

MethodDescription
.schema()Set input validation schema
.outputSchema()Set output validation schema
.use()Add middleware
.metadata()Attach metadata
.action()Define the handler (terminal)

See the Builder Chain reference for details on each method.

Copyright © 2026