Bot protection for Next.js App Router
Install @centinel/nextjs to check matched routes or single Route Handlers, wherever your Next.js app is hosted.
How it fits into Next.js
- Package and requirements
- @centinel/nextjs for Next.js 13+ with the App Router, React 18+ and Node.js 18+.
- Two enforcement modes
- createCentinelMiddlewareFromEnv() across matched routes, or isBot(request) inside specific Route Handlers.
- Browser script
- Wrap app/layout.tsx in <CentinelLayout siteKey={...}> to add browser signals to server validation.
- Response routes
- Blocked visitors go to /block and challenged visitors to /interstitial. Both pages live in your app.
How a request moves through Next.js
The middleware runs on matched routes before your page or Route Handler, so blocked requests never reach your code.
Matcher
Next.js runs middleware.ts, or proxy.ts on Next.js 16, only for paths in config.matcher, such as /api/:path* and /dashboard/:path*.
Validator call
The Centinel handler sends the request to the validator with CENTINEL_SECRET_KEY and waits up to 500 ms.
Decision
result.response continues the request on allow, redirects to /block on block and to /interstitial on challenge.
Failure
Timeouts and errors fail open. After repeated failures the middleware backs off from 1 s up to 5 minutes.
Minimal configuration and a live check
Protect matched routes
import type { NextRequest } from 'next/server';
import { createCentinelMiddlewareFromEnv } from '@centinel/nextjs';
const centinel = createCentinelMiddlewareFromEnv();
export default async function middleware(request: NextRequest) {
const result = await centinel(request);
return result.response;
}
export const config = {
matcher: ['/api/:path*', '/dashboard/:path*']
};List only the routes you want checked in config.matcher. Unmatched routes and static files never reach the validator.
Run the dev server with debug output
DEBUG=centinel:* next devLoad a protected route and look for Validation completed in Nms. Validator service responded with 401 means the secret key is wrong and requests fail open. In DevTools, confirm a _centinel cookie and the collector script.js request.
Deployment options
Middleware, or proxy.ts on Next.js 16
Use createCentinelMiddlewareFromEnv() with a route matcher and return result.response on every matched request.
Next.js middleware guide (opens in a new tab)Manual validation in Route Handlers
Call createRequestValidatorFromEnv().isBot(request) inside handlers such as app/api/login/route.ts when you want your own JSON error response.
Manual validation tab in the Next.js guide (opens in a new tab)
What to know before deploying
Next.js 16 ignores middleware.ts
Next.js 16 replaced middleware.ts with proxy.ts, exporting proxy. A leftover middleware.ts is skipped at build time without an error, so protection stops.
Return result.response, not the handler result
The handler resolves to a CentinelResult object, not a Response. Exporting it directly makes every matched request fail with a 500.
Create both /block and /interstitial
The middleware redirects to these two paths, and neither is configurable in the current release. Add both pages before you enable enforcement.
Use isBot() from v1.2.3 or later
Older releases returned true from isBot() on any validator error and had no timeout. Upgrade before you put manual validation on a login route.
What stays untouched
- Your hosting on Vercel, AWS, Docker or your own Node servers
- Your authentication stack, such as NextAuth, Clerk or Auth0
- Your React Server Components, database layer and payments
- Routes outside your matcher
Next.js integration questions
Walk through your Next.js setup
Bring one route bots are hitting. We will map the right connector, timeouts, and log-only rollout for your Next.js stack.