Cloudflare Pages
A static Pages site can post straight to FormRoost from the browser, as in browser submissions. This recipe adds two Pages Functions: a server-side proxy that submits with an API key, and a webhook receiver that verifies Standard Webhooks signatures. Both use only Web APIs (fetch, crypto.subtle), with no Node-only dependencies. Hosts, IDs, and keys below are placeholders.
Secrets
Set these as encrypted environment variables in the Pages project settings, or with Wrangler:
npx wrangler pages secret put FORMROOST_URL # https://formroost.com/s/v1/sandbox/fpub_REPLACE_ME
npx wrangler pages secret put FORMROOST_API_KEY # fr_sandbox_REPLACE_ME
npx wrangler pages secret put WEBHOOK_SECRET # whsec_... from the webhook endpoint
For local development, put the same names in .dev.vars and keep that file out of Git.
Submission proxy
// functions/api/contact.js
export async function onRequestPost({ request, env }) {
const form = await request.formData()
const response = await fetch(env.FORMROOST_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${env.FORMROOST_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: form.get('email'),
message: form.get('message'),
_gotcha: form.get('_gotcha') ?? '',
}),
})
const target = response.status === 201 ? '/thanks/' : '/contact/error/'
return Response.redirect(new URL(target, request.url), 303)
}
Point a static form at it with <form action="/api/contact" method="post">. The proxy sends no Origin, so production requires the bearer key. FormRoost's per-network rate limit sees Cloudflare rather than the visitor; add rate limiting rules for /api/contact.
Webhook receiver
Create a webhook endpoint on the form for https://<your-site>.pages.dev/api/formroost-webhook and subscribe to submission.created. The receiver checks the signature on the raw body before parsing JSON.
// functions/api/formroost-webhook.js
const encoder = new TextEncoder()
const fromBase64 = (value) => Uint8Array.from(atob(value), (char) => char.charCodeAt(0))
async function verify(request, secret) {
const id = request.headers.get('webhook-id') ?? ''
const stamp = request.headers.get('webhook-timestamp') ?? ''
const signatures = request.headers.get('webhook-signature') ?? ''
if (!/^evt_[A-Za-z0-9_-]+$/.test(id) || !/^\d+$/.test(stamp)) return null
if (Math.abs(Date.now() / 1000 - Number(stamp)) > 300) return null
if (!secret?.startsWith('whsec_')) throw new Error('Set WEBHOOK_SECRET')
const raw = new Uint8Array(await request.arrayBuffer())
const prefix = encoder.encode(`${id}.${stamp}.`)
const signed = new Uint8Array(prefix.length + raw.length)
signed.set(prefix)
signed.set(raw, prefix.length)
const key = await crypto.subtle.importKey(
'raw',
fromBase64(secret.slice('whsec_'.length)),
{ name: 'HMAC', hash: 'SHA-256' },
false,
['verify'],
)
for (const part of signatures.split(' ')) {
const [version, signature] = part.split(',')
if (version !== 'v1' || !signature) continue
let bytes
try {
bytes = fromBase64(signature)
} catch {
continue
}
// subtle.verify compares in constant time.
if (await crypto.subtle.verify('HMAC', key, bytes, signed)) return { id, raw }
}
return null
}
export async function onRequestPost({ request, env }) {
const verified = await verify(request, env.WEBHOOK_SECRET)
if (!verified) return new Response(null, { status: 401 })
let event
try {
event = JSON.parse(new TextDecoder().decode(verified.raw))
} catch {
return new Response(null, { status: 400 })
}
if (event?.id !== verified.id) return new Response(null, { status: 400 })
if (event.type === 'com.formroost.submission.created.v1') {
// Delivery is at least once: deduplicate with an atomic unique constraint
// (for example a D1 table with `event_id TEXT PRIMARY KEY` and `INSERT OR IGNORE`),
// rather than an eventually consistent KV read-before-write check.
console.log('New lead', event.data.receiptId)
}
return new Response(null, { status: 204 })
}
Signatures cover webhook-id, webhook-timestamp, and the raw body, joined by .. During a secret rotation, the header carries one v1 signature per secret; any match is accepted. Return 2xx quickly and move slow work to a queue, since FormRoost stops waiting after ten seconds. See webhook receivers for event fields and retries.
Go to production
Publish the form configuration, change sandbox to production in FORMROOST_URL, use a production key, and create a production webhook endpoint pointed at your HTTPS Pages URL. Because each webhook endpoint has its own signing secret, update WEBHOOK_SECRET to the production endpoint's secret (or bind separate sandbox and production secrets/paths if you want both active at once).