Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Storage and Data

Forms and Submissions

Handle contact forms without a server: Netlify Forms with data-netlify, spam protection, notifications and webhooks, and the Vercel approach with serverless functions and validation.

A static site has no back end, yet almost every site needs a contact form, a newsletter signup or a feedback box. The two platforms take different routes: Netlify has a built-in form handler you switch on with an attribute, while Vercel has no form product, so you handle submissions in a function (or a framework action) and send the data somewhere.

The platforms cannot run on this site, and the live editors here sandbox forms so they never reach a real server. The HTML and config shown are read-only; the runnable code is a JavaScript stand-in that parses and validates a submission the way a handler would.

Netlify Forms

Add data-netlify="true" and a unique name to a normal HTML form:

HTML
<form name="contact" method="POST" data-netlify="true" netlify-honeypot="bot-field">
  <p hidden><label>Do not fill this: <input name="bot-field"></label></p>
  <label>Email <input type="email" name="email" required></label>
  <label>Message <textarea name="message" required></textarea></label>
  <button type="submit">Send</button>
</form>

At deploy time Netlify's build scans the HTML, detects the form and registers it. Submissions are stored in the site's Forms area, can trigger email notifications, and can call a webhook or a Netlify function (a function named submission-created runs automatically). Key facts:

  • The form must be present in the static HTML. A form rendered only by JavaScript (React, Vue) needs a hidden static copy of the form with the same name so the build can detect it; submit with fetch using the header Content-Type: application/x-www-form-urlencoded and a form-name field.
  • The honeypot field (hidden from humans, filled by bots) and reCAPTCHA support reduce spam, and Netlify also runs spam filtering.
  • A action="/thanks" attribute shows your own success page after submission.
  • Plans include a monthly submission allowance, then you pay or upgrade.

Vercel and others: handle it yourself

On Vercel, point the form at your own endpoint, such as a serverless function:

JavaScript
// api/contact.js  (read-only sketch)
export async function POST(request) {
  const form = await request.formData();
  const email = String(form.get("email") ?? "").trim();
  const message = String(form.get("message") ?? "").trim();
  if (!email.includes("@") || message.length < 5) {
    return Response.json({ error: "Invalid input" }, { status: 400 });
  }
  // forward to an email API, a database, or a service such as Formspree
  return Response.json({ ok: true });
}

You choose where the data goes: an email provider's HTTP API, a database (Databases from Serverless), or a hosted form service. Framework features (Next.js Server Actions, Remix actions, SvelteKit form actions) offer the same with less plumbing. The same technique works on Netlify when you need custom logic.

Two approaches

Netlify Forms Add data-netlify and deploy.
Your own function Validate and forward the data.
Honeypot A hidden field that traps bots.
Validate twice Browser for UX, server for safety.

Never trust the browser alone.

1 / 2

Parsing and validating a submission

The handler's job is to turn a request body into clean data or a clear error. The stand-in uses URLSearchParams (the format browsers send for ordinary forms) and applies rules including a honeypot check.

JS Submission handler stand-in

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Spam, abuse and limits

Public forms attract bots. Layer defenses:

  1. Honeypot field (cheap, catches simple bots).
  2. Rate limit per IP using a counter (Edge Config and Key-Value Stores).
  3. CAPTCHA or a proof-of-work/turnstile style check for persistent abuse.
  4. Server-side validation and length caps, always.
  5. Never put API keys in client code: your function holds the email-API key as an environment variable.

Also respect privacy: say what you do with the data, avoid collecting more than you need, and delete old submissions.

Making the client experience smooth

A plain form works without JavaScript and reloads the page. To avoid reloads, intercept the submit event and post with fetch, showing success or errors inline. The stand-in builds the request body exactly as a browser would, including the extra form-name field Netlify needs for JavaScript submissions, and a function that turns a server response into a user message:

JS Client-side submit stand-in

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does Netlify know a form exists?

    Form detection happens at deploy time by scanning the built HTML, which is why JavaScript-only forms need a static copy.
  2. 2. What is the purpose of a honeypot field?

    Humans do not see or fill it; automated scripts often do.
  3. 3. Where must input be validated for safety?

    Browser checks improve usability; anyone can send a request directly, so server checks are mandatory.
  4. 4. On Vercel, where do form submissions go without extra setup?

    Vercel has no built-in form product; use a function, framework action or hosted form service.

Summary

  • Netlify Forms: data-netlify="true", detected at build time, with notifications and webhooks.
  • Vercel and others: handle the POST in a function and forward the data.
  • Use honeypots, rate limits and server-side validation against spam.
  • JavaScript-rendered forms need a static twin on Netlify.
  • Keep API keys in environment variables, never in client code.

Next: Preview Deployments