Stop form spam in Nuxt

Updated

This guide adds Bot403 to a contact form in Nuxt. The browser solves a small proof of work. Then a server route 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 and without JavaScript, and after a client-side navigation.

The example uses Nuxt 4.6.4 (Nitro 2.13.6, Vue 3.5.45) and @bot403/node 1.0.2 on Node.js 24.22.1. 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 and a form loaded straight from the server both got a token and went through. 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.

Before you start

Sign in. Create a site. Copy its public key (pk_) and its secret key (sk_). Install the package:

Terminal
npm install @bot403/node

Put the secret key in an environment variable named NUXT_BOT403_SECRET_KEY, on the server and in .env for nuxt dev. Never put it in your code or your repository. The name matters: see the trap below.

.env
# .env (Git ignores it in a new Nuxt app)
NUXT_BOT403_SECRET_KEY=sk_live_YOUR_SECRET_KEY

The files you add or change:

Layout
nuxt.config.ts                 # the script tag and the runtime config
server/utils/bot403.ts         # the client
server/api/contact.post.ts     # the check
app/pages/contact.vue          # the form
app/pages/thanks.vue

1. The script tag and the runtime config

Put the script in app.head. It then loads on every page and protects every form in the app, also forms that Vue 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).

nuxt.config.ts
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },
  app: {
    head: {
      script: [
        { src: 'https://bot403.com/v1/bot403.js', 'data-bot403-key': 'pk_live_YOUR_PUBLIC_KEY', async: true },
      ],
    },
  },
  runtimeConfig: {
    // Empty here. Set NUXT_BOT403_SECRET_KEY on the server when it runs.
    bot403SecretKey: '',
  },
})

In the page head, Nuxt renders it as <script src="https://bot403.com/v1/bot403.js" data-bot403-key="pk_live_YOUR_PUBLIC_KEY" async></script>. The public key can sit in the config: it is public. The secret key stays empty in the file, and Nuxt fills it at run time.

2. The client

server/utils/bot403.ts
import { Bot403 } from '@bot403/node'

let client: Bot403 | undefined

// One client for the server, made on the first request, when the runtime config is known.
export function useBot403(): Bot403 {
  client ??= new Bot403({ secretKey: useRuntimeConfig().bot403SecretKey, timeoutMs: 5000 })
  return client
}

Files in server/utils are auto-imported in server routes. The client is made on the first request and kept for the life of the server. timeoutMs: 5000 stops the call after 5 seconds. The default is 10 seconds.

3. The route

server/api/contact.post.ts
import { Bot403Error } from '@bot403/node'

export default defineEventHandler(async (event) => {
  const form = await readFormData(event)

  // 1. Post the whole form. Bot403 reads the _bot403 field and the honeypot field.
  let ok: boolean
  try {
    const result = await useBot403().verifyForm(form)
    // 2. Refuse when ok is false, except for reason "quota".
    ok = result.ok || result.reason === 'quota'
  } catch (err) {
    // Any other error, such as a missing secret key, is a bug: let it fail.
    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})`)
      ok = false
    } else {
      // 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}`)
      ok = true
    }
  }
  if (!ok) return sendRedirect(event, '/contact?error=1', 303)

  // A real visitor: send the email or store the message here
  return sendRedirect(event, '/thanks', 303)
})

readFormData reads both a normal form post and a multipart/form-data body. verifyForm sends only the text fields, so a file upload never goes to Bot403. In the test run, a form with a file sent only email, message and _bot403.

The route answers with a 303 redirect, so a form without JavaScript works too. After a refused check, the visitor goes back to the form with ?error=1.

4. The pages

app/pages/contact.vue
<script setup lang="ts">
const route = useRoute()
</script>

<template>
  <form method="post" action="/api/contact">
    <label>Email <input name="email" type="email" required></label>
    <label>Message <textarea name="message" required /></label>
    <button type="submit">Send</button>
    <p v-if="route.query.error" role="alert">Send the form again.</p>
  </form>
</template>
app/pages/thanks.vue
<template>
  <p>Thanks. Your message is on its way.</p>
</template>

The form is a normal HTML form that posts to /api/contact. You need no @submit handler and no $fetch.

The trap: the secret key needs the NUXT_ prefix

Name the variable NUXT_BOT403_SECRET_KEY, not BOT403_SECRET_KEY. Nuxt fills runtimeConfig.bot403SecretKey at run time only from a variable with the NUXT_ prefix and the key in upper snake case. With BOT403_SECRET_KEY, the key stays empty.

In the test run, the server started with BOT403_SECRET_KEY=sk_live_… and no NUXT_ variable. The first form got HTTP 500, and the log said:

Log
TypeError: @bot403/node: secretKey is required

That 500 is what you want. The first version of this route caught every error and let the form through, as it does when Bot403 does not answer. With the wrong variable name, that version let every form through with no check at all, and nothing looked wrong. So the route catches only Bot403Error, and lets any other error fail.

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. The route 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).

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
npx nuxt build
NUXT_BOT403_SECRET_KEY=sk_live_YOUR_SECRET_KEY node .output/server/index.mjs

Send a form without a token, the way a simple bot does. It must go back to the form:

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

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 another Vite-based framework, read Stop form spam in SvelteKit. All guides are in the guides index.

Stop spam, not customers.

The CAPTCHA your visitors never see.

Get started free