JSON fetch
Post the form as JSON from the browser, keep the visitor on the page and handle 200, 400, 413 and 429 explicitly.
Sample: samples/json-fetch (contact-form.js, an ES module exporting mountContactForm, plus index.html).
The snippet
const res = await fetch('https://api.contactusiq.com/s/au_xxxxxxx', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({
name, email, message,
_honeypot: '',
_page: location.href,
'cf-turnstile-response': token, // omit if the page has no Turnstile
}),
});
if (res.status === 200) {
const { id } = await res.json(); // { ok: true, id: "sub_..." }
} else if (res.status === 400) {
const { error } = await res.json(); // { ok: false, error: "..." }
} else if (res.status === 429) {
const wait = res.headers.get('Retry-After'); // seconds
} else if (res.status === 413) {
// body over 64 KB
}How it works
Content-Type: application/jsonmakes the request cross-origin from the browser's point of view, so the intake API answers theOPTIONSpreflight with CORS headers for the form's verified domains only. Serve the page from a verified domain.Accept: application/jsonswitches the success response from the HTML thank-you page to{ok, id}._pageis optional; it tells the inbox which page the message came from.cf-turnstile-responseis the token from the Turnstile widget. The sample renders Turnstile with the explicit API (?render=explicit) so the token is always available and the widget is reset after each attempt.- Optional
Idempotency-Keyheader or_idempotencyfield de-duplicates double clicks for 10 minutes.
The sample module, mountContactForm(form, { formId, turnstileSiteKey, onSuccess }), serialises the form with FormData, posts it, and maps each status to a message in a role="status" element. Network or CORS failures show "could not reach the server".
Steps
- Copy
contact-form.jsnext to your page. - Use a
formwith inputs namedname,email,messageand an empty_honeypot, plus adivwithdata-turnstileand apwithdata-status(the module creates them if missing). - Call
mountContactForm(document.querySelector('#contact'), { formId: 'au_xxxxxxx', turnstileSiteKey: '0x4AAAAAAA_PLACEHOLDER' }). OmitturnstileSiteKeyto skip Turnstile. - Deploy on the verified domain.
How to test
Network tab: OPTIONS /s/au_xxxxxxx returns 204, then POST returns 200 with {"ok":true,"id":"sub_..."}. Submit six times within ten minutes to see the 429 path.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
| CORS error, no response | The page origin is not a verified domain, so the preflight was refused. Serve from the verified domain, or allow any origin on the form for development. |
200 but nothing in the inbox | Silent origin or spam rejection. Check the domain is verified and that the honeypot is empty (autofill extensions sometimes fill it; the autocomplete="off" and off-screen position minimise this). |
| Turnstile never renders | Wrong site key, domain missing from the Turnstile widget, or CSP blocking challenges.cloudflare.com in script-src and frame-src. |