Skip to main content
It keeps the fields in state, posts them as JSON, and shows you formcarry’s response. That same request carries files, a spam blocker token, and validation errors. To try it yourself, the form’s Setup page in the dashboard opens a CodeSandbox with every example on this page wired to your endpoint.

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 send Accept: application/json so the answer comes back as JSON:
The keys of the JSON body are the field names formcarry stores. The 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 a FormData from the form element and leave the content type to the browser:
Every input in the form needs a 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:
For small files you can stick with JSON and send the file as a data URL. Read it with FileReader, and give each file its own key (never an array):
Use data URLs for small files. Using them with larger files bloats the request by about a third and reaches the 50 MB limit sooner.
Files are stored on paid plans only. Free plans store the rest of the submission without them.

Spam blocker

To add a challenge:
  1. Render the challenge in your component.
  2. Send its token in the request body under the name formcarry expects.
  3. Paste the secret key into the form’s settings under Form Security.
With reCAPTCHA v2:
v3 doesn’t have a widget: call 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.
Don’t send while the token is empty. formcarry answers 403 and the visitor sees an error they could have avoided.

Validation errors

A 422 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:
Leave 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

Stuck? Write to help@formcarry.com. Include the form id.