🚀 新規事業

webhook-receiver

最終更新 2026年05月08日 / 40_新規事業/SCALE_Build/カタログ/基本装備集/webhook-receiver.md

Webhook 受信エンドポイント

何のために?

Webhook 無いと「相手のイベント」を取れず、Polling で疲弊する。

どう実装する?

/api/webhooks/ で署名検証 + 冪等性キーで重複処理防止。

コード例

// 署名検証(Stripe例)
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET●●●●●●);

export async function POST(req: Request) {
  const sig = req.headers.get('stripe-signature')!;
  const body = await req.text();
  const event = stripe.webhooks.constructEvent(body, sig, process.env.STRIPE_WEBHOOK_SECRET!);
  // 冪等性: event.id を DB に記録、既存ならスキップ
  if (await alreadyProcessed(event.id)) return new Response('OK');
  // 処理...
  return new Response('OK');
}