Forms — Denzel Astro
The one form of this Astro template, the newsletter box in the footer of every page, works as soon as you run it.
It takes an e-mail address, checks that it looks like one, shows “…” on its button while sending, then “Thank you!” or “Something went wrong”, and clears the field. A hidden honeypot field drops spam.
Out of the box it runs in demo mode: nothing is sent, and the address is 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 sign-ups
Section titled “The quickest way to receive sign-ups”| You want | Mode | Hosting |
|---|---|---|
| An email for every sign-up, with no server to run | Static: a form service (Formspree, Web3Forms, Getform, Basin) | Any static host |
| Sign-ups in your mailing tool, a spreadsheet, Slack… | 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 sign up from the footer. -
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 email, with form (newsletter) and subject (“New newsletter subscriber”) next to it, as
multipart/form-data, and expects a 2xx answer. 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 sign-ups go withFORM_PROVIDER:You want Set The subscriber in a Mailchimp audience FORM_PROVIDER=mailchimp+MAILCHIMP_API_KEY,MAILCHIMP_AUDIENCE_ID;MAILCHIMP_STATUSis optionalAn email for every sign-up FORM_PROVIDER=resend+RESEND_API_KEY,FORM_EMAIL_TO,FORM_EMAIL_FROMIt in your mailing tool, Slack, a spreadsheet… FORM_PROVIDER=webhook+FORM_WEBHOOK_URL(Zapier, Make, n8n, Formspree, your own API);FORM_WEBHOOK_SECRETis optionalThe 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/newsletter(src/lib/forms/route.ts, added bysrc/integrations/forms.ts); every page stays static, only that route runs on the server, and it checks the address again. -
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 newsletter box (form newsletter, title “New newsletter subscriber”):
| Field | Name it arrives under | Required |
|---|---|---|
email |
yes, and it must look like an address |
In server mode, Resend sends the title as the email’s subject; Mailchimp adds or updates the subscriber in your audience and tags them “New newsletter subscriber”; a webhook receives the field with its label.
Changing the form
Section titled “Changing the form”The box’s words are content: footer.newsletter in src/content/site.json holds its title, the line above the field
(“Join 89,000 subscribers!”), the placeholder, the button (“Sign Up”), the two messages the button shows after sending
(success, error) and the note under it. The note says “By signing up, you agree to our Privacy Policy” but links
nowhere: write your own, and put your privacy page’s address in it if you have one.
The form’s definition (its fields, the required one, the e-mail check, the title of each sign-up) is
src/lib/forms/definitions.ts; the box is src/design-system/components/actions/NewsletterForm.astro and its script
src/design-system/scripts/newsletter.ts. Keep the field named email: Resend and Mailchimp use it.
Testing before launch
Section titled “Testing before launch”- Sign up from the footer of your own site after deploying, with a real address.
- Check the destination (inbox, spreadsheet, audience).
- 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 button says “Something went wrong”.
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 a provider with double opt-in (MAILCHIMP_STATUS=pending).
Privacy
Section titled “Privacy”Sign-ups go only where you point them; nothing is sent anywhere else. An e-mail address is personal data, and most countries expect you to say what happens to it: link your privacy policy from the note under the box, and choose a form service or provider that keeps the data where the law where you work allows. The box has no consent checkbox.
Mailchimp’s MAILCHIMP_STATUS decides what you may send: transactional (the default) adds contacts who can’t receive
campaigns, pending sends them a confirmation e-mail first (double opt-in), and subscribed is for addresses that gave
explicit consent. For a newsletter, pending is the usual choice.