API Reference

Webhooks

Receive real-time notifications for governance events via HTTP webhooks.

Overview

Webhooks allow you to receive real-time notifications when events occur in your Tork environment.

Event-Driven

Push notifications for events

Signed Payloads

HMAC signature verification

Auto Retry

Failed deliveries retried

Configuring Webhooks

Webhooks are delivered by Tork, so they are registered over the REST API. There is no Python cloud SDK — @torknetwork/sdk is npm-only, and tork-governance on PyPI decides on-device and never calls out. Register an endpoint with a Bearer token:

python
import os
import requests

# There is no Python cloud SDK — use the REST API.
TORK_API_KEY = os.environ["TORK_API_KEY"]

response = requests.post(
    "https://tork.network/api/v1/webhooks",
    headers={
        "Authorization": f"Bearer {TORK_API_KEY}",
        "Content-Type": "application/json",
    },
    json={
        "name": "Production governance events",
        "url": "https://your-server.com/webhooks/tork",
        "events": [
            "policy.violation",
            "approval_requested",
            "approval_approved",
            "hitl.pending",
            "circuit_breaker.triggered",
        ],
    },
)
response.raise_for_status()

webhook = response.json()
print(f"Webhook ID: {webhook['id']}")
# The signing secret is returned ONLY at creation time — store it now.
print(f"Signing secret: {webhook['secret']}")

Event Types

Available webhook event types:

policy.violationAn agent action was blocked by a policy
pii.detectedPII was detected in governed content
hitl.pendingAn action is waiting on human approval
hitl.approvedA human approved a pending action
hitl.rejectedA human rejected a pending action
agent.blockedAn agent was blocked from acting
circuit_breaker.triggeredA circuit breaker was tripped
rate_limit.exceededA rate limit was exceeded
security.alertA security alert was raised
approval_requestedA new approval request was created (legacy)
approval_approvedAn approval was granted (legacy)
approval_rejectedAn approval was rejected (legacy)
approval_timeoutAn approval request timed out (legacy)
approval_expiredAn approval request expired (legacy)

Webhook Payload

All webhooks follow this payload structure:

json
{
  "id": "evt_abc123def456",
  "type": "policy.violation",
  "created_at": "2024-01-15T10:30:00Z",
  "data": {
    "agent_id": "agent-123",
    "policy_name": "block-pii-sharing",
    "action": "BLOCK",
    "reason": "PII detected in output",
    "content_preview": "The user's SSN is...",
    "metadata": {
      "user_id": "user_456",
      "session_id": "sess_789"
    }
  },
  "environment": "production"
}

Signature Verification

Verify webhook signatures to ensure authenticity:

python
import hmac
import hashlib
from flask import Flask, request

app = Flask(__name__)
WEBHOOK_SECRET = "whsec_your_signing_secret"

@app.route("/webhooks/tork", methods=["POST"])
def handle_webhook():
    # Get the signature from headers
    signature = request.headers.get("X-Tork-Signature")
    timestamp = request.headers.get("X-Tork-Timestamp")

    # Compute expected signature
    payload = f"{timestamp}.{request.data.decode()}"
    expected = hmac.new(
        WEBHOOK_SECRET.encode(),
        payload.encode(),
        hashlib.sha256
    ).hexdigest()

    # Verify signature
    if not hmac.compare_digest(signature, f"sha256={expected}"):
        return "Invalid signature", 401

    # Process the event
    event = request.json
    print(f"Received event: {event['type']}")

    return "OK", 200

Retry Policy

Failed webhook deliveries are automatically retried:

AttemptDelay
1st retry1 minute
2nd retry5 minutes
3rd retry30 minutes
4th retry2 hours
5th retry24 hours

Important: Your webhook endpoint must respond with a 2xx status code within 30 seconds, or it will be marked as failed.

Documentation

Learn to integrate TORK

Upgrade Plan

Current: free

Support

Get help from our team