react-bot-detection

@tsdevstack/react-bot-detection is a React library for client-side bot detection using behavioral analysis and honeypot fields. Zero dependencies beyond React, SSR-safe, fully typed.

npm install @tsdevstack/react-bot-detection

Quick start

import { BotProtectedForm } from '@tsdevstack/react-bot-detection';

function ContactForm() {
  const handleSubmit = async (formData: FormData, botResult) => {
    await fetch('/api/contact', {
      method: 'POST',
      body: JSON.stringify({
        email: formData.get('email'),
        botScore: botResult.score,
        isBot: botResult.isBot,
      }),
    });
  };

  return (
    <BotProtectedForm onSubmit={handleSubmit}>
      <input name="email" type="email" placeholder="Email" />
      <textarea name="message" placeholder="Message" />
    </BotProtectedForm>
  );
}

Exports

ExportTypeDescription
BotProtectedFormComponentDrop-in form wrapper with behavioral analysis + honeypot
useBotDetection()HookBehavioral analysis for custom form implementations
useHoneypot()HookHoneypot-based detection for custom forms
HoneypotComponentStandalone honeypot field

Detection methods

Behavioral analysis (useBotDetection)

Tracks mouse movements, typing patterns, focus events, and time on form. Scores each signal:

SignalScoreDescription
No mouse movement+30Bots often skip mouse events
Unnatural mouse patterns+20Perfectly straight lines
Consistent typing speed+15Humans have variable rhythm
Superhuman typing+20< 50ms between keystrokes
Fast form completion+40< 2 seconds total
Few focus events+15< 2 field interactions
WebDriver detected+25navigator.webdriver === true
Headless browser+35Window size is 0

Threshold: Score >= 50 is classified as a bot.

Honeypot (useHoneypot)

Hidden fields invisible to humans but visible to bots that parse HTML. When filled, adds +100 to score.

BotProtectedForm props

PropTypeDefaultDescription
onSubmit(data, result) => Promise<void>requiredCalled on submission
onBotDetected(result) => void—Called when bot detected
submitButtonTextstring"Submit"Button label
loadingButtonTextstring"Processing"Loading state label
ButtonComponentComponentType—Custom button for design system integration
showDebugPanelbooleanfalseShow detection stats (dev only)

Best practices

  1. Always validate server-side — client-side detection can be bypassed. Send the botScore to your backend and verify there.
  2. Don't reveal detection — silently accept bot submissions but don't process them. This prevents bots from learning your detection methods.
  3. Combine with rate limiting — bot detection is one layer. Also use IP-based rate limiting and email verification.
  4. Use the debug panel in development: showDebugPanel={process.env.NODE_ENV === 'development'}