For AI agents: the complete documentation index is available at /docs/llms.txt, the full documentation bundle is available at /docs/llms-full.txt, and this page is available as Markdown at /docs/recipes/hugo.md.

Hugo

A Hugo partial that renders a native HTML form. It needs no JavaScript: FormRoost answers the post with a redirect to your own thank-you or error page. Hosts and IDs below are placeholders.

Configure the form

In the form's sandbox draft:

  1. Add your dev origin, such as http://localhost:1313, to Allowed origins.
  2. Set Success redirect to an absolute URL such as http://localhost:1313/thanks/.
  3. Set Error redirect to an absolute URL such as http://localhost:1313/contact/error/.

On success FormRoost answers 303 to the success redirect. On a rejected submission it answers 303 to the error redirect with the problem code appended, for example ?error=submission_schema_invalid. Without redirects, visitors see a FormRoost result page instead.

Site config

# hugo.toml
[params.formroost]
endpoint = "https://formroost.com/s/v1/sandbox/fpub_REPLACE_ME"

Partial

{{/* layouts/partials/formroost-form.html */}} {{ with site.Params.formroost.endpoint }}
<form class="formroost" action="{{ . }}" method="post">
  <label>Email <input name="email" type="email" required /></label>
  <label>Message <textarea name="message" required></textarea></label>
  {{/* Honeypot: hidden from people, left empty by real visitors. */}}
  <div aria-hidden="true" style="position:absolute;left:-10000px">
    <label>Leave empty <input name="_gotcha" tabindex="-1" autocomplete="off" /></label>
  </div>
  <button type="submit">Send</button>
  <p class="formroost-status" role="status"></p>
</form>
{{ else }} {{ warnf "params.formroost.endpoint is not set" }} {{ end }}

Use it from any template with {{ partial "formroost-form.html" . }}, or wrap it in a shortcode for Markdown content.

Error page

The error page can explain the code. Codes are stable; see errors.

<!-- In the layout for /contact/error/; Hugo strips raw HTML from Markdown by default. -->
<p id="form-error">Your message could not be sent. Please go back and try again.</p>
<script>
  const code = new URLSearchParams(location.search).get('error')
  if (code === 'submission_schema_invalid')
    document.getElementById('form-error').textContent =
      'Some fields were missing or invalid. Please go back and check them.'
</script>

Optional progressive enhancement

This script keeps visitors on the page, sends JSON with credentials: 'omit', and adds _fr_started_at as a number so minimum-completion timing can apply. Without JavaScript the native post still works.

<script>
  for (const form of document.querySelectorAll('form.formroost')) {
    const startedAt = Date.now()
    const status = form.querySelector('.formroost-status')
    form.addEventListener('submit', async (event) => {
      event.preventDefault()
      status.textContent = 'Sending…'
      try {
        const response = await fetch(form.action, {
          method: 'POST',
          credentials: 'omit',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            ...Object.fromEntries(new FormData(form)),
            _fr_started_at: startedAt,
          }),
        })
        if (response.status === 201) {
          form.reset()
          status.textContent = 'Thanks, we received your message.'
          return
        }
        const problem = await response.json().catch(() => null)
        status.textContent =
          problem?.code === 'submission_schema_invalid'
            ? 'Please check your entries.'
            : 'Something went wrong. Please try again.'
      } catch {
        status.textContent = 'Network error. Please try again.'
      }
    })
  }
</script>

Go to production

Add your live HTTPS origin, set the redirects to your live URLs, and publish. Then change sandbox to production in hugo.toml. See browser submissions and configuration and publish.