Webhook

メール配信イベント

メールの配信状況をリアルタイムで通知します。Webhook URLは ダッシュボードで設定します。すべてのイベントはセキュリティのためHMAC-SHA256で署名されています。

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

Webhookの署名は必ず検証してください。 ダッシュボードでWebhookのシークレットを生成し、受信したリクエストの検証に使います。HMACは再シリアライズしたJSONではなく、生のリクエストボディで計算してください。

クイックスタート

署名を検証してからイベントを処理するリスナーです。イベントを受け付けたら 2xx ステータスで応答してください。

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');
});

ヘッダー

  • X-Webhook-Signaturestring必須

    リクエストボディのHMAC-SHA256署名。形式: sha256=hex_digest

ngrokでテスト

ローカルのリスナーをインターネットに公開し、生成されたURLをダッシュボードでWebhook URLとして登録します。

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

イベント

すべてのイベントは event, timestamp と dataオブジェクトを持つJSONです。イベントを開くと、完全なペイロードと項目を確認できます。

エンドポイントの失敗が多すぎますか?ダッシュボードで各Webhookの状態を確認し、再有効化や失敗した配信の再試行ができます。 ダッシュボードでWebhookを開く