Add a Contact Form to Vue 3

Build a Vue 3 Composition API contact form that posts straight to W3Forms, no custom API needed. Get submissions by email and in a dashboard.

  1. Create a free W3Forms account and a form. Copy the access key shown once.
  2. Optional: in Form → Settings, list your site under Allowed domains, so posts from other sites are marked as spam. Include the port when testing locally (for example localhost:5500).
  3. Paste the markup below. Replace YOUR_ACCESS_KEY with your key.
  4. Submit a test. Check the dashboard inbox and your notification email.

Composition API + FormData keeps the example short. Put the access key in an env var (e.g. VITE_W3FORMS_ACCESS_KEY) for production builds.

ContactForm.vue
<script setup>
import { ref } from "vue";

const status = ref("");
const accessKey = "YOUR_ACCESS_KEY";

async function onSubmit(e) {
  e.preventDefault();
  status.value = "Sending…";
  const form = e.currentTarget;
  const res = await fetch("https://api.w3forms.com/submit", {
    method: "POST",
    headers: { "Content-Type": "application/json", Accept: "application/json" },
    body: JSON.stringify(Object.fromEntries(new FormData(form))),
  });
  const data = await res.json();
  if (res.ok && data.success) {
    status.value = "Thanks — your message was sent.";
    form.reset();
  } else {
    status.value = data.message || data.error || "Something went wrong.";
  }
}
</script>

<template>
  <form @submit="onSubmit">
    <input type="hidden" name="access_key" :value="accessKey" />
    <label for="name">Name</label>
    <input id="name" name="name" required />
    <label for="email">Email</label>
    <input id="email" type="email" name="email" required />
    <label for="message">Message</label>
    <textarea id="message" name="message" required />
    <input type="text" name="_gotcha" tabindex="-1" autocomplete="off"
           aria-hidden="true" style="position:absolute;left:-9999px" />
    <button type="submit">Send</button>
    <p role="status">{{ status }}</p>
  </form>
</template>

Next steps

Other stacks