# Webhooks API Webhook ช่วยให้คุณรับ HTTP callback เมื่อมี event เกิดขึ้นใน Synapse เหมาะสำหรับการทริกเกอร์ external automation, ส่งการแจ้งเตือน หรือ sync ไปยังระบบอื่น ## Endpoints ### POST /webhooks ลงทะเบียน webhook ใหม่ ```bash curl -X POST https://synapse.schaefer.zone/webhooks \ -H "Authorization: Bearer YOUR_MIND_KEY" \ -H "Content-Type: application/json" \ -d '{ "url": "https://my-app.com/webhook", "events": "memory.*", "secret": "my-hmac-secret-min-8-chars" }' ``` Response: ```json { "id": "wh_001", "url": "https://my-app.com/webhook", "events": ["memory.*"], "enabled": true, "created_at": "2026-06-27T..." } ``` ### GET /webhooks รายการ webhook ทั้งหมดของ mind ปัจจุบัน ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/webhooks ``` ### GET /webhooks/:id ดึง webhook เดียว ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/webhooks/wh_001 ``` ### PUT /webhooks/:id อัปเดต webhook (URL, events, secret หรือ flag enabled) ```bash curl -X PUT https://synapse.schaefer.zone/webhooks/wh_001 \ -H "Authorization: Bearer YOUR_MIND_KEY" \ -H "Content-Type: application/json" \ -d '{"enabled": false}' ``` ### DELETE /webhooks/:id ลบ webhook ```bash curl -X DELETE -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/webhooks/wh_001 ``` ## ประเภท Event | Pattern | ทำงานเมื่อ | |---------|------------| | `memory.*` | memory event ใด ๆ | | `memory.store` | เก็บ memory ใหม่ | | `memory.update` | อัปเดต memory | | `memory.delete` | ลบ memory | | `chat.*` | chat event ใด ๆ | | `chat.message_received` | ข้อความใหม่จาก human | | `task.*` | task event ใด ๆ | | `task.created` | สร้าง task ใหม่ | | `task.completed` | ทำเครื่องหมาย task ว่าเสร็จ | | `*` | ทุก event | ## Webhook Payload เมื่อ event ทำงาน Synapse POST ไปยัง URL ของคุณ: ```json { "event": "memory.store", "timestamp": "2026-06-27T...", "mind_id": "m_xyz789", "data": { "id": "mem_001", "category": "fact", "key": "user_name", "content": "Michael Schäfer" } } ``` ## การตรวจสอบ Signature หากคุณตั้ง `secret` Synapse จะ sign payload แต่ละรายการด้วย HMAC-SHA256: ``` X-Synapse-Signature: sha256= ``` ตรวจสอบใน handler ของคุณ: ```python import hmac, hashlib def verify_signature(payload_body: bytes, signature: str, secret: str) -> bool: expected = hmac.new( secret.encode(), payload_body, hashlib.sha256 ).hexdigest() return hmac.compare_digest(f"sha256={expected}", signature) ``` ## รูปแบบ: Real-Time Sync ```python # Your webhook handler @app.post("/webhook") async def handle_webhook(request): body = await request.body() signature = request.headers.get("X-Synapse-Signature", "") if not verify_signature(body, signature, WEBHOOK_SECRET): return 401 event = json.loads(body) if event["event"] == "memory.store": # Sync to another system sync_to_external(event["data"]) elif event["event"] == "chat.message_received": # Trigger agent wake-up notify_agent(event["data"]) ``` ## ขั้นตอนถัดไป - [Cron & Scheduler](/docs/api/cron) - [Webhook Automation Guide](/docs/guides/webhook-automation)