Prerequisites
Before you begin, you’ll need:- A formcarry account. Sign up is free.
- A form in the dashboard. Its endpoint is on the form’s Setup page. The examples use
https://formcarry.com/s/AbC123xyz; put yours in its place.
The component
To send a form from React, keep the fields in state, post them as JSON, and sendAccept: application/json so the answer comes back as JSON:
email key is the visitor’s address, so it becomes the reply-to address of your notification and the recipient of the auto response. A stored submission answers with code: 200. A refused one answers with the reason in message, see What every form needs.
Files
To send files, build aFormData from the form element and leave the content type to the browser:
name, because FormData reads them from the elements, and the file input needs one too. Send Accept and nothing else rather than adding Content-Type yourself, otherwise the multipart boundary is missing and the upload fails.
For several files, or for File objects that a dropzone library hands you, append each one under its own name:
FileReader, and give each file its own key (never an array):
Files are stored on paid plans only. Free plans store the rest of the submission without them.
Spam blocker
To add a challenge:- Render the challenge in your component.
- Send its token in the request body under the name formcarry expects.
- Paste the secret key into the form’s settings under Form Security.
With reCAPTCHA v2:
executeRecaptcha() when the visitor submits and send what it returns. hCaptcha and Turnstile give you the token through their onVerify and onSuccess callbacks. While testing, add localhost to the challenge’s allowed domains.
Validation errors
A422 carries errors, one entry per failing field, each with a message. To show each one next to its field:
Axios
The same request with Axios:Content-Type to Axios. Setting it yourself breaks the boundary on FormData uploads. Axios rejects on any status outside 2xx, so formcarry’s answer to a refused submission is in err.response.data.
What’s next
- Field validations: the rules you can set per field.
- Spam protection: the challenges, the filter and the honeypot.
- Email notifications: the auto response the visitor gets, keyed on
email.