• 訊息
  • 託管代理
  • 管理
Search...
⌘K
第一步
概覽快速入門在 Console 中建立原型
定義您的代理
代理設定工具MCP 連接器權限政策代理技能
設定代理環境
雲端環境設定雲端沙箱參考
將工作委派給您的代理
啟動工作階段工作階段操作工作階段事件串流訂閱 Webhook定義結果使用保管庫進行驗證
管理代理上下文
存取 GitHub附加與下載檔案
進階協調
多代理工作階段排程部署
參考
託管代理參考
處理檔案
Files APIPDF 支援圖片與視覺
技能
概覽最佳實務企業技能
MCP
遠端 MCP 伺服器
雲端平台上的 Claude
AWS 上的 Claude Platform
Log in
訂閱 Webhook
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
託管代理/將工作委派給您的代理

訂閱 Webhook

在重大事件發生時獲得通知,無需輪詢。

「Session」(工作階段)是長時間執行的互動。雖然大多數即時互動是透過 SSE 事件串流進行,但 webhook 會在重大狀態變更時通知您。

Webhook 事件會回傳事件的 type 和 id,而非完整的物件。當您收到 webhook 事件時,需要透過 GET 呼叫直接擷取該物件。這可避免在重試時傳遞過時的資料,並使每次傳遞保持精簡。

支援的事件類型

註冊端點

前往 Console 中的 Manage > Webhooks。

Webhook 端點包含以下項目:

  • URL: 必須是使用 443 連接埠的 HTTPS,且主機名稱必須可公開解析。
  • 事件類型: 此端點接收的 data.type 值清單。端點只會接收其訂閱的事件,以及測試事件(請參閱傳遞行為)。
  • 簽署密鑰: 建立時產生的 32 位元組、以 whsec_ 為前綴的密鑰。此密鑰只會顯示一次,請妥善儲存以驗證 webhook 傳遞。

驗證簽章

每次傳遞都會帶有 X-Webhook-Signature 標頭。使用 SDK 的 unwrap() 輔助函式可一步完成簽章驗證與事件解析。如果簽章無效或酬載已超過五分鐘,此函式會擲回例外。

將 ANTHROPIC_WEBHOOK_SIGNING_KEY 設定為端點建立時顯示的、以 whsec_ 為前綴的密鑰。

處理事件

解析主體內容,依據 data.type 進行分支處理,並透過 ID 擷取資源。回傳任何 2xx 狀態碼以確認接收。其他任何狀態碼(包括 3xx)都會被視為失敗並觸發重試。

每個事件酬載都具有相同的結構,包含事件類型、識別碼,以及物件建立時的時間戳記。

{
  "type": "event",
  "id": "event_01ABC...",
  "created_at": "2026-03-18T14:05:22Z",
  "data": {
    "type": "session.status_idled",
    "id": "sesn_01XYZ...",
    "organization_id": "8a3d2f1e-...",
    "workspace_id": "c7b0e4d9-..."
  }
}

頂層的 event.id 對每個事件而言是唯一的,而非對每次傳遞而言。如果您收到相同的 event.id 兩次,表示這是重試,您可以將其捨棄。

傳遞行為

  • 不保證順序。 session.status_idled 可能會在 session.outcome_evaluation_ended 之前送達,即使結果是先產生的。如果順序很重要,請使用 created_at 時間戳記進行排序。
  • 重試: Anthropic 至少會重試一次。重試會傳遞相同的 event.id。
  • 不會跟隨重新導向。 3xx 會被視為失敗。如果您的端點遷移了,請在 Console 中更新 URL。
  • 自動停用: 在大約連續 20 次傳遞失敗後,端點會自動設定為 disabled,並附帶機器可讀的 disabled_reason;如果主機名稱解析為私有 IP 或端點回傳重新導向,則會立即停用。解決問題後,請在 Console 中手動重新啟用。

Was this page helpful?

  • 支援的事件類型
  • 註冊端點
  • 驗證簽章
  • 處理事件
  • 傳遞行為
from flask import Flask, request
import anthropic

client = anthropic.Anthropic()  # reads ANTHROPIC_WEBHOOK_SIGNING_KEY from env
app = Flask(__name__)


@app.route("/webhook", methods=["POST"])
def webhook():
    try:
        # 若簽章無效或酬載已過期,unwrap() 會引發例外
        event = client.beta.webhooks.unwrap(
            request.get_data(as_text=True),
            headers=dict(request.headers),
        )
    except Exception:
        return "invalid signature", 400

    if event.data.type == "session.status_idled":
        print("session idled:", event.data.id)
    # 處理其他事件類型

    return "", 200
if event.data.type == "session.status_idled":
    session = client.beta.sessions.retrieve(event.data.id)
    notify_user(session)
return "", 204