Every project we ship has at least one webhook integration. They look simple — POST a payload, return 200, done. Production teaches you they are not simple. Three patterns we apply everywhere.

1. Idempotency keys, always

Sender systems retry. Your endpoint must produce the same outcome no matter how many times a given event arrives. We require an idempotency key (provider's event ID is fine) and store it. A second arrival of the same key is a no-op.

DB::transaction(function () use ($payload) {
    $existed = WebhookEvent::firstOrCreate(['provider_id' => $payload['id']]);
    if ($existed->wasRecentlyCreated) {
        ProcessWebhookEvent::dispatch($payload);
    }
});

2. Accept-and-defer, always

Your endpoint accepts the payload, persists it, returns 200, and processes asynchronously. Synchronous processing in the webhook handler is the most common cause of provider retries — your handler is too slow, the provider times out, the provider retries, your handler runs twice. The accept-and-defer pattern decouples receipt from processing.

3. Replay infrastructure from day one

Build a "replay this event" admin action from the very first webhook integration. You will need it. The first time a payload caused a bug, fixed-but-already-failed, you'll be glad. The first time a downstream service was offline for two hours, you'll be glad.

What we don't do

  • We don't validate signatures in middleware. Signature validation lives in the controller, with the body raw, before any framework parsing. Middleware ordering bugs have eaten too many afternoons.
  • We don't return non-200 to indicate "I'll process this later." Either you accept it (200) or you didn't (4xx/5xx). The provider's retry logic doesn't speak nuance.