Stop form spam in React Router

Updated

This guide adds Bot403 to a contact form in React Router, in framework mode (the successor of Remix). The browser solves a small proof of work. Then the route's action checks the token with one call to https://bot403.com/api/v1/verify before it accepts the message. The visitor sees no puzzle, and the script sets no cookie. The free plan covers 5,000 checks a month and needs no card. The form works with <Form>, after a client-side navigation, and with no JavaScript.

The example uses react-router 8.5.1, React 19.3.1, Vite 8.3.8 and @bot403/node 1.0.2 on Node.js 24.22.1, from the default create-react-router template. On 12 November 2026 we ran a production build against a local stand-in for bot403.com. The stand-in served the real Bot403 browser script and its challenges, and gave the documented verify answers. In headless Chromium, a form opened by a client-side navigation got a token and went through, and the same token sent a second time was refused. With curl, a form without a token and a filled honeypot were refused. A 402 quota answer, an HTML error page and an API that did not answer let the form through. npm run typecheck passed.

Before you start

Sign in. Create a site. Copy its public key (pk_) and its secret key (sk_). Install the package, and put the secret key in an environment variable, never in your code or your repository:

Terminal
npm install @bot403/node
export BOT403_SECRET_KEY=sk_live_YOUR_SECRET_KEY

The files you add or change:

Layout
app/root.tsx               # the script tag
app/bot403.server.ts       # the client and the check
app/routes.ts              # the routes
app/routes/contact.tsx     # the form and the action
app/routes/thanks.tsx

1. The script tag

Put the script in the <head> of Layout in app/root.tsx. It then loads once and protects every form in the app, also forms that React renders after a client-side navigation. Before the browser sends a form, the script adds a hidden _bot403 field with the token, and a hidden honeypot field (_website by default).

app/root.tsx
export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
        <script src="https://bot403.com/v1/bot403.js" data-bot403-key="pk_live_YOUR_PUBLIC_KEY" async />
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

2. The check

app/bot403.server.ts
import { Bot403, Bot403Error } from "@bot403/node";

// Throws at startup when BOT403_SECRET_KEY is empty.
const bot403 = new Bot403({ secretKey: process.env.BOT403_SECRET_KEY ?? "", timeoutMs: 5000 });

/** Posts the whole form to Bot403. Returns true when the form may go through. */
export async function checkForm(form: FormData): Promise<boolean> {
  try {
    // 1. Bot403 reads the _bot403 field and the honeypot field.
    const result = await bot403.verifyForm(form);
    // 2. Refuse when ok is false, except for reason "quota".
    return result.ok || result.reason === "quota";
  } catch (err) {
    if (!(err instanceof Bot403Error)) throw err;
    if (err.status >= 400 && err.status < 500) {
      // 401 wrong key, 413 body too large, 429 too many calls
      console.error(`bot403 refused the verify call: ${err.code} (HTTP ${err.status})`);
      return false;
    }
    // No answer (status 0), or a 5xx page from a proxy: let the form through.
    console.error(`bot403 did not answer, letting the form through: ${err.message}`);
    return true;
  }
}

The .server in the file name tells React Router to keep this module out of the browser bundle. In the test build, no file in build/client contained the verify call or the variable name.

The client is made when the server starts. If BOT403_SECRET_KEY is empty, new Bot403() throws secretKey is required, and the server does not start. In the test run, react-router-serve stopped with that error and answered no request. You find a missing key at the deploy, and no form is ever accepted without a check.

3. The routes and the action

app/routes.ts
import { type RouteConfig, index, route } from "@react-router/dev/routes";

export default [
  index("routes/home.tsx"),
  route("contact", "routes/contact.tsx"),
  route("thanks", "routes/thanks.tsx"),
] satisfies RouteConfig;
app/routes/contact.tsx
import { Form, data, redirect } from "react-router";
import type { Route } from "./+types/contact";
import { checkForm } from "../bot403.server";

export async function action({ request }: Route.ActionArgs) {
  const form = await request.formData();
  if (!(await checkForm(form))) {
    return data({ error: "Send the form again." }, { status: 400 });
  }
  // A real visitor: send the email or store the message here
  return redirect("/thanks");
}

export default function Contact({ actionData }: Route.ComponentProps) {
  return (
    <Form method="post">
      <label>Email <input name="email" type="email" required /></label>
      <label>Message <textarea name="message" required /></label>
      <button type="submit">Send</button>
      {actionData?.error && <p role="alert">{actionData.error}</p>}
    </Form>
  );
}
app/routes/thanks.tsx
export default function Thanks() {
  return <p>Thanks. Your message is on its way.</p>;
}

The action reads the form with request.formData(), which handles a normal form post and multipart/form-data. verifyForm sends only the text fields, so a file upload never goes to Bot403. A refused check returns data(..., { status: 400 }), and the form shows the error from actionData.

With JavaScript, <Form> sends the form with fetch. In the test run, it posted to /contact.data, and the token arrived with the form. Without JavaScript, the browser posts to /contact, and the same action runs.

The trap: fetcher.submit with an object drops the token

Pass the form element to fetcher.submit, not an object built from its fields. The script puts the token in the _bot403 field of the form. An object that you build lists only the fields you name, so the token stays behind.

Your form's onSubmit
// Refused: the token is not in the object
fetcher.submit({ email, message }, { method: "post", action: "/contact" });

// Goes through: the whole form, with _bot403 and the honeypot field
fetcher.submit(event.currentTarget, { method: "post", action: "/contact" });

In the test run, both versions ran from an onSubmit handler. When the handler ran, the form's _bot403 field already held a 328-character token. The object version sent no token, and the action answered "Send the form again." with HTTP 400. The form-element version went to /thanks.

<fetcher.Form> works too, because it sends the whole form. The script listens to the submit event before your own handler. It waits for the token, puts it in the form, and then submits again, so your handler always finds the token in the form.

When Bot403 does not answer, or the key is wrong

verifyForm resolves for every token that fails, also for quota with HTTP 402. It throws a Bot403Error only when the call itself fails. checkForm sorts these by err.status:

  • 0, a network error or the 5-second timeout: the form goes through.
  • 500 to 599, for example an HTML error page from a proxy: the form goes through.
  • 400 to 499: the form is refused. That is 401 for a wrong key, 413 for a body over 64 KB, or 429 for too many verify calls in a minute (100 on the free plan).

Any other error is a bug, so checkForm throws it again instead of letting the form through.

In the test run, an API that waited 30 seconds let the form through after 5.0 seconds, with this log line:

Log
bot403 did not answer, letting the form through: @bot403/node: POST /api/v1/verify failed: This operation was aborted

A wrong key refused every form and logged:

Log
bot403 refused the verify call: unauthorized (HTTP 401)

Look for that line after a deploy. Every form fails until you fix the key.

Test it

Build the app, and start it with the key:

Terminal
npm run build
BOT403_SECRET_KEY=sk_live_YOUR_SECRET_KEY npm start

Send a form without a token, the way a simple bot does. The action must refuse it:

Terminal
curl -s -o /dev/null -w '%{http_code}\n' -d '[email protected]' -d 'message=Hi' http://localhost:3000/contact
# 400

Then open http://localhost:3000/contact in a browser and send the form. The browser goes to /thanks, and the site in your dashboard shows one check. If you get a failure that you do not expect, read the troubleshooting page. It lists every reason and its fix. For Next.js, read Stop form spam in Next.js. All guides are in the guides index.

Stop spam, not customers.

The CAPTCHA your visitors never see.

Get started free