Docs / Frameworks
Contact form for Nuxt
A Nuxt 3 component that posts with $fetch, shows the result inline, and still works as a plain HTML form before hydration.
<!-- components/ContactForm.vue (Nuxt 3): ref and $fetch are auto-imported -->
<script setup lang="ts">
const status = ref<"idle" | "sending" | "sent" | "error">("idle");
async function onSubmit(e: Event) {
const form = e.target as HTMLFormElement;
status.value = "sending";
try {
await $fetch("https://wrenform.com/f/YOUR_FORM_ID", {
method: "POST",
headers: { Accept: "application/json" },
body: new FormData(form),
});
status.value = "sent";
form.reset();
} catch {
status.value = "error";
}
}
</script>
<template>
<form action="https://wrenform.com/f/YOUR_FORM_ID" method="POST" @submit.prevent="onSubmit">
<label>Name <input type="text" name="name" autocomplete="name" required /></label>
<label>Email <input type="email" name="email" autocomplete="email" required /></label>
<label>Message <textarea name="message" required></textarea></label>
<input type="text" name="_honey" style="display:none" tabindex="-1" autocomplete="off" />
<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'">Something went wrong. Please try again.</span>
</p>
</form>
</template>Steps
- Create a form and copy its form ID, or let your AI tool create one without an account.
- Save the snippet as
components/ContactForm.vueand replaceYOUR_FORM_IDin both places. Nuxt auto-imports the component,refand$fetch, so it needs no import lines. - Use it on a page, for example
pages/contact.vue:
<template>
<main>
<h1>Contact</h1>
<ContactForm />
</main>
</template>- Deploy as usual (
nuxt buildornuxt generate). Then add your site's origin under Allowed origins in the form's settings.
No server route needed. You don't need a server/api handler or a mail provider: the visitor's browser posts straight to Wrenform. The form ID is public by design, so it can live in client code.
Works before hydration. The form keeps its action and method, so a visitor who submits before the page hydrates, or with JavaScript off, still sends the message as a plain HTML post. Set the form's Success URL to a thank-you page for that case (see redirects).
Keep the form ID in config
To set the ID once, add it to runtimeConfig.public and read it in the component:
export default defineNuxtConfig({
runtimeConfig: {
public: { wrenformId: "YOUR_FORM_ID" },
},
});Then, in the component, build the endpoint with const endpoint = "https://wrenform.com/f/" + useRuntimeConfig().public.wrenformId; and use it for both $fetch and :action. You can override it per environment with NUXT_PUBLIC_WRENFORM_ID.
Test it
Run npx nuxi dev, open /contact and send the form. You should see the thank-you line, the submission in your dashboard and an email. While testing locally, leave Allowed origins empty or add http://localhost:3000.
Common problems
- CORS error or
origin_not_allowed. Add the site's origin, includinghttps://, to the form's allowed origins. empty_submission. The inputs neednameattributes.v-modelalone doesn't add them.$fetchis not defined. The file is outside a Nuxt app (plain Vue). Use the Vue guide, which posts withfetch.- You see Wrenform's own thank-you page. The form posted before hydration. Set the form's Success URL to your own page.
- Strict CSP. Add
connect-src https://wrenform.comandform-action https://wrenform.com.