Introduction to Server Actions: Handling Forms in Next.js
Master Server Actions in Next.js to handle form submissions directly on the server. Stop building custom API endpoints and simplify your data workflows today.
Previously in this course, we explored building reusable blog components to keep our UI modular and clean. Now that we have a solid structure for our components, it's time to make our application interactive. In this lesson, we are moving beyond static content by learning how to handle user input using Server Actions.
What are Server Actions?
In traditional web development, handling a form submission usually requires a multi-step dance: you create an API endpoint, define a route, parse the incoming JSON, validate the data, and finally trigger your business logic.
Server Actions in Next.js change this by allowing you to define functions that execute directly on the server, callable from your client components. They are essentially RPC (Remote Procedure Call) functions that eliminate the need for manual API routing. When a user submits a form, the browser triggers a POST request to your server, which executes your action function immediately.
The 'use server' Directive
The magic of Server Actions relies on the 'use server' directive. This directive tells the Next.js compiler that a specific function (or an entire file) should be treated as a server-side entry point.
When you place 'use server' at the top of a file, every exported function in that file becomes a Server Action. If you place it inside a function, only that specific function becomes an action.
Here is how you define a basic action in a new file, actions.ts:
TYPESCRIPTCE9178">'use server' export async function submitComment(formData: FormData) { const username = formData.get(CE9178">'username'); const comment = formData.get(CE9178">'comment'); console.log(CE9178">`Received comment from ${username}: ${comment}`); // In future lessons, we'll save this to a database! }
Processing Form Data
To use this action in your UI, you simply pass the function to the action prop of a standard HTML <form> element. Next.js automatically passes the FormData object to your function, which is a standard Web API that makes extracting input values straightforward.
Let's integrate this into our blog’s comment section:
TSXimport { submitComment } from CE9178">'./actions'; export default function CommentForm() { return ( <form action={submitComment} className="flex flex-col gap-4"> <input name="username" placeholder="Your name" className="border p-2" required /> <textarea name="comment" placeholder="Write a comment..." className="border p-2" required /> <button type="submit" className="bg-blue-500 text-white p-2"> Submit </button> </form> ); }
This code works without any manual fetch requests or event.preventDefault() calls. The browser handles the form submission, and Next.js manages the execution on the server.
Hands-on Exercise: Create a Basic Contact Form
- Create a new file named
app/actions.tsand add the'use server'directive. - Define an asynchronous function
handleContactSubmit(formData: FormData)that extracts theemailfield and logs it to your terminal. - Create a new page at
app/contact/page.tsx. - Import your action and build a simple form using the
actionattribute. - Open your development server, fill out the form, and check your terminal console to see the log.
Common Pitfalls
- Forgetting 'use server': If you try to call a function from a client component without the directive, you will get a runtime error.
- Assuming Client-Side Context: Remember that Server Actions run on the server. You cannot use
window,document, or other browser-specific APIs inside them. - Security: Always treat incoming
FormDataas untrusted. Just like with traditional APIs, you must validate and sanitize user input on the server before doing anything with it. - Client vs. Server Components: You can import Server Actions into Client Components (using
'use client') perfectly fine, but you cannot define them inside the Client Component file itself.
FAQ
Are Server Actions just for forms?
No, while they are commonly used for forms, they can be triggered from anywhere, including event handlers like onClick or even inside a useEffect.
How does this compare to traditional API routes? While API routes are great for public-facing REST APIs, Server Actions are optimized for internal application logic and form handling, significantly reducing boilerplate code.
Recap
We've bridged the gap between the UI and the server by implementing our first Server Action. We learned that:
'use server'is the key to marking backend-only logic.FormDataprovides a standard way to access form inputs.- Standard HTML forms can be transformed into powerful server-side tools without custom API endpoints.
Up next, we will refine this workflow by building a newsletter sign-up form that handles real user state and provides immediate feedback.
Work with me

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.

Next.js E-commerce Store Development
Turn your Facebook page or small shop into a real online store — fast, mobile-first, and built to sell. Own your storefront, not just a social page.