---
title: JSON fetch
description: 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`](/samples/json-fetch/README.md) (`contact-form.js`, an ES module exporting `mountContactForm`, plus `index.html`).

## The snippet

```js
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/json` makes the request cross-origin from the browser's point of view, so the intake API answers the `OPTIONS` preflight with CORS headers for the form's verified domains only. Serve the page from a verified domain.
- `Accept: application/json` switches the success response from the HTML thank-you page to `{ok, id}`.
- `_page` is optional; it tells the inbox which page the message came from.
- `cf-turnstile-response` is 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-Key` header or `_idempotency` field 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

1. Copy `contact-form.js` next to your page.
2. Use a `form` with inputs named `name`, `email`, `message` and an empty `_honeypot`, plus a `div` with `data-turnstile` and a `p` with `data-status` (the module creates them if missing).
3. Call `mountContactForm(document.querySelector('#contact'), { formId: 'au_xxxxxxx', turnstileSiteKey: '0x4AAAAAAA_PLACEHOLDER' })`. Omit `turnstileSiteKey` to skip Turnstile.
4. 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`. |
