Webhooks
Obtener una clave de APIRegistrar un webhook
Registra una dirección https pública y los eventos que quieres. El secreto de firma (whsec_…) se muestra solo en esta respuesta. Una aplicación puede tener hasta 10 webhooks.
curl -X POST https://api.sofia-post.com/api/v1/webhooks \
-H "Authorization: Bearer $SOFIA_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"url":"https://app.example.com/hooks/sofia","events":["post.published","post.failed"]}'Eventos
| Evento | data |
|---|---|
post.published | postId, establishmentId, platform, publishedAt, permalink |
post.failed | postId, establishmentId, platform, error |
webhook.test | webhookId |
Sofia envía cada evento por POST como JSON { id, type, createdAt, data }, con las cabeceras Sofia-Event-Id, Sofia-Event-Type y Sofia-Signature. Solo se anuncian las publicaciones que creó tu aplicación.
Verificar la firma
Sofia-Signature es t=<unix time>,v1=<hex>, donde v1 es el HMAC-SHA256 de <t>.<raw body> con tu secreto. Verifícala sobre el cuerpo sin procesar y rechaza una marca de tiempo antigua:
import { createHmac, timingSafeEqual } from 'node:crypto';
const isSignedBySofia = (header, rawBody, secret) => {
const parts = Object.fromEntries(
(header ?? '').split(',').map((part) => part.trim().split('=', 2)),
);
const timestamp = Number(parts.t);
if (!Number.isInteger(timestamp) || typeof parts.v1 !== 'string') return false;
if (Math.abs(Date.now() / 1000 - timestamp) > 300) return false;
const expected = createHmac('sha256', secret).update(`${timestamp}.${rawBody}`).digest();
const received = Buffer.from(parts.v1, 'hex');
return received.length === expected.length && timingSafeEqual(received, expected);
};Entrega y reintentos
- Una respuesta
2xxentrega el evento. Cualquier otra, una redirección (nunca se sigue) o 10 segundos sin respuesta lo reintentan tras 1 minuto, 5 minutos, 30 minutos y 2 horas, hasta 5 intentos. - El mismo evento puede llegar dos veces: deduplica por
Sofia-Event-Id. - Responde rápido y procesa el evento después.
POST /v1/webhooks/{id}/test envía un evento webhook.test, firmado como los demás.