HarshPatel

Ahmedabad, Gujarat
Back to Blog
Next.jsServer ActionsReactFull-stackApp Router

Next.js 15 Server Actions Explained: Build a Full-Stack Form Without an API Route

Harsh PatelMay 4, 20263 min read21 views
Next.js 15 Server Actions Explained: Build a Full-Stack Form Without an API Route

Introduction

Before Server Actions, a simple form submission in Next.js meant: write a client component, wire up state, create an API route, call it with fetch, handle errors. It worked, but it was boilerplate-heavy.

Next.js 15 changes this. Server Actions let you write async functions that run on the server and call them directly from your JSX — no API route, no fetch, no useEffect. Here's how to build a contact form with them from scratch.

What is a Server Action?

A Server Action is an async function marked with the "use server" directive. When a form submits or a button is clicked, Next.js serialises the form data and sends it to the server — automatically. You never write the API endpoint yourself.

Step 1: Create the Server Action

Create a file called actions/contact.ts:

"use server";

import { db } from "@/lib/db";

export async function submitContact(formData: FormData) {
  const name = formData.get("name") as string;
  const email = formData.get("email") as string;
  const message = formData.get("message") as string;

  if (!name || !email || !message) {
    throw new Error("All fields are required");
  }

  await db.contactMessage.create({
    data: { name, email, message },
  });

  return { success: true };
}

Step 2: Build the Form Component

Now create your form. This is a Server Component — no "use client" needed:

import { submitContact } from "@/actions/contact";

export default function ContactForm() {
  return (