Next.js Server Actions: Handling Forms Without API Routes
No more writing fetch calls and API routes by hand. Server Actions let a form call a server function directly.
What you will learn
- Server Actions are defined with the
"use server"directive at the top of the function or file. - Form data arrives on the server as a
FormDataobject. - Zod validation: always check that the email is valid and the message is within the length limit before saving.
- Email notifications: once the data is checked, Resend can instantly notify the site owner, straight to their phone.
Example of a secure Server Action
In src/actions/contact.ts, define a server function that receives the form data, validates it with a Zod schema and sends an email.
Step by step
Install Zod validation and the Resend email service
npm install zod resendA standard stack for checking input and reliably delivering email from a website.
Tip from practice
Always set up DKIM and SPF records for your domain in the Resend settings. This greatly improves the chance that your emails land in the inbox rather than the spam folder.