Webhook

Eventi di consegna email

Ricevi notifiche in tempo reale sullo stato di consegna delle email. Configura il tuo URL webhook nella dashboard. Tutti gli eventi sono firmati con HMAC-SHA256 per sicurezza.

POSThttps://your-server.com/webhook/email/delivery
!

Verifica sempre la firma del webhook. Genera il segreto del webhook nella dashboard e usalo per verificare le richieste in arrivo. Calcola l'HMAC sul body grezzo della richiesta, non su un JSON riserializzato.

Avvio rapido

Un listener che verifica la firma e poi gestisce l'evento. Rispondi con uno stato 2xx quando l'evento è accettato.

const express = require('express');
const crypto = require('crypto');

const app = express();
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;

function verifySignature(rawBody, signature) {
  if (!signature) return false;

  const expected = 'sha256=' + crypto
    .createHmac('sha256', WEBHOOK_SECRET)
    .update(rawBody)
    .digest('hex');

  const a = Buffer.from(signature);
  const b = Buffer.from(expected);
  // timingSafeEqual throws when the lengths differ
  return a.length === b.length && crypto.timingSafeEqual(a, b);
}

// express.raw keeps the body as a Buffer, exactly as it was sent
app.post('/webhook/email/delivery', express.raw({ type: 'application/json' }), (req, res) => {
  if (!verifySignature(req.body, req.headers['x-webhook-signature'])) {
    return res.status(401).json({ error: 'Invalid signature' });
  }

  const { event, emailId, data } = JSON.parse(req.body.toString('utf8'));

  console.log('[Email Delivery] ' + event + ' - ' + emailId);

  if (event === 'delivered') {
    console.log('   Recipient: ' + data.recipientEmail);
  } else if (event === 'bounced') {
    console.log('   Reason: ' + data.reason);
  }

  res.status(200).json({ received: true });
});

app.listen(3001, () => {
  console.log('Email delivery webhook listener on port 3001');
  console.log('   POST /webhook/email/delivery');
});

Header

  • X-Webhook-Signaturestringobbligatorio

    Firma HMAC-SHA256 del body della richiesta. Formato: sha256=hex_digest

Test con ngrok

Esponi il tuo listener locale su internet e registra l'URL generato come URL webhook nella dashboard.

ngrok http 3001
# Your webhook URL:
# https://abc123.ngrok.io/webhook/email/delivery

Eventi

Ogni evento è un oggetto JSON con event, timestamp e un oggetto data . Apri un evento per vedere payload e campi completi.

L'endpoint fallisce troppo spesso? Nella dashboard puoi vedere lo stato di ogni webhook, riattivarlo e ritentare le consegne fallite. Apri Webhook nella dashboard