# How to send an HTML form to Slack

Slack can't receive a form post on its own. It gives you an **incoming webhook**: a secret URL that posts a message to one channel when you send it JSON. Your form needs something in between that turns the form fields into that JSON and keeps the URL out of your page.

This guide builds that in-between step two ways: a small Cloudflare Worker you run yourself, then a form backend that does it for you and sends each submission to the right channel.

## 1. Create a Slack incoming webhook

1. Go to [api.slack.com/apps](https://api.slack.com/apps), click **Create New App**, then **From scratch**.
2. Name it (for example "Website forms") and pick your workspace.
3. Open **Incoming Webhooks**, switch it on, and click **Add New Webhook**.
4. Pick the channel and copy the URL. It looks like `https://hooks.slack.com/services/T000/B000/XXXX`.

Treat that URL as a password. Anyone who has it can post to your channel.

## Why the form can't post to Slack directly

It's tempting to `fetch()` the webhook from the browser. Two problems:

- **The URL ends up in your page source.** Anyone can copy it and post whatever they like to your channel, as often as they like.
- **Slack expects a message**, a JSON body with a `text` field, not your form's `name`, `email` and `message` fields. A plain `<form action>` pointed at the webhook doesn't produce one.

So the form posts to your own endpoint, and that endpoint calls Slack.

## 2. Option A: a Cloudflare Worker

A Worker is free for this volume and needs no server. The form:

```html
<form action="https://contact-to-slack.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:

```js
// Slack reads <, > and & as markup: <!channel> would ping everyone.
const esc = (s) => String(s ?? "").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");

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

    const form = await request.formData();
    // Honeypot filled in: pretend it worked so the bot learns nothing.
    if (form.get("_gotcha")) return Response.redirect(env.THANKS_URL, 303);

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

    const res = await fetch(env.SLACK_WEBHOOK_URL, {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ text: `*New message from ${name}* (${email})\n>${message.replace(/\n/g, "\n>")}` }),
    });
    if (!res.ok) return new Response("Couldn't send your message. Please try again.", { status: 502 });

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

Keep the webhook URL out of the code:

```bash
npx wrangler secret put SLACK_WEBHOOK_URL
```

and set `THANKS_URL` (your thank-you page) as a variable in `wrangler.jsonc`, then `npx wrangler deploy`.

Two details in that code are easy to miss:

- **Escaping.** Slack treats `<`, `>` and `&` as control characters. Without escaping, a visitor who types `<!channel>` pings your whole channel.
- **A 303 redirect** sends the browser to your thank-you page with a GET, so refreshing it doesn't post the form twice.

### What this version doesn't do

It works, and for a personal site it may be all you need. What it leaves out:

- **Spam.** The honeypot stops simple bots. Scripts that post straight to the Worker, and people pasting SEO pitches, still land in Slack. Adding [Turnstile](https://formkove.com/blog/stop-contact-form-spam/?ref=hashnode) helps with the first; the second needs something that reads the message.
- **Storage.** If Slack is down or the webhook was revoked, the submission is gone.
- **Rate limits.** Nothing stops one script from posting a thousand messages into your channel.
- **Sorting.** Every message goes to the same channel. Sending sales leads to `#sales` and support requests to `#support` means writing your own rules, and deciding which is which.

## 3. Option B: a form backend that routes by what the message says

With [Formkove](https://formkove.com/?ref=hashnode), the form posts to a Formkove URL. Each submission is saved, checked for spam, emailed to you, and posted to Slack, and rules decide which channel gets which submission.

**Point the form at Formkove.** Create a form at [app.formkove.com](https://app.formkove.com/signup?ref=hashnode) and use its endpoint as the action. The `_gotcha` honeypot above is on by default.

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

**Connect Slack.** Create the incoming webhook from step 1, then in the form's **Integrations → Slack**, paste the URL and save. Click **Send test** to check it.

**Describe your tags.** On the form's **AI & Spam** page, add tags in plain words:

| Tag | Description |
|---|---|
| `sales-lead` | Wants a quote, a price or to hire us |
| `support` | An existing customer with a problem |

Formkove classifies each submission before delivery: spam is held back, and the tags that fit are added along with a sentiment.

**Route each channel.** Add a Slack connection per channel, each with its own webhook URL and a routing rule:

- `#sales`: **Tag** has any of `sales-lead`
- `#support`: **Tag** has any of `support`, **or** **Sentiment** is any of `negative`

Rules can also test any form field (for example `budget` is greater than `5000`), and you can combine conditions with **all** or **any**. A connection without rules gets every submission that isn't spam. Email isn't routed: you get every real submission by email whatever the rules say.

What you get on top of the Worker version:

- **Spam held back before Slack sees it.** It waits in your dashboard for 30 days, and **Not spam, deliver** sends a false positive on to its channel.
- **Every submission saved**, even when Slack is down. Failed deliveries are retried and show up in **Delivery history**.
- **Rate limits** on every form: 10 a minute and 30 an hour per IP address.

Slack, routing and tags 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)). The setup is in the [Slack docs](https://formkove.com/docs/slack-integration/?ref=hashnode) and the [routing rules docs](https://formkove.com/docs/zapier-and-webhooks/?ref=hashnode#routing-rules).

---

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


