Vue contact form to email

A Vue 3 single-file component posts the form from the browser to FormRoute. It also works inside Nuxt pages, because the request is made only after the component is mounted in the browser.

You need a FormRoute account with a verified domain and a form whose fields are name, email and message, or change the names below to match yours. Getting started covers the setup.

1. Add your site key

With Vite, public variables start with VITE_. In Nuxt, read the key from your public runtime config instead.

.env
VITE_FORMROUTE_SITE_KEY=fr_your_site_key

2. Create the component

The name attributes must match the fields you set up in FormRoute.

src/components/ContactForm.vue
<script setup>
import { onMounted, ref } from "vue";

const ENDPOINT = "https://form-route.com/api/v1/send";
const SITE_KEY = import.meta.env.VITE_FORMROUTE_SITE_KEY;

const status = ref("idle");
let shownAt = 0;

onMounted(() => {
  shownAt = Date.now();
});

async function onSubmit(event) {
  const form = event.target;
  const data = new FormData(form);
  status.value = "sending";

  try {
    const response = await fetch(ENDPOINT, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        siteKey: SITE_KEY,
        t: Date.now() - shownAt,
        hp: data.get("hp") ?? "",
        name: data.get("name"),
        email: data.get("email"),
        message: data.get("message"),
      }),
    });

    if (response.ok) {
      form.reset();
      status.value = "sent";
    } else {
      status.value = "error";
    }
  } catch {
    status.value = "error";
  }
}
</script>

<template>
  <form @submit.prevent="onSubmit">
    <label>Name <input name="name" required maxlength="120" /></label>
    <label>Email <input name="email" type="email" required maxlength="254" /></label>
    <label>Message <textarea name="message" required maxlength="5000"></textarea></label>

    <!-- Honeypot: hidden from people, must stay empty -->
    <div aria-hidden="true" style="position: absolute; left: -10000px">
      <label>Leave this empty <input name="hp" tabindex="-1" autocomplete="off" /></label>
    </div>

    <button type="submit" :disabled="status === 'sending'">Send</button>

    <p role="status">
      <span v-if="status === 'sent'">Thanks, your message was sent.</span>
      <span v-if="status === 'error'">Sorry, that didn't go through. Please try again.</span>
    </p>
  </form>
</template>

3. List your website in FormRoute

In your FormRoute account, open your form and add every address the form is served from to Allowed websites, for example https://www.example.com and, while you develop, http://localhost:5173. Each entry is a scheme and host, with a port only if your site uses one.

A browser submission from an address that isn't listed is refused, and your account shows it as Rejected: website not allowed. For local development, use the exact address your dev server prints.

4. Send a test

Open the page, fill in the form and send it. The submission appears on your FormRoute home page within seconds, as Sent or with the reason it was refused, and the email arrives from your own domain.

Fill the form in at a human pace: a submission sent within 1.2 seconds of the form appearing is treated as automated.

Good to know

  • Deploying? Set the same environment variable in your host's settings (for example your Vercel or Netlify project): .env files on your machine are not deployed. Or put the key in the component as a constant; it is public by design.

More guides

HTML · Next.js · React · Astro · WordPress · Webflow · Framer · Hugo, Jekyll and Eleventy