All guides

Contact form for Next.js

In Next.js you could write a route handler, add an email SDK and manage its API keys. For a contact form, posting from the browser to Pasteform is less code and has no secrets in your repo. Works with the App Router and Pages Router, and with output: "export".

Get my free form link Replace YOUR_FORM_ID below with your form ID, or copy the snippets from your dashboard where it's already filled in.

Step by step

  1. Create a form in Pasteform and copy the endpoint.
  2. Add the component below as app/contact/contact-form.tsx (App Router) or components/ContactForm.tsx (Pages Router).
  3. Render it from your contact page: <ContactForm />.
  4. Add your domain and localhost to Allowed domains.

contact-form.tsx

"use client";
import { useState } from "react";

export default function ContactForm() {
  const [state, setState] = useState<"idle" | "sending" | "sent" | "error">("idle");
  const [error, setError] = useState("");

  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault();
    setState("sending");
    const form = e.currentTarget;
    const res = await fetch("https://forms.prizenlabs.com/f/YOUR_FORM_ID", {
      method: "POST",
      headers: { "Content-Type": "application/json", Accept: "application/json" },
      body: JSON.stringify(Object.fromEntries(new FormData(form))),
    });
    const data = await res.json().catch(() => ({}));
    if (res.ok && data.success) {
      form.reset();
      setState("sent");
    } else {
      setError(data.message ?? "Something went wrong");
      setState("error");
    }
  }

  if (state === "sent") return <p>Thanks! We'll be in touch soon.</p>;
  return (
    <form onSubmit={handleSubmit} className="space-y-3">
      <input name="name" placeholder="Name" required />
      <input name="email" type="email" placeholder="Email" required />
      <textarea name="message" placeholder="Message" required />
      <input name="_gotcha" className="hidden" tabIndex={-1} autoComplete="off" />
      <button type="submit" disabled={state === "sending"}>
        {state === "sending" ? "Sending…" : "Send"}
      </button>
      {state === "error" && <p role="alert">{error}</p>}
    </form>
  );
}

Allowed domains

Add your production domain, and localhost for next dev.

Common pitfalls

What you get

Make your Next.js form send

Free form link in about 2 minutes. No credit card.

Get my free form link

Guides for other platforms

Lovable · Bolt · v0 · Framer · Webflow · Astro · Hugo · Jekyll · GitHub Pages · Netlify · Vercel · WordPress (static export) · Carrd · plain HTML