-
What Server Actions (or Server Functions) are
-
How are they different from API Routes
- When to use each
- How to implement Server Actions (with simple examples)
-
Important security and performance tips
-
What this means for the future of API Routes
What Are Server Actions / Server Functions?
-
You can call them directly from your React components (especially Server Components).
-
You can even use them inside Client Components (as long as they are imported from files with "use server" at the top).
- They are great for doing tasks like saving data, submitting forms, or updating something on the server.
- Behind the scenes, they work using POST requests only.
-
The data you pass to them and get back must be serializable (which means it can be safely turned into JSON and sent over the network).
API Routes (Route Handlers)
-
You want to create endpoints that other apps or systems can call.
-
You are building a public API.
- You have complex routing or multiple methods in one place.
- You need to control things like headers, authentication tokens, or CORS.
Server Actions vs API Routes: Which to Use When

When to Use Each
Use Server Actions when:
-
You are submitting a form from a component.
-
You want to update data without creating a new API file.
- Your logic is directly related to the component UI.
- You want automatic revalidation or caching features.
Use API Routes when:
-
You need to expose endpoints to external systems or third parties.
-
You need to handle multiple HTTP methods (GET, POST, etc.).
- You need to work with headers, tokens, or handle CORS.
- You are working on a large enterprise-level app or microservice.
How to Use Server Actions: Step-by-Step Examples
Example 1: Creating a Post with a Form
// app/actions.ts
'use server'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
export async function createPostAction(formData: FormData) {
const title = formData.get('title')
const content = formData.get('content')
if (!title || !content) {
throw new Error('Missing fields')
}
await db.posts.create({ title, content })
revalidatePath('/posts')
redirect('/posts')
}
// app/(pages)/new-post/page.tsx
import { createPostAction } from '@/app/actions'
export default function NewPostPage() {
return (
<form action={createPostAction}>
<input name="title" placeholder="Title" />
<textarea name="content" placeholder="Content"></textarea>
<button type="submit">Create Post</button>
</form>
)
}
What’s happening:
Example 2: Sending Feedback from a Client Component
// app/actions.ts
'use server'
export async function sendFeedback(formData: FormData) {
const feedback = formData.get('feedback')
return { status: 'ok' }
}
// app/components/FeedbackForm.tsx
'use client'
import { useFormState } from 'react-dom'
import { sendFeedback } from '@/app/actions'
export function FeedbackForm() {
const [state, action, pending] = useFormState(sendFeedback, { feedback: '' })
return (
<form action={action}>
<textarea name="feedback" />
<button type="submit">
{pending ? 'Sending...' : 'Send Feedback'}
</button>
</form>
)
}
What’s happening:
Example 3: Using Cookies, Redirects & Revalidation
// app/actions.ts
'use server'
import { cookies } from 'next/headers'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
export async function loginUser(formData: FormData) {
const username = formData.get('username')
const password = formData.get('password')
const cookieStore = cookies()
cookieStore.set('token', 'some-jwt-token', { httpOnly: true, path: '/' })
revalidatePath('/dashboard')
redirect('/dashboard')
}
What’s happening:
Security, Limitations, and Things to Keep in Mind
Real-World Use Cases
-
Auth0 has shown how Server Actions can securely call external APIs without needing extra route handlers.
-
GitHub developers mentioned that Server Actions helped them simplify code and reduce time, though they still use API Routes for large-scale fetching.
- Medium discussions point out that Server Actions improve developer productivity but should be used wisely to avoid overuse.
What’s Coming Next
-
Even faster caching and parallel execution
-
Better TypeScript safety
- Improved developer tools
- Stronger integration with edge runtimes and serverless functions
Conclusion
-
Use Server Actions for small, internal logic and form submissions.
-
Use API Routes for larger systems, integrations, and public APIs.
At Sparkle Web, our full-stack experts specialize in Next.js, React, and serverless architecture. We build scalable and high-performance web solutions powered by the latest frameworks like Next.js 15 and Server Actions. Want to upgrade your app to the future-ready Next.js stack? Let’s make it happen together. Connect with us today.

Vaishali Gaudani
Skilled React.js Developer with 3+ years of experience in creating dynamic, scalable, and user-friendly web applications. Dedicated to delivering high-quality solutions through innovative thinking and technical expertise.
Reply