使用你偏好的包管理器将 SDK 添加到你的项目。
npm install net/httpyarn add net/httppnpm add net/http以最简配置快速上手运行。
package mainimport ( "fmt" "io" "net/http")func main() { http.HandleFunc("/webhook/tracio", func(w http.ResponseWriter, r *http.Request) { body, _ := io.ReadAll(r.Body) // verify r.Header.Get("X-Tracio-Signature") against body, then act fmt.Println(string(body)) w.WriteHeader(http.StatusOK) }) http.ListenAndServe(":8080", nil)}包含错误处理、加载状态和高级配置的生产级模式。
package mainimport ( "crypto/hmac" "crypto/sha256" "encoding/hex" "encoding/json" "io" "log" "net/http" "os" "strings")type Event struct { VisitorID string `json:"visitorId"` Bot struct { Result string `json:"result"` } `json:"bot"` Decision struct { RiskScore int `json:"riskScore"` } `json:"decision"`}// verify recomputes the HMAC-SHA256 over "<t>.<rawBody>" and compares it.func verify(body []byte, header, secret string) bool { parts := map[string]string{} for _, kv := range strings.Split(header, ",") { if p := strings.SplitN(kv, "=", 2); len(p) == 2 { parts[p[0]] = p[1] } } mac := hmac.New(sha256.New, []byte(secret)) mac.Write([]byte(parts["t"] + ".")) mac.Write(body) expected := hex.EncodeToString(mac.Sum(nil)) return hmac.Equal([]byte(expected), []byte(parts["v1"]))}func main() { secret := os.Getenv("TRACIO_WEBHOOK_SECRET") http.HandleFunc("/webhook/tracio", func(w http.ResponseWriter, r *http.Request) { body, _ := io.ReadAll(r.Body) if !verify(body, r.Header.Get("X-Tracio-Signature"), secret) { http.Error(w, "Invalid signature", http.StatusUnauthorized) return } var e Event if err := json.Unmarshal(body, &e); err != nil { http.Error(w, "Bad payload", http.StatusBadRequest) return } if e.Bot.Result == "bot" || e.Decision.RiskScore > 50 { log.Printf("flagging visitor %s (bot=%s risk=%d)", e.VisitorID, e.Bot.Result, e.Decision.RiskScore) } w.WriteHeader(http.StatusOK) }) log.Fatal(http.ListenAndServe(":8080", nil))}用于初始化和配置 SDK 的所有可用选项。
publicKeystring来自仪表盘的公钥 — 可安全地随浏览器代码发布endpointstring用于代理路由部署的自定义端点 URL — 显式 URL 优先于 regionregionstring数据区域:us 或 eutimeoutMsnumber整个 getResult() 调用的超时时间(毫秒)linkedIdstring已登录用户在您系统内的账户 ID,用于关联共享同一设备的访问tagstring附加到识别请求上的自由格式标签,例如 checkout 或 logindebugboolean将脚本生命周期和网络活动输出到浏览器控制台scriptUrlstring完全覆盖 agent 脚本 URL — 用于自托管或 Subresource Integrity通过完整的 API 参考、webhook 配置和高级指南深入了解。