Nuxt contact form

Formgong team ·

Paste this into Nuxt. Posts go to Formgong. No database and no Resend.

250 free submissions a month

How forms work in Nuxt

Nuxt can run code on the server, so the native way to handle a form is a server route under server/api that sends email. That needs a running server, so it is not available after nuxt generate, and you have to keep an email provider configured.

With Formgong the form posts from the browser and the server is not involved. That works with SSR, with static output from nuxt generate, and on any host.

Set it up in Nuxt

  1. Create a form in Formgong and copy the access key.
  2. Add the component below to components/. In Nuxt, send the request with $fetch (or plain fetch) inside the submit handler. useFetch is meant for loading data while the page renders, not for clicks.
  3. Put the key in runtimeConfig.public if you want it in configuration; only public runtime config reaches the browser.
  4. Run nuxt dev, send a test, then build or generate as usual.
Nuxt
<script setup>
import { ref } from "vue";

const status = ref("");

async function onSubmit(event) {
  event.preventDefault();
  const fields = Object.fromEntries(new FormData(event.currentTarget));
  const response = await fetch("https://formgong.com/submit", {
    method: "POST",
    headers: { "Content-Type": "application/json", Accept: "application/json" },
    body: JSON.stringify({ _lang: "en", ...fields, access_key: "fk_your_access_key" }),
  });
  const result = await response.json();
  status.value = result.message || (result.success ? "Sent. Thank you!" : "Could not send. Please try again.");
}
</script>

<template>
  <form @submit="onSubmit">
    <label>Name <input name="name" placeholder="Your name" required></label>
    <label>Email <input name="email" type="email" placeholder="you@example.com" required></label>
    <label>Message <textarea name="message" placeholder="How can we help?" required></textarea></label>
    <div aria-hidden="true" style="position:absolute;inset-inline-start:0;top:0;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)">
      <input name="botcheck" tabindex="-1" autocomplete="off">
    </div>
    <button type="submit">Send</button>
    <p role="status">{{ status }}</p>
  </form>
</template>

Component package: npm install @formgong/vue (npm: @formgong/vue)

Add to an existing project: npx formgong init (npm: formgong)

Formgong or a Nuxt server route?

Write a server route when the submission has to touch your own data or services.

Use Formgong for contact and lead forms, especially on statically generated sites where no server route exists.

Questions and answers

Why $fetch and not useFetch?

Nuxt's docs recommend useFetch for data needed while the page renders and $fetch for requests triggered by the user, such as sending a form.

Does it work with nuxt generate?

Yes. The form talks to Formgong directly, so a fully static build works.

Will SSR send the form twice?

No. The request runs only in the submit handler, which runs in the browser.

Start free