用途
Vercel BotID 用于在代码中为特定路由和服务端操作提供隐形、按路由的机器人防护。它不依赖 CAPTCHA,也不在代理或中间件中读取评分头,而是由客户端脚本为指定请求附加挑战头,服务端调用 checkBotId() 将请求头发送至 Vercel 并返回当前请求的判定结果。
主要能力
- 客户端通过 initBotId() 声明受保护路由,支持路径通配符(* 匹配任意字符,包括 / 和空)与方法通配符('*' 匹配任意方法)。
- 服务端通过 checkBotId() 返回判定字段:isBot、isHuman、isVerifiedBot、verifiedBotName、verifiedBotCategory、bypassed。
- 支持已验证机器人(botid 1.5.0+),可从已验证机器人目录中放行特定代理,例如 chatgpt-operator。
- 提供 Basic(免费,所有套餐)与 Deep Analysis(Kasada,Pro 每 1,000 次 checkBotId() 调用 1 美元,Enterprise 定制)两个级别,仅对 checkBotId() 调用计费。
- 支持按路由指定级别:advancedOptions.checkLevel 可设为 'deepAnalysis' 或 'basic'(1.4.5+),需在 protect 条目与 checkBotId() 两侧一致,否则验证失败。
- 支持独立前端主机:checkBotId({ advancedOptions: { extraAllowedHosts: ['app.example.com'] } })。
- 支持每次访问仅检查一次:页面加载时验证,仅在 isHuman && !bypassed 时签发签名 Cookie。
使用方式
安装:npm i botid。无需配置包装器或重写规则,Vercel 会自动路由 BotID 请求;应移除旧的 withBotId、botid/nuxt 或 BotID vercel.json 重写。
客户端声明受保护路由(Next.js 15.3+ 示例):
// instrumentation-client.ts
import { initBotId } from 'botid/client/core';
initBotId({
protect: [
{ path: '/api/signup', method: 'POST' },
{ path: '/api/user/*', method: 'POST' },
{ path: '/checkout', method: 'POST' }, // server action invoked from /checkout
],
});
Next.js < 15.3 使用根布局 <head> 中的 <BotIdClient protect={routes} />(来自 botid/client)。Nuxt 使用 plugins/botid.client.ts 并设置 enforce: 'pre'。SvelteKit 在 src/hooks.client.ts 中调用 init()。请求必须使用 fetch 或 XHR;传统 <form action method> 提交不受支持,应改用 onSubmit + fetch 或服务端操作。
服务端在昂贵操作前检查:
import { checkBotId } from 'botid/server';
export async function POST(request: Request) {
const { isBot } = await checkBotId();
if (isBot) return Response.json({ error: 'Access denied' }, { status: 403 });
// inference, writes, payments
}
服务端操作顶部同样调用。Pages Router 在开发环境需传入 advancedOptions: { headers: req.headers }。
已验证机器人放行示例:
const { isBot, isVerifiedBot, verifiedBotName } = await checkBotId();
if (isBot && !(isVerifiedBot && verifiedBotName === 'chatgpt-operator')) {
return Response.json({ error: 'Access denied' }, { status: 403 });
}
对于不在目录中的可信服务,应使用单独带认证的路由或限定其 IP 的 WAF 绕过规则。被绕过的流量返回 bypassed: true。
Deep Analysis 可在仪表盘 Firewall → Rules → Vercel BotID Deep Analysis 开启,或执行 vercel firewall rules update bot-id --action deep-analysis(分阶段生效,basic 会回退)。因涉及计费,应先与用户确认并由其发布。
适用场景
- 注册、登录、结账、表单提交以及由自有前端调用的服务端操作。
- AI 推理及其他昂贵端点。
限制与注意事项
- 仅支持 Vercel:需要请求上下文与 OIDC 令牌,自托管或 next start 下会失败;VERCEL_OIDC_TOKEN 未设置即表示运行在 Vercel 之外。
- 不适用于全站页面与爬虫流量(应使用 Bot Protection 或 AI Bots 托管规则)、由服务器、webhook 或 CLI 调用的公共 API(应使用 API 密钥、速率限制或 WAF 规则),以及按名称或类别定向机器人(应使用自定义规则)。
- 常见问题:始终判定为机器人,通常是路由未加入 protect、路径或方法不匹配,或非浏览器调用方;日志中的配置警告会指出需添加的 { path, method }。始终判定为人类,通常是处理程序未调用 checkBotId() 或其结果被忽略。
- 测试:本地(NODE_ENV=development)返回人类;可用 checkBotId({ developmentOptions: { bypass: 'BAD-BOT' } })(或 'HUMAN'、'GOOD-BOT')强制判定。已部署环境应从应用 UI 或加载了脚本的页面发起 fetch 测试;curl、Postman 和地址栏会被报告为机器人,属预期行为。
- 监控:在 Firewall 流量视图中筛选 BotID;Observability Plus 可提供指标。
- 签名 Cookie 是持有者凭证,仍需保留认证、速率限制与 CSRF 防护。
参考
- 文档:https://vercel.com/docs/botid
- 入门:https://vercel.com/docs/botid/get-started
- 高级配置:https://vercel.com/docs/botid/advanced-configuration
- 已验证机器人:https://vercel.com/docs/botid/verified-bots
- 表单提交:https://vercel.com/docs/botid/form-submissions
- 页面加载时验证:https://vercel.com/docs/botid/verify-on-page-load