Templates / APIs

Contact form backend: store messages and get an email

A POST endpoint for any site’s contact form: validates input, filters bots with a honeypot, saves the message and emails you. Works with Lovable and Framer.

Static sites and AI-built frontends still need somewhere to send a contact form. This endpoint accepts JSON or a classic form post, drops obvious bots, keeps every message in a table and emails you.

Build it with Wisp

Type this into Wisp, or press Build this — it writes the code below, deploys it and gives you a URL:

A POST /contact endpoint for my website form with name, email and message: validate them, ignore submissions where the hidden field "website" is filled, save to a messages table and email me via Resend (RESEND_API_KEY, NOTIFY_TO, NOTIFY_FROM).

The code

POST /contact · API

import { config, table } from 'wisp';

const esc = (s) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;');

export async function handler(req) {
  // Accept JSON (fetch) or a regular <form method="post">.
  const input = typeof req.body === 'string' ? Object.fromEntries(new URLSearchParams(req.body)) : (req.body ?? {});
  if (input.website) return { body: { ok: true } }; // honeypot: humans never fill the hidden field
  const name = String(input.name ?? '').trim().slice(0, 200);
  const email = String(input.email ?? '').trim().slice(0, 200);
  const message = String(input.message ?? '').trim().slice(0, 5000);
  if (!name || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) || message.length < 5) {
    return { status: 400, body: { error: 'Please fill in your name, a valid email and a message' } };
  }

  await table('messages').insert({ name, email, message, ip: req.ip });
  await fetch('https://api.resend.com/emails', {
    method: 'POST',
    headers: { authorization: `Bearer ${config.require('RESEND_API_KEY')}`, 'content-type': 'application/json' },
    body: JSON.stringify({
      from: config.require('NOTIFY_FROM'),
      to: config.require('NOTIFY_TO'),
      reply_to: email,
      subject: `Contact form: ${name}`,
      html: `<p><b>${esc(name)}</b> &lt;${esc(email)}&gt;</p><p>${esc(message).replace(/\n/g, '<br>')}</p>`,
    }),
  });
  // Classic form posts get sent to a thank-you page.
  if (typeof req.body === 'string' && input.redirect) return { status: 303, headers: { location: input.redirect } };
  return { body: { ok: true } };
}

Set it up

Add these in the project’s Variables & secrets (secrets are encrypted and never shown in logs):

Try it

curl -X POST https://<your-project-url>/contact \
  -H "content-type: application/json" \
  -d '{"name":"Ada","email":"ada@example.com","message":"Hello there"}'

In HTML: <form method="post" action="https://<your-project-url>/contact"> with a hidden <input name="website" style="display:none"> and, optionally, <input type="hidden" name="redirect" value="https://your-site.com/thanks">.

How it works

Variations

Questions

Will this work from a Lovable, Framer or Webflow site? Yes — any site can POST to the URL. CORS is handled, so fetch from the browser works.

Build it with Wisp — free trial

Related