# How to send an HTML form to Discord

Discord channels have **webhooks**: a secret URL that posts a message to one channel when you send it JSON. That makes Discord an easy place to collect form submissions for a small team, a community or a side project. The form just can't call the webhook itself.

This guide covers the webhook, a small Cloudflare Worker that sits between your form and Discord, the Discord-specific traps, and a form backend that does it for you.

## 1\. Create a Discord webhook

1. Open your server's **Server Settings → Integrations → Webhooks**.
2. Create a webhook, name it ("Website forms") and pick the channel.
3. Click **Copy Webhook URL**. It looks like `https://discord.com/api/webhooks/123…/abc…`.

Anyone with that URL can post to your channel, so it never goes in your page source.

## 2\. Option A: a Cloudflare Worker

The form posts to your Worker:

```html
<form action="https://contact-to-discord.YOUR-SUBDOMAIN.workers.dev" method="POST">
  <input name="name" placeholder="Name" required>
  <input name="email" type="email" placeholder="Email" required>
  <textarea name="message" placeholder="Message" required></textarea>
  <!-- Honeypot: hidden from people, bots fill it in -->
  <div aria-hidden="true" style="position:absolute; left:-9999px">
    <input type="text" name="_gotcha" tabindex="-1" autocomplete="off">
  </div>
  <button type="submit">Send</button>
</form>
```

The Worker turns the fields into a Discord message:

```js
export default {
  async fetch(request, env) {
    if (request.method !== "POST") return new Response("Method not allowed", { status: 405 });

    const form = await request.formData();
    if (form.get("_gotcha")) return Response.redirect(env.THANKS_URL, 303);

    const name = String(form.get("name") ?? "").slice(0, 200);
    const email = String(form.get("email") ?? "").slice(0, 200);
    const message = String(form.get("message") ?? "").slice(0, 1500);
    if (!email || !message) return new Response("Email and message are required", { status: 400 });

    const webhookUrl = new URL(env.DISCORD_WEBHOOK_URL);
    // wait=true makes Discord confirm the message was saved before answering.
    webhookUrl.searchParams.set("wait", "true");

    const res = await fetch(webhookUrl, {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({
        content: `**New message from ${name}** (${email})\n${message}`,
        // Turn off mention parsing so visitor text can't ping users.
        allowed_mentions: { parse: [] },
      }),
    });
    if (!res.ok) return new Response("Couldn't send your message. Please try again.", { status: 502 });

    return Response.redirect(env.THANKS_URL, 303);
  },
};
```

Store the URL as a secret and deploy:

```bash
npx wrangler secret put DISCORD_WEBHOOK_URL
npx wrangler deploy
```

with `THANKS_URL` set as a variable in `wrangler.jsonc`.

### The Discord traps

- **Mentions.** Discord webhooks parse user mentions (`<@user_id>`) by default (unlike regular messages, `@everyone` and role mentions are off by default). Passing `allowed_mentions: { parse: [] }` turns off all mention parsing so visitor text can't ping anyone.
- **Unconfirmed writes.** Without `?wait=true`, Discord answers `204` before confirming the message was saved, so a failed save won't return an error. Adding `wait=true` makes the Worker wait for confirmation.
- **Length.** A message's `content` is capped at 2,000 characters, and Discord rejects longer ones with a 400. Trim the message (the code keeps 1,500 for the text, leaving room for the name and email), or the long submissions are the ones you lose.

### What this version doesn't do

- **Spam.** Bots that post straight to the Worker, and hand-written SEO pitches, still reach the channel. See [how to stop contact form spam](https://formkove.com/blog/stop-contact-form-spam/?ref=hashnode) for the layers.
- **Storage.** If the webhook is deleted or Discord is down, the submission is gone.
- **Rate limits.** One script can flood the channel.
- **Sorting.** Every message lands in the same channel.

## 3\. Option B: a form backend

With [Formkove](https://formkove.com/?ref=hashnode), the form posts to a Formkove URL instead of your own Worker:

```html
<form action="https://app.formkove.com/api/forms/YOUR_FORM_ID/submissions" method="POST">
```

Then, in the form's **Integrations → Discord**, paste the webhook URL from step 1 and click **Send test**. Each submission arrives as a message with the form name, the fields and, when AI is on, its sentiment and tags.

To send only some submissions to a channel, add a **routing rule** to the connection. For example, a `#leads` channel that only gets submissions tagged `sales-lead`, or a `#bugs` channel for submissions where the `topic` field is `bug`. Rules can test the tags and sentiment Formkove adds, the spam score, or any form field, and you can add one Discord connection per channel.

What comes with it:

- **Spam held back** before anything reaches Discord, kept in your dashboard for 30 days in case one was a real message.
- **Every submission saved and emailed to you**, even when Discord is down. Failed deliveries are retried and listed in **Delivery history**.
- **Rate limits** on every form, and mentions in submissions never ping your server.

Discord, tags and routing are on the Pro and Agency plans, and Free accounts get them on their [first 50 submissions](https://formkove.com/docs/limits/?ref=hashnode#your-first-50-submissions-on-free) ([pricing](https://formkove.com/pricing/?ref=hashnode)). Setup details are in the [Discord docs](https://formkove.com/docs/discord-integration/?ref=hashnode).

---

*Originally published on* [*formkove.com*](https://formkove.com/blog/html-form-to-discord/?ref=hashnode)*.*
