Forms — Compose Digi Astro
The contact form of this Astro template works as soon as you run it: “Send message”, on the Contact page.
It checks the first name, the last name and the e-mail (the required fields) and shows a message under each one that is
missing or wrong (“Please enter a valid email address.”), shows a spinner on its button while sending, says “Thank you”
and clears the fields, keeps what the visitor typed when something fails (“Something went wrong”), and drops spam with a
hidden honeypot field. It is the form contact.
Out of the box it runs in demo mode: nothing is sent, and the fields are logged in the browser console. That’s fine while you build, but useless once you publish, so pick a destination before launch.
The quickest way to receive submissions
Section titled “The quickest way to receive submissions”| You want | Mode | Hosting |
|---|---|---|
| An email for every message, with no server to run | Static: a form service (Formspree, Web3Forms, Getform, Basin) | Any static host |
| Messages in your CRM, Slack, a spreadsheet… | Static: a webhook (Zapier, Make, n8n, Pipedream) | Any static host |
| Resend or Mailchimp directly, with no third party in between | Server: the site’s own route (below) | A host with an Astro adapter |
Most sites want static mode: it keeps the site 100% static and takes two minutes.
-
Create a form at your service and copy its endpoint URL (Formspree:
https://formspree.io/f/<id>). -
Copy the example settings and paste the URL:
Terminal window cp .env.example .envPUBLIC_FORM_ENDPOINT=https://formspree.io/f/abcdwxyzWeb3Forms also needs
PUBLIC_FORM_ACCESS_KEY. -
Restart
npm run devand send a test from the Contact page. -
If your host builds the site, put the same variable in its dashboard before deploying:
.envstays on your computer (it is not in Git). The value is written into the pages when the site builds, so rebuild after changing it.
The endpoint is public (it is in the page), which is how these services are designed to work; they filter spam on
their side. The form posts its fields with form (contact) and subject (the submission’s title, “New contact
request”) next to them. Details for each service are in Forms reference.
Server mode
Section titled “Server mode”For Resend or Mailchimp without a third-party service, the form posts to a route on your own site:
-
Add the adapter for your host:
npx astro add vercel(ornetlify,node, …). -
Set
PUBLIC_FORM_MODE=server, and choose where submissions go withFORM_PROVIDER:You want Set An email for every submission FORM_PROVIDER=resend+RESEND_API_KEY,FORM_EMAIL_TO,FORM_EMAIL_FROMIt in your CRM, Slack, a spreadsheet… FORM_PROVIDER=webhook+FORM_WEBHOOK_URL(Zapier, Make, n8n, Formspree, your own API);FORM_WEBHOOK_SECRETis optionalThe sender in a Mailchimp audience FORM_PROVIDER=mailchimp+MAILCHIMP_API_KEY,MAILCHIMP_AUDIENCE_IDThe server log only, while you test FORM_PROVIDER=console(the default)Several at once work:
FORM_PROVIDER=resend,mailchimp. -
Restart
npm run devand send a test. The form now posts to/api/forms/contact(src/lib/forms/route.ts, added bysrc/integrations/forms.ts); every page stays static, only that route runs on the server. -
Put the same variables in your host’s dashboard before deploying, and redeploy after changing them.
Details for each provider, what it receives, the webhook payload, Mailchimp consent settings and how to write your own provider are in Forms reference.
What the form asks for
Section titled “What the form asks for”The contact form (form contact, button “Send message”, title “New contact request”):
| Field | Name it arrives under | Required |
|---|---|---|
| First Name | first-name |
yes |
| Last Name | last-name |
yes |
email |
yes | |
| Phone Number | phone |
|
| Message | message |
|
| Services (a round checkbox per service) | services, once for each ticked box |
Each field has its label above it and a placeholder inside (“First name”, “your@company.com”…). The services are the
names of the services in src/content/collections/services.json, in their order: add, rename or remove a service and the
checkboxes follow. In server mode, Resend puts the sender’s first and last name after the title in the email’s subject
(“New contact request — Jane Smith”) and sets the reply address to the sender’s e-mail; Mailchimp stores the names as
FNAME and LNAME, tags the contact with the title and keeps the rest as a note; a webhook receives every field of the
form ("" when left empty, services as a list) with its label.
Changing the form
Section titled “Changing the form”The labels, placeholders, error messages, the button’s three texts (“Send message”, “Thank you” and “Something went
wrong”) and the title of each submission (notification) are content: form in src/content/pages/contact.json. The
title and the lead above the form are title (two lines) and lead in the same file. A field arrives at your service
under its name, so keep the names you already map somewhere.
The form sends every field it shows, and in server mode src/lib/forms/definitions.ts reads the same list to check the
required ones again, so the list in the copy is the place to edit:
-
To make a field required, set
"required": trueon it and give it anerrormessage (to require the phone number, for example). The browser checks it before sending and, in server mode, the route checks it again. No asterisk is shown: say it in the label if you want to. A required field must be on the page: in server mode, a required field that is only in the list makes every submission fail. -
To make a field optional, remove its
"required": true. Keep the e-mail: Resend and Mailchimp use it. -
To add a field, add an entry to
form.fields(mind the comma between entries), for example a budget:"budget": { "name": "budget", "label": "Budget", "placeholder": "$5,000 a month", "type": "text" }The types are
text(the default),email,tel,textareaandcheckboxes(withoptions). The fields are laid out by hand, so also place it in the form, insrc/components/sections/ContactSection.astro: add<FormField {...fields.budget} />where it should go, and addbudget: FormFieldData;to thefieldsof theContactFormtype at the top of that file. -
To remove a field, delete its entry, its
<FormField … />line and its line in theContactFormtype. Keepemail: Resend and Mailchimp use it. -
Don’t give a real field the name
fax: it is the honeypot’s (Spam).
Testing before launch
Section titled “Testing before launch”- Submit the form on your own site after deploying, with a real address.
- Check the destination (inbox, spreadsheet, audience) and reply to the test.
- Try a failure on purpose: point
PUBLIC_FORM_ENDPOINTat a wrong URL in a test build (or set a wrong API key in server mode), submit, and confirm the visitor sees “Something went wrong” and their text is still there.
The honeypot stops the usual bots: the form carries a hidden field named fax that a person never sees; a bot that
fills it is shown “Thank you” and nothing is sent. If you start getting spam anyway, use a form service in static mode
(Formspree, Web3Forms and Basin all include spam filtering), or add a captcha to the form component
(src/design-system/components/forms/Form.astro).
Privacy
Section titled “Privacy”Submissions go only where you point them; nothing is sent anywhere else. A message carries a person’s name, e-mail and
phone, and whatever they write about their project. Most countries expect you to say what happens to that data: the
template has no privacy page, so add one (Pages and sections) and link it from the footer, and choose a
form service or provider that keeps the data where the law where you work allows. Compose has no consent checkbox: add one
(a checkboxes field, as above) if you need it.
The form doesn’t ask for marketing consent, so if you send it to Mailchimp, keep MAILCHIMP_STATUS at transactional
(the default): those contacts can get your replies, not your campaigns.