SvelteKit contact form

Formgong team ·

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

250 free submissions a month

How forms work in SvelteKit

SvelteKit's native way to handle forms is a form action in +page.server.js, often with use:enhance. Form actions need a server: pages with actions cannot be prerendered, so they are not available on sites built with adapter-static.

A form that posts to https://formgong.com/submit works on any SvelteKit page, prerendered or not. Use a plain form with method="POST" and the Formgong URL as action. Or send it with fetch in an onsubmit handler to show the answer in place.

Set it up in SvelteKit

  1. Create a form in Formgong and copy the access key.
  2. Add the component below to src/lib and use it in your page. A form with an outside action is sent by the browser; SvelteKit does not intercept it.
  3. If you keep the key in configuration, use a PUBLIC_ variable from $env/static/public; other variables never reach the browser.
  4. Run vite dev, send a test, then build with your adapter as usual.
SvelteKit
<script>
  let status = $state("");

  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 = result.message || (result.success ? "Sent. Thank you!" : "Could not send. Please try again.");
  }
</script>

<form onsubmit={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>

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

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

Formgong or a form action?

Use a form action when the submission belongs to your app: saving it to your database or checking the session.

Use Formgong for contact and lead forms, and always on prerendered or static sites, where form actions are not available.

Questions and answers

Can I use use:enhance with Formgong?

No. use:enhance works with SvelteKit form actions. For an in-page result, use fetch in the submit handler, as in the snippet.

Does it work with adapter-static?

Yes. Nothing runs on your server for the form.

Where do I put the access key?

In the component or in a PUBLIC_ environment variable. The key is public by design.

Start free