JavaScript
Send a form with JavaScript to keep the visitor on the page: the endpoint of the form then answers with JSON rather than redirecting.
Sending a form
Keep the form as for an HTML form: its action is the endpoint, its method is POST, and every field has a name. Then catch its submission, and send the values as FormData with a POST request to the same endpoint. The endpoint answers with JSON when the request doesn't ask for HTML, as fetch does by default.
contact.html
<form id="contact" action="https://submit.backmyform.com/f/k3x9q2mf" method="POST">
<input type="email" name="email" required />
<textarea name="message"></textarea>
<button type="submit">Send</button>
</form>contact.js
const form = document.querySelector('#contact');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const response = await fetch(form.action, {
method: 'POST',
body: new FormData(form),
headers: { Accept: 'application/json' }
});
if (response.ok) {
form.replaceWith('Thank you! We will get back to you soon.');
} else {
const error = await response.json();
alert(error.message);
}
});new FormData(form) holds the files of the form too: the request is then sent as multipart/form-data, as needed.
Keep the action on the form
Even when the form is sent by your code, keep its action on the endpoint:
- the forms.js script finds the BackMyForm forms by their
action: without it, the form is ignored, and you get no time to respond, no not finished submission and no partial submission; - the script adds a hidden field to the form: build the
FormDatafrom the form when it is sent, as above, so that the field goes with it; - when your JavaScript fails or is blocked, the browser still sends the form to the endpoint.
The same goes for the frameworks (React, Vue, Svelte, Astro...): render a real <form> with its action, method and the name of its fields, and send new FormData(form) from its submit handler. Show the badge under the form too: it is required on the free plan.
The endpoint doesn't accept JSON bodies: send FormData or URL-encoded values. No API key is needed, and none must be sent: the endpoint is public.
The response
A received submission is answered with a 201 status and its id:
Response
{ "submissionId": "1f0b2c4f-3c4d-6e5f-9a0b-1c2d3e4f5a6b" }The redirect URL of the form is not used: show your own message.
Errors
A refused submission is answered with an error, in the format of the API. The code tells why:
| Status | Code | Meaning |
|---|---|---|
400 | invalid_resource | The submission is too large, or a field name or a value is too long. See the limits. |
404 | resource_missing | The form doesn't exist, or is disabled. |
413 | file_too_large | A file is over the size limit; data.maxSize gives it in bytes, when it can be shown. |
413 | too_many_files | The submission has more files than the form accepts; data.maxFiles gives the limit. |
415 | file_type_not_allowed | The type of a file is not accepted by the form. |
415 | files_not_accepted | The form doesn't accept files. |
415 | unsupported_media_type | The body is not FormData nor URL-encoded. |
429 | too_many_requests | Too many submissions from the visitor: wait data.retryAfter seconds. |
From a server
The endpoint accepts submissions sent from a server too, for example to forward a form your backend already receives. The submission then has no origin.
curl https://submit.backmyform.com/f/k3x9q2mf \
-H "Accept: application/json" \
-d name="John Smith" \
-d [email protected] \
-d message="Hello!"Send a submission from a server
The limits are per IP address: a server sending the submissions of many visitors reaches them sooner than the visitors would.