Bot protection for Next.js

Score in middleware.ts before the route renders, so blocked traffic never reaches your server components or your data layer.

How ZeroBot fits Next.js

Next.js apps often push rendering to the edge, which is good for speed but means there is no single server-side choke point where you can inspect traffic. middleware.ts restores one: it runs before route resolution, so a blocked request never triggers rendering, never calls your data layer, and never costs you a function invocation. The trade-off is that middleware sits on the critical path for every matched route, so keep the matcher narrow and skip static assets.

The integration

middleware.ts

import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; export async function middleware(req: NextRequest) { const r = await fetch('https://api.zerobot.info/v3/openapi', { method: 'POST', headers: { Authorization: `Bearer ${process.env.ZEROBOT_KEY}`, 'Content-Type': 'application/x-www-form-urlencoded', }, body: new URLSearchParams({ ip: req.headers.get('x-forwarded-for') ?? '', domain: req.nextUrl.hostname, useragent: req.headers.get('user-agent') ?? '', }), }).then((x) => x.json()).catch(() => null); if (r && r.risk_score >= 80) { return new NextResponse('Blocked', { status: 403 }); } return NextResponse.next(); } export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };

Full endpoint reference and an interactive tester are in the API docs. Get a key from the free tier — no card required.

What actually attacks Next.js sites

Server component scraping

If your pages render data server-side, a scraper pays nothing for the HTML you paid a database query for. Server-rendered content is more expensive to serve than a static page and equally easy to harvest.

API route enumeration

Route handlers under /api are public unless you guard them, and Next.js makes it easy to ship one without thinking about auth.

Build and preview endpoint exposure

Staging and preview deployments left reachable are a common source of indexed duplicate content and unintended public access.

Frequently asked questions

Does this work on Vercel edge runtime?

Yes. The middleware uses fetch and URLSearchParams only, both of which are available in the edge runtime. Do not add Node-only imports.

Is there a latency cost?

One HTTP round trip on matched routes, median under 50ms. The matcher in the snippet excludes static assets, which is the main thing to get right.

Will this block legitimate users behind a corporate proxy?

A shared corporate IP with normal browser behaviour scores low, because the score is composite rather than IP-only. Every block is logged with its reason so you can review and whitelist.

Protect your Next.js app this week.

Free tier, real verdicts, and documentation with a built-in tester.

Get started free →

Bot protection for other stacks

WooCommerce Laravel Node.js Django Cloudflare Worker