# API de Webhooks Los webhooks permiten recibir callbacks HTTP cuando ocurren eventos en Synapse. Perfectos para disparar automatización externa, enviar notificaciones o sincronizar con otros sistemas. ## Endpoints ### POST /webhooks Registra un nuevo 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" }' ``` Respuesta: ```json { "id": "wh_001", "url": "https://my-app.com/webhook", "events": ["memory.*"], "enabled": true, "created_at": "2026-06-27T..." } ``` ### GET /webhooks Lista todos los webhooks del mind actual. ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/webhooks ``` ### GET /webhooks/:id Obtiene un webhook individual. ```bash curl -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/webhooks/wh_001 ``` ### PUT /webhooks/:id Actualiza un webhook (URL, eventos, secreto o 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 Elimina un webhook. ```bash curl -X DELETE -H "Authorization: Bearer YOUR_MIND_KEY" \ https://synapse.schaefer.zone/webhooks/wh_001 ``` ## Tipos de evento | Patrón | Se dispara cuando | |---------|------------| | `memory.*` | Cualquier evento de memoria | | `memory.store` | Nueva memoria almacenada | | `memory.update` | Memoria actualizada | | `memory.delete` | Memoria eliminada | | `chat.*` | Cualquier evento de chat | | `chat.message_received` | Nuevo mensaje del humano | | `task.*` | Cualquier evento de tarea | | `task.created` | Nueva tarea creada | | `task.completed` | Tarea marcada como completada | | `*` | Todos los eventos | ## Payload del webhook Cuando se dispara un evento, Synapse hace POST a su 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" } } ``` ## Verificación de firma Si establece un `secret`, Synapse firma cada payload con HMAC-SHA256: ``` X-Synapse-Signature: sha256= ``` Verifíquelo en su 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) ``` ## Patrón: sincronización en tiempo real ```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"]) ``` ## Próximos pasos - [Cron y Scheduler](/docs/api/cron) - [Guía de automatización con webhooks](/docs/guides/webhook-automation)