# Human Check > Free, invisible bot protection by placeforhumans.org: no account, no keys, no tracking. Two ways to add it: a widget in a form, or a gate in front of the whole site. This guide is written for AI assistants that add Human Check to a project. Docs for humans: https://placeforhumans.org/en/human-check/docs ## Which way? - Protect a form (login, sign-up, contact, comment, checkout): part A, widget. - Protect the whole site before anyone sees it (like Cloudflare's "Just a moment …"): part B, gate. - Both together work too. ## A. Widget in a form ### 1. Script and widget ```html
``` `data-use` sets the recommendation of the verify endpoint: - `login`, step-up when unsure: Ask for a second factor or an email code - `signup`, step-up when unsure: Confirm the email address before the account is active - `comment`, step-up when unsure: Hold for review - `contact`, step-up when unsure: Accept, but mark as possible spam - `checkout`, step-up when unsure: Ask for an extra payment confirmation (3-D Secure) - `form`, step-up when unsure: Ask for an extra confirmation, e.g. by email The widget speaks the page's language (``); set it explicitly with `data-lang="en"` or `"de"`. For free text (comments, messages), also check that it was typed by hand: ```html
``` On submit, the widget adds the field `humancheck-token` to the form. For pages that submit with JavaScript: ```js const { token } = await humanCheck.getToken(); // optional: "#my-widget" await fetch("/api/contact", { method: "POST", body: JSON.stringify({ text, token }) }); ``` ### 2. Verify the token on your server (required) ```js const res = await fetch("https://placeforhumans.org/api/v1/verify", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ token: req.body["humancheck-token"], use: "login" }), }); const check = await res.json(); if (!check.success || check.hostname !== "your-site.com") { return res.status(403).send("Invalid check."); } const { action, stepUp, hint } = check.recommendation; if (action === "block") return res.status(403).send(hint); if (action === "step-up") return sendEmailCode(); // stepUp says what fits // "allow": continue as usual ``` PHP: ```php $res = file_get_contents("https://placeforhumans.org/api/v1/verify", false, stream_context_create(["http" => [ "method" => "POST", "header" => "Content-Type: application/x-www-form-urlencoded", "content" => http_build_query(["token" => $_POST["humancheck-token"] ?? "", "use" => "login"]), ]])); $check = json_decode($res, true); if (empty($check["success"]) || $check["hostname"] !== "your-site.com" || $check["recommendation"]["action"] === "block") { http_response_code(403); exit($check["recommendation"]["hint"] ?? "Invalid check."); } ``` The response contains `success`, `hostname`, `score`, `verdict` (`mensch` = human, `unsicher` = unsure, `bot`, `ausgeschlossen` = excluded) and `recommendation` with `action` (`allow`, `step-up` or `block`), `stepUp` (the fitting extra step) and `hint` (a note for the visitor). Pass `lang: "en"` to get English texts regardless of the widget. On `step-up`, implement the extra step instead of simply rejecting. ## B. Gate in front of the whole site 1. Normal mode: visitors get the page right away. humancheck.js checks in the background as soon as someone uses the page, and on success the server sets a signed cookie (default 7 days, then re-checked silently). 2. Only visitors who haven't passed after a few page views, or who don't request like a browser, see the short check page. When unsure, the visitor ticks the "I am human" box there once. 3. Attack mode: turns itself on during a surge. Then everyone without a cookie only gets the small check page. 4. Rate limit: more than 120 requests per minute and IP without a passed check get a 429. ### 1. Get the module ```sh # into the folder the template for your platform names curl -o humancheck-gate.ts https://placeforhumans.org/humancheck-gate.ts # without TypeScript: https://placeforhumans.org/humancheck-gate.js # secret for the cookie, e.g. as the environment variable HUMAN_CHECK_SECRET openssl rand -hex 32 ``` Don't change `humancheck-gate.ts`. Everything you need goes through the options. Without network access, the Human Check MCP server returns the source code with the tool `get_gate_module`. ### 2. Template for the platform #### Next.js (on Vercel or your own server) File: proxy.ts and humancheck-gate.ts next to app/ (or in src/). Up to Next.js 15, file and function are called middleware Set the secret: `vercel env add HUMAN_CHECK_SECRET` ```ts import { NextResponse, type NextRequest } from "next/server"; import { humanCheckGate } from "./humancheck-gate"; export function proxy(request: NextRequest) { return humanCheckGate(request, { secret: process.env.HUMAN_CHECK_SECRET! }, () => NextResponse.next()); } // Skip Next.js internals export const config = { matcher: ["/((?!_next/static|_next/image).*)"], }; ``` #### Vercel with another framework or plain HTML File: middleware.ts and humancheck-gate.ts in the project folder, next to package.json Set the secret: `vercel env add HUMAN_CHECK_SECRET` Needs once: npm install @vercel/functions ```ts import { next } from "@vercel/functions"; import { humanCheckGate } from "./humancheck-gate"; export default function middleware(request: Request) { return humanCheckGate(request, { secret: process.env.HUMAN_CHECK_SECRET! }, () => next()); } ``` #### Netlify File: netlify/edge-functions/humancheck.ts, humancheck-gate.ts in the project folder Set the secret: `netlify env:set HUMAN_CHECK_SECRET "$(openssl rand -hex 32)"` ```ts import type { Config, Context } from "@netlify/edge-functions"; import { humanCheckGate } from "../../humancheck-gate.ts"; export default (request: Request, context: Context) => humanCheckGate( request, { secret: Netlify.env.get("HUMAN_CHECK_SECRET")!, ip: () => context.ip }, () => context.next(), ); export const config: Config = { path: "/*" }; ``` #### Cloudflare Workers File: src/index.ts and src/humancheck-gate.ts, as a route in front of your domain or with static assets Set the secret: `npx wrangler secret put HUMAN_CHECK_SECRET` With static assets, in wrangler.jsonc: "assets": { "directory": "./public", "binding": "ASSETS", "run_worker_first": true } ```ts import { humanCheckGate } from "./humancheck-gate"; type Env = { HUMAN_CHECK_SECRET: string; ASSETS?: Fetcher }; export default { fetch(request: Request, env: Env): Promise { return humanCheckGate( request, { secret: env.HUMAN_CHECK_SECRET, ip: (r) => r.headers.get("cf-connecting-ip") }, () => (env.ASSETS ? env.ASSETS.fetch(request) : fetch(request)), ); }, }; ``` #### Cloudflare Pages File: functions/_middleware.ts, humancheck-gate.ts in the project folder Set the secret: `npx wrangler pages secret put HUMAN_CHECK_SECRET` ```ts import { humanCheckGate } from "../humancheck-gate"; export const onRequest: PagesFunction<{ HUMAN_CHECK_SECRET: string }> = (context) => humanCheckGate( context.request, { secret: context.env.HUMAN_CHECK_SECRET, ip: (r) => r.headers.get("cf-connecting-ip") }, () => context.next(), ); ``` Other servers with web-standard Request and Response (Deno, Bun, Hono): `return humanCheckGate(request, { secret }, () => servePage())`. ### 3. Script on every page So that the background check runs, add this once to the layout: ```html // Next.js: in app/layout.tsx import Script from "next/script";