Library / SDK
next-safe-action/next-safe-action avatar
next-safe-action/next-safe-action

next-safe-action: typed Server Actions for Next.js, and where the wrapper stops helping

Type safe and validated Server Actions in your Next.js project.

3,077 stars48 forksTypeScriptMIT

At a glance

What is it?
next-safe-action wraps Next.js Server Actions in a validation and middleware layer so inputs, outputs and errors keep their types from the client call site to the server handler. It fits App Router codebases that already use a validator, and fits poorly where plain framework primitives are enough.
Who is it for?
Adopt next-safe-action if your App Router project already validates inputs with Zod or another supported library and you want the parsed result and the error shape to be typed at the call site. Do not adopt it if you are on the Pages Router, if your actions are trivial one-liners, or if you cannot accept a wrapper owning the request lifecycle.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 2 days ago.
What is it written in?
Mainly TypeScript, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on October 9, 2026, and from our analysis. They are not legal advice.

Editorial analysis

The gap next-safe-action fills in Next.js Server Actions

A Server Action in the App Router is a function marked with "use server". The framework gives you a transport and a callable reference. It does not give you a validated argument. Whatever the client sends arrives as the parameter, and TypeScript's guarantee ends at the network boundary: the declared type of the argument is a claim about the caller, not a fact about the payload. Every action therefore needs the same three things by hand, a parse step, a shape for field-level errors, and a decision about what a thrown exception looks like on the client.

next-safe-action is for teams that got tired of writing that scaffolding per action. The README describes it as a library that "takes full advantage of the latest and greatest Next.js, React and TypeScript features to let you define type safe Server Actions and execute them inside React Components". The scope is narrower than a data layer: it does not own caching, does not replace your database client, and does not generate an HTTP API. It owns the boundary between a React component and one server function.

The intended audience is an App Router codebase with TypeScript already in strict mode and a validation library already in the dependency tree. If your project has no validator, this library is the wrong entry point; add one first.

How the action wrapper, validation and middleware fit together

The mechanism is a builder. You create a client with createSafeActionClient, then chain a schema and a handler onto it. The schema is the parsing step: it runs on the server before your handler body, and its inferred output type becomes the type of the parsed input your handler receives. That is the end-to-end part. The client call site sees the same inferred types because the action object carries them through the generated reference.

Middleware is the second layer. The README lists a "powerful middleware system" among the features, and the repository layout supports that description: the monorepo has a packages directory for the library itself, an apps directory holding a playground and a docs site, and a turbo.json driving the workspace tasks. Middleware in this design is where authentication, logging and context injection live, because it runs before the action body and can short-circuit it. A middleware that returns early prevents the handler from executing, which is the intended way to reject an unauthenticated call without throwing from inside business logic.

Validation is not tied to one library. The topics list zod, and the feature list says "Input/output validation using multiple validation libraries", so the parse step is pluggable. Output validation is the less obvious half: you can assert the shape your handler returns, which catches the case where a database row drifts from the type you promised the client.

Error handling is a separate channel from the return value. The README advertises "Advanced server error handling", and this is the part most worth reading in the docs before you design your UI, because the shape of a validation failure and the shape of a thrown server error are different, and your form component has to render both.

Installing next-safe-action and defining a first action

The README gives a single install command, and the package is published on npm under the name next-safe-action. The monorepo's package.json sets the Node engine floor at >=18.17, so confirm your runtime before installing.

bash
npm i next-safe-action

After that, create a client and attach a schema and a handler. The README does not reproduce a full example inline; it points to the playground application in the repository at apps/playground, which is described as "a basic working implementation of the library". Read that directory rather than guessing at the API surface, because the builder methods are the part that changed between major versions.

The README does not show the builder chain, so the exact call sequence has to come from the playground or the documentation site. What the feature list does establish is that a schema is attached to an action and that middleware runs before the handler body. Confirm the current method names against the version you install rather than copying a snippet from an older tutorial.

For a first real use, wire the action into a form. The feature list includes "Form Actions support" and "Optimistic updates", which means the library is designed to sit behind React's form handling rather than beside it. Start with a single field, confirm that a bad value produces a field error instead of a crash, then add middleware for authentication once the happy path works.

Where next-safe-action is the wrong tool

The library is a wrapper, and wrappers impose a shape. If your team's convention is to call Server Actions directly and parse inside them with a shared helper, adding this dependency buys you type inference at the call site and a consistent error envelope, at the cost of one more abstraction between the component and the function. For an application with five actions, that trade is not obviously worth it.

The harder constraint is the framework. Server Actions are an App Router feature. The README and the topics list are built around the App Router, React Server Components and mutations. A Pages Router codebase has no Server Actions to wrap, so the library has nothing to do there. Similarly, if your server functions are consumed by something other than a React component, such as a webhook handler or a background job, the client-side execution half of the library is dead weight.

There is also a versioning risk. The README devotes a section to migrating from v7 to v8 and links a dedicated migration guide, which tells you the builder API has moved across major versions. The recent releases in the repository are all 8.x, with 8.7.3 published on 2026-09-07. If you pin an older major, expect to read that guide before upgrading, and expect your middleware to be the part that needs editing.

Finally, error handling has a learning curve. Because validation failures and thrown server errors arrive through different paths, a component that assumes one shape will silently mishandle the other. Budget time for reading the error-handling documentation rather than discovering the distinction in production.

next-safe-action compared with tRPC and with plain actions

People searching for an alternative usually land on tRPC, and the comparison is worth stating precisely. tRPC builds a typed API layer: you define a router, procedures live on that router, and the client is a generated proxy that mirrors the router's shape. It assumes your server exposes an HTTP surface and that you want one place describing all of it. next-safe-action does not build a router. Each action is defined where it is used, and the transport is whatever Next.js already provides for Server Actions. There is no client proxy to generate and no API tree to maintain.

The practical difference shows up in caching and in non-React consumers. A tRPC router is callable from anything that can speak its protocol. A next-safe-action action is a Server Action, so its natural caller is a React component in the same application. If you need a typed endpoint that a mobile client or a third-party integration can call, tRPC is the closer fit. If your entire consumer surface is React components inside one Next.js app, the router is ceremony you do not need.

Against plain Server Actions, the difference is the parse step and the error envelope. Plain actions give you the raw argument and let you decide everything else. That is fine and often preferable for small codebases. The wrapper earns its place when the number of actions grows to the point where inconsistent validation and three different error shapes become a maintenance problem.

Maintenance, upgrade cost and the MIT licence

The repository is not archived, and the last push was on 2026-09-07. Releases are frequent within the 8.x line: 8.7.3, 8.7.2 and 8.7.1 all landed within the first week of September 2026. The project also publishes preview releases through pkg.pr.new, which the README mentions, so you can test an unreleased build without waiting for a tagged version.

Upgrade cost is concentrated in major versions. The existence of a v7 to v8 migration guide means the maintainers treat breaking changes as a documented event rather than something to discover from the changelog. The monorepo uses Changesets, visible in the .changeset directory and the version-packages and release scripts in the root package.json, so each release carries a changelog entry. For a team pinning the library, the realistic cost is one migration pass per major version, and the files most likely to need edits are wherever you define clients and middleware, since those are the chained builder calls.

The licence is MIT. That permits commercial use, modification and redistribution provided the copyright notice and permission notice are included. It offers no patent grant and no warranty, and this is not legal advice; check with your own counsel if your organisation has stricter licence review than MIT typically clears.

Editorial conclusion

Adopt next-safe-action if your App Router project already validates inputs with Zod or another supported library and you want the parsed result and the error shape to be typed at the call site. Do not adopt it if you are on the Pages Router, if your actions are trivial one-liners, or if you cannot accept a wrapper owning the request lifecycle. Before committing, read the v7 to v8 migration guide, confirm which validation libraries your version supports, and check whether the middleware hooks you rely on exist in the release you are pinning.

Frequently asked questions

What is next-safe-action?

It is a TypeScript library for defining type safe Server Actions in Next.js and executing them from React components. It adds input and output validation, a middleware system and server error handling on top of the framework's own Server Actions.

How do I install next-safe-action?

The README gives one command: npm i next-safe-action. The monorepo's package.json sets the Node engine requirement to >=18.17.

Which validation libraries does next-safe-action support?

The feature list states input and output validation using multiple validation libraries, and zod appears in the repository topics. The README does not enumerate the full set, so check the documentation site for the list matching your version.

Does next-safe-action work with the Next.js Pages Router?

The library wraps Server Actions, which are an App Router feature, and its topics list app-dir, react-server-components and server-actions. The README does not describe a Pages Router mode.

What does next-safe-action middleware do?

Middleware runs before the action body and can short-circuit it, which is how authentication and context injection are typically handled. The README lists a powerful middleware system among the features.

Official sources

  1. License: MIT
  2. next-safe-action/next-safe-action on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/next-safe-action-next-safe-action.svg)](https://hysenlabs.com/projects/next-safe-action-next-safe-action)