使用你偏好的包管理器将 SDK 添加到你的项目。
npm install expressyarn add expresspnpm add express以最简配置快速上手运行。
import express from 'express'const app = express()// Capture the raw body so the signature can be verified byte-for-byte.app.use(express.json({ verify: (req, _res, buf) => ((req as any).rawBody = buf) }))// Identification events are pushed here in real timeapp.post('/webhook/tracio', (req, res) => { const event = req.body console.log(event.visitorId, event.bot.result) res.status(200).send('OK')})app.listen(3000)包含错误处理、加载状态和高级配置的生产级模式。
import express from 'express'import crypto from 'crypto'const app = express()app.use(express.json({ verify: (req, _res, buf) => ((req as any).rawBody = buf) }))// Recompute the HMAC-SHA256 over "<t>.<rawBody>" and compare it.function verify(rawBody: Buffer, header: string, secret: string): boolean { const p = Object.fromEntries(header.split(',').map((kv) => kv.split('=') as [string, string])) const signed = Buffer.concat([Buffer.from(`${p.t}.`), rawBody]) const expected = crypto.createHmac('sha256', secret).update(signed).digest('hex') const a = Buffer.from(p.v1, 'hex') const b = Buffer.from(expected, 'hex') return a.length === b.length && crypto.timingSafeEqual(a, b)}app.post('/webhook/tracio', (req, res) => { const sig = req.headers['x-tracio-signature'] as string if (!verify((req as any).rawBody, sig, process.env.TRACIO_WEBHOOK_SECRET!)) { return res.status(401).json({ error: 'Invalid signature' }) } const event = req.body if (event.bot.result === 'bot') { return res.status(200).json({ action: 'blocked', visitorId: event.visitorId }) } if (event.decision.riskScore > 50) { return res.status(200).json({ action: 'review', visitorId: event.visitorId }) } res.status(200).json({ action: 'allow', visitorId: event.visitorId })})app.listen(3000)用于初始化和配置 SDK 的所有可用选项。
publicKeystring来自仪表盘的公钥 — 可安全地随浏览器代码发布endpointstring用于代理路由部署的自定义端点 URL — 显式 URL 优先于 regionregionstring数据区域:us 或 eutimeoutMsnumber整个 getResult() 调用的超时时间(毫秒)linkedIdstring已登录用户在您系统内的账户 ID,用于关联共享同一设备的访问tagstring附加到识别请求上的自由格式标签,例如 checkout 或 logindebugboolean将脚本生命周期和网络活动输出到浏览器控制台scriptUrlstring完全覆盖 agent 脚本 URL — 用于自托管或 Subresource Integrity通过完整的 API 参考、webhook 配置和高级指南深入了解。