お好みのパッケージマネージャーで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はregionより優先されますregionstringデータリージョン:usまたはeutimeoutMsnumbergetResult()呼び出し全体のタイムアウト(ミリ秒)linkedIdstringログイン中のユーザーの内部アカウントID。同じデバイスを共有する訪問を関連づけられますtagstring識別リクエストに付与する自由形式のラベル(例:checkoutやlogin)debugbooleanスクリプトのライフサイクルとネットワーク動作をブラウザコンソールに出力しますscriptUrlstringエージェントスクリプトURLの完全な上書き — セルフホストやSubresource Integrity向け完全なAPIリファレンス、Webhook設定、高度なガイドでさらに深く学べます。