Astro
Two patterns: a static page that posts straight from the browser, and an SSR endpoint that submits with a server key. Hosts, IDs, and keys below are placeholders.
Add your dev origin, such as http://localhost:4321, to Allowed origins on the form first. See browser submissions.
The form works without JavaScript as a native post. With JavaScript, the script sends JSON instead, adds _fr_started_at, and shows errors inline.
# .env — PUBLIC_ values are bundled into the page, so this holds no secret.
PUBLIC_FORMROOST_URL=https://formroost.com/s/v1/sandbox/fpub_REPLACE_ME
---
// src/pages/contact.astro
const endpoint = import.meta.env.PUBLIC_FORMROOST_URL
---
<form id="contact" action={endpoint} method="post">
<label>Email <input name="email" type="email" required /></label>
<label>Message <textarea name="message" required></textarea></label>
<!-- Honeypot: hidden from people, left empty by real visitors. -->
<div aria-hidden="true" style="position:absolute;left:-10000px">
<label>Leave empty <input name="_gotcha" tabindex="-1" autocomplete="off" /></label>
</div>
<button type="submit">Send</button>
<p id="contact-status" role="status"></p>
</form>
<script>
const form = document.querySelector<HTMLFormElement>('#contact')!
const status = document.querySelector<HTMLParagraphElement>('#contact-status')!
const startedAt = Date.now()
form.addEventListener('submit', async (event) => {
event.preventDefault()
const fields = Object.fromEntries(new FormData(form))
status.textContent = 'Sending…'
try {
const response = await fetch(form.action, {
method: 'POST',
credentials: 'omit',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
// A number, not a string: Unix milliseconds when the page loaded.
body: JSON.stringify({ ...fields, _fr_started_at: startedAt }),
})
for (const el of form.querySelectorAll('[aria-invalid="true"]')) {
el.removeAttribute('aria-invalid')
}
if (response.status === 201) {
form.reset()
status.textContent = 'Thanks, we received your message.'
return
}
const problem = response.headers
.get('content-type')
?.includes('application/problem+json')
? await response.json()
: null
if (problem?.code === 'submission_schema_invalid' && Array.isArray(problem.errors)) {
for (const err of problem.errors) {
const field = String(err.path ?? '').replace(/^\//, '').split('/')[0]
if (field) form.elements.namedItem(field)?.setAttribute?.('aria-invalid', 'true')
}
status.textContent = 'Please check the highlighted fields.'
return
}
status.textContent = 'Something went wrong. Please try again.'
} catch {
// Covers network errors and unreadable cross-origin error responses (such as rate limits).
status.textContent = 'Could not submit right now. Wait a moment and try again.'
}
})
</script>
Success is 201 with a JSON receipt. Errors are RFC 9457 Problem Details; see errors for every code.
If you turn on Cloudflare Turnstile for the form, render the widget inside the form with data-response-field-name="_fr_turnstile_token" so its token is sent in the reserved field. See reserved anti-spam fields.
SSR endpoint with a server key
Use this when the submission should come from your server, for example after your own checks. It needs an Astro adapter for on-demand rendering.
# .env — no PUBLIC_ prefix, so Astro keeps these on the server.
FORMROOST_URL=https://formroost.com/s/v1/sandbox/fpub_REPLACE_ME
FORMROOST_API_KEY=fr_sandbox_REPLACE_ME
// src/pages/api/contact.ts
import type { APIRoute } from 'astro'
export const prerender = false
export const POST: APIRoute = async ({ request }) => {
const form = await request.formData()
const response = await fetch(import.meta.env.FORMROOST_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${import.meta.env.FORMROOST_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: form.get('email'),
message: form.get('message'),
_gotcha: form.get('_gotcha') ?? '',
}),
signal: AbortSignal.timeout(10_000),
})
if (response.status === 201)
return Response.redirect(new URL('/thanks', request.url), 303)
const problem = await response.json().catch(() => null)
console.error('FormRoost rejected the submission', response.status, problem?.code)
return Response.redirect(new URL('/contact?error=1', request.url), 303)
}
Point the form at your endpoint with <form action="/api/contact" method="post">. Server requests omit Origin, so production requires the Authorization header. Keep the key out of any PUBLIC_ variable. To make retries safe, send an Idempotency-Key that stays the same for one visitor submission, as in the Next.js recipe. See server API keys.
Go to production
Add your live HTTPS origin, publish the configuration, and change sandbox to production in the URL. A server endpoint also needs a production key.