Preparing your workspace
Forms can trigger server-side functions for validated mutations without requiring every operation to become a public client-side API.
Modern Next.js applications can use server-side mutation patterns for actions such as creating records, updating profiles, or submitting forms. The important principles are validation, authentication, authorization, and safe error handling. A server-side mutation should never trust values merely because the request came from the application's UI. After a mutation, the application may need to refresh or revalidate affected data so the interface reflects the new state.
An admin portal can submit a course form that creates a course on the server after checking the user's role and validating all fields.
'use server';
export async function createCourse(formData: FormData) {
const title = String(formData.get('title') ?? '').trim();
if (!title) {
throw new Error('Title is required');
}
// Authenticate, authorize, validate and save the course.
}The server-side function validates the submitted course title before performing the database mutation.