Static sites & frameworks
How do I handle form submissions on Cloudflare Pages?
By the FormToMail team · Checked against official sources · Updated
Cloudflare Pages has no built-in form collection like Netlify Forms. Either write a Pages Function (a file in /functions that handles the POST, validates a Turnstile token server-side, stores rows in D1 and sends email via an API), or point the form at a form endpoint and skip the code.
| Piece | Role |
|---|---|
| Pages Functions | Server code for routes like /api/submit (onRequestPost) |
| Turnstile | Bot check; verify the token server-side with the siteverify API |
| D1 | SQL database to store submissions |
| An email API | Sends you the notification (Cloudflare doesn't do this for you) |
Pages Function outline
// functions/api/submit.js
export async function onRequestPost({ request, env }) {
const form = await request.formData();
if (form.get("_gotcha")) return new Response("ok");
const v = await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
method: "POST",
body: new URLSearchParams({ secret: env.TURNSTILE_SECRET, response: form.get("cf-turnstile-response") ?? "" }),
}).then((r) => r.json());
if (!v.success) return new Response("Failed bot check", { status: 400 });
// store in D1, then call your email provider's API with env.EMAIL_API_KEY
return Response.redirect(new URL("/thanks", request.url), 303);
}Or with no code
Set the form's action to a FormToMail endpoint. If you already use Turnstile, paste your Turnstile secret key into the form's settings and FormToMail verifies the token for you.
<form action="https://getformtomail.com/f/YOUR_FORM_ID" method="POST">
<input name="name" required>
<input name="email" type="email" required>
<textarea name="message" required></textarea>
<input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">
<button type="submit">Send</button>
</form>Quick answers
Cloudflare Turnstile with a form backend?
Add the Turnstile widget to your form and paste its secret key in your FormToMail form settings; tokens are verified on every submission.
How do I build my own contact form API with Cloudflare Workers?
Use a Pages Function or Worker as above: parse the POST, verify Turnstile, store, send through an email API.
Netlify forms alternative after moving to Cloudflare Pages?
What to do next
Skip the Function: create a free endpoint (with optional Turnstile check).
Get your form endpointSources
- Cloudflare Docs: Pages tutorial, create a HTML form
- Cloudflare Docs: Pages Functions
- Cloudflare Docs: Turnstile server-side validation
- Cloudflare Docs: D1
Checked: . Prices, limits and features of other services change: confirm current values at the official links above. Product names belong to their owners; we are not affiliated with them.