JavaScript and fetch
Send the form with fetch to keep people on your page and show the result yourself. This page covers the request, the JSON answer, every error, and cross-origin requests.
Last updated
Send a form with FormData
Take the fields from the form, add your access key, and post them. The Accept: application/json header is what asks for a JSON answer instead of a redirect.
<form id="contact">
<input type="text" name="name" required>
<input type="email" name="email" required>
<textarea name="message" required></textarea>
<button type="submit">Send</button>
<p id="result"></p>
</form>
<script>
const form = document.getElementById('contact');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const data = new FormData(form);
data.append('access_key', 'YOUR_ACCESS_KEY');
const response = await fetch('https://submit.formrelay.domodomu.com/submit', {
method: 'POST',
headers: { Accept: 'application/json' },
body: data,
});
const result = await response.json();
document.getElementById('result').textContent =
result.success ? 'Thanks! Your message was sent.' : result.message;
if (result.success) form.reset();
});
</script>Send JSON
You can also post one JSON object. Text values are kept as they are, true and false become the words, null becomes an empty value, and numbers, arrays and nested objects are kept as their JSON text. A request with a JSON body always gets a JSON answer.
const response = await fetch('https://submit.formrelay.domodomu.com/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify({
access_key: 'YOUR_ACCESS_KEY',
name: 'Ada Lovelace',
email: 'ada@example.com',
message: 'Hello',
}),
});
const result = await response.json();
if (!result.success) console.error(response.status, result.message);The special fields work the same way in both: subject, email, botcheck and the captcha token. redirect has no effect on a request that asks for JSON. See Special fields.
The answer
Every JSON answer has the same three properties.
| Property | Type | Meaning |
|---|---|---|
success | boolean | Whether the submission was accepted. |
message | string | A sentence you can show to the person: "Submission received." or the reason it was refused. |
id | string or null | The submission's id when one was accepted. It is the same id the API and webhooks use. Otherwise null. |
Accepted (HTTP 200)
{
"success": true,
"message": "Submission received.",
"id": "0193a1b2-7c3d-7e4f-8a9b-0c1d2e3f4a5b"
}Refused (HTTP 403 here)
{
"success": false,
"message": "This form doesn't accept submissions from this site.",
"id": null
}Accepted means queued for delivery: the email is sent a moment later, not before the answer. A submission that filled in the spam trap also gets a success answer, with id set to null, and nothing is kept.
Status codes
| Status | Message | Why |
|---|---|---|
| 200 | Submission received. | Accepted. A plain form post gets a 303 redirect instead. |
| 400 | Unreadable request. Send application/x-www-form-urlencoded, multipart/form-data or application/json. | The body couldn't be read as its content type, or the content type isn't one of the three. |
| 400 | Missing or malformed access_key. | There is no access_key field, or it doesn't look like a key. |
| 400 | Please complete the captcha. | The form requires a captcha and the token is missing, used already, or failed the check. |
| 400 | The submission has no fields. | Nothing was sent besides the special fields. |
| 400 | The submission has too many fields, or one is too long. | Over one of the limits. |
| 403 | This form doesn't accept submissions from this site. | The form has a list of allowed sites and the request's Origin isn't on it. |
| 404 | This form isn't accepting submissions. | No form has that key, or the form is paused. |
| 405 | Use POST. | Any method other than POST or OPTIONS. |
| 413 | That submission is too large. | The fields together are over 64 KB. |
| 429 | Too many submissions from your network. Please try again later. | The per-visitor rate limit. See Spam protection. |
| 429 | This form's account has reached its monthly submission limit. | The account's plan allowance for the month is used up. |
With file uploads, a post can also be refused with 400 ("This form doesn't accept file uploads", too many files, or a file type that isn't accepted), with 413 when files and fields together exceed 4 MB, and with 503 if the file couldn't be stored; send it again in that case. To send a file with fetch, pass new FormData(form) as the body and leave the Content-Type header to the browser.
Read success rather than the wording of message: the wording can change. A request that never reaches FormRelay, such as one made offline, makes fetch throw, so wrap the call in try and catch if you want to show your own message for that.
Cross-origin requests
- Any origin may call the submit address from a browser. The answer names the calling origin in
Access-Control-Allow-Origin. - What restricts a form to your site is its own list of allowed sites, under Settings. A request from another site is then answered with 403.
- The allowed methods are
POSTandOPTIONS. The allowed request headers areContent-TypeandAccept, so don't add custom headers. - A JSON body makes the browser send a preflight
OPTIONSrequest first. It is answered with 204. AFormDatabody needs no preflight. - No cookies or credentials are used.
Something missing or wrong? Email support@domodomu.com. New to FormRelay? See what it does or start free.