Forms with Sanity
Integrating a form service is no different than integrating any external system with Sanity. That being said, integrating an external form service with Sanity most commonly falls into two buckets, you either want to:
- Author forms in an external service, then reference those forms by ID in your content (âI want my-marketing-form from MailChimp to go here on my pageâ), orâŚ
- Author forms inside Sanity, with the external service being used as a âbucketâ for all the form submissions on your site (services like Netlify Forms or Formspree)
There are other form use cases Sanity can cover, you could even use Sanity to collect form submissions and manage user generated content (like we do on this site and https://www.sanity.io/learn), but this guide will cover the above two most common use cases.
The plugin @sanity/form-toolkit offers pre-built tooling for both types of form integrations if youâre looking to have something âout of the boxâ. This guide will dive into a more general look at how form-toolkit goes about creating these integrations.
form-toolkit currently has integrations for these services:
- MailChimp
- HubSpot
form-toolkit also exposes the formSchema
plugin and FormRendering
React component, which provides a pre-built form schema for your Studio and a component to render those forms respectively.
Syncing external forms with Sanity typically assumes that the service youâre syncing has some way to embed forms on your front-end that expects an ID to know which form to render. In such cases @sanity/sanity-plugin-async-list allows you to add a string field to your Sanity documents that fetches data from a remote source. Our guide on syncing external data sources includes a section outlining this approach in detail.
Protip
@sanity/form-toolkit includes a package, formSchema
for building and rendering forms from your Sanity Studio
For basic form authoring, it may be preferred to author the form structure and fields in Sanity, pass that data to a component to render the form, and then use a âcatch-allâ service for form submissions like Formspree or Netlify forms.
If youâd prefer to build your own implementation instead of using form-toolkit, the process is the following:
- Model a typical form in Sanity schemas, including
- A
form
type containing an array offormField
objects - A
formField
type with various props for the HTMLinput
element liketype
,placeholder
,name
,required
,label
and others based on your needs - Additional properties on
form
orformField
based on your needs, perhaps your forms need multiple sections or you want to control the form action from the CMS, all can be built into your schema
- A
- Create a component that renders your form in your front-end framework of choice
- Take the
form
from a GROQ query, render a<form>
element, passing the relevant props from your data - Take the
fields
array, and return an appropriate input for each provided field and itstype
- Optionally, use a form package like TanStack Form or react-hook-form for better error and state management
- Take the
- Create logic for how your form handles and sends submissions.
- With some platforms like Netlify forms this means adding data attributes to the
<form>
element - In other cases like Formspree its adding their URL as the
action
attribute on your form
- With some platforms like Netlify forms this means adding data attributes to the