Laravel Stripe Webhook Returns 419 or 400: Fix It

Stripe shows your Laravel webhook failing with 419, 400, 301 or 503? Move the route out of CSRF, verify the signature on the raw body, and stop config caching from hiding your secret.

A Stripe webhook that returns 419 in Laravel is being blocked by CSRF protection, because Stripe cannot send a CSRF token. Put the webhook route in routes/api.php or exclude its path from CSRF checks, then protect it with Stripe's signature check instead. If you get 400, 301 or 503 instead, the signature, a redirect or maintenance mode is the cause.

Stripe shows the response code for every delivery in the Dashboard under Developers, Webhooks. That code tells you where to look. This guide goes through each one in order, so you can get a failing endpoint back to 200 and keep it there.

Step 1: Read the Status Code in the Stripe Dashboard

Open your endpoint in the Stripe Dashboard and click a failed event. You will see the HTTP status your server returned and the response body.

  • 419: Laravel's CSRF middleware rejected the request (Step 2).
  • 400: your own handler rejected it, usually a signature or secret problem (Steps 3 and 4).
  • 301, 302, 308: your server redirected the request (Step 5).
  • 503: the app is in maintenance mode or down (Step 5).
  • 404 or 405: the URL or HTTP method is wrong. Webhooks are POST only.
  • 500: your code threw an exception. Read storage/logs/laravel.log.

Laravel's own docs say the Stripe webhook route must be excluded from CSRF protection, because Stripe does not know which token to send. The Laravel CSRF documentation covers this, and a Laracasts thread on webhook issues shows the same 419 report.

Step 2: Fix the 419 With an API Route

A webhook is machine to machine. It has no session and no form. The cleanest fix is to define it in routes/api.php, which does not use the web middleware group:

// routes/api.php
use App\Http\Controllers\StripeWebhookController;
use Illuminate\Support\Facades\Route;

Route::post('/stripe/webhook', StripeWebhookController::class);

On Laravel 11 and newer, the api.php file does not exist by default. Run php artisan install:api to create it. Your URL becomes https://example.com/api/stripe/webhook. Update the endpoint URL in Stripe to match.

If you want to keep the route in routes/web.php, exclude it from CSRF instead. On Laravel 11 and newer:

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: [
        'stripe/webhook',
    ]);
})

On Laravel 10 and older, add the path to the $except array in app/Http/Middleware/VerifyCsrfToken.php. Use the exact path, without the domain. The same 419 happens with other providers; this guide on a Paystack webhook returning 419 in Laravel describes an identical cause.

If you use Laravel Cashier, it registers its own /stripe/webhook route and handles CSRF for it. Do not add a second route with the same path.

A 419 on a login form is a different problem. For that, read Laravel 419 Page Expired on Login in Production.

Step 3: Verify the Signature on the Raw Body

Once CSRF is out of the way, anyone can post to the URL. The Stripe signature is what proves the request came from Stripe. Install the library with composer require stripe/stripe-php, then read the raw body:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe\Exception\SignatureVerificationException;
use Stripe\Webhook;

class StripeWebhookController extends Controller
{
    public function __invoke(Request $request)
    {
        $payload = $request->getContent();
        $signature = $request->header('Stripe-Signature');

        try {
            $event = Webhook::constructEvent(
                $payload,
                $signature,
                config('services.stripe.webhook_secret')
            );
        } catch (\UnexpectedValueException $e) {
            return response('Invalid payload', 400);
        } catch (SignatureVerificationException $e) {
            return response('Invalid signature', 400);
        }

        // Save the event and hand the work to a queue (Step 6).

        return response('ok', 200);
    }
}

Use $request->getContent(). Do not use json_encode($request->all()). Stripe signs the exact bytes it sent, and re-encoding changes spacing and key order, so the check fails even with the right secret.

Step 4: Check the Secret and Config Caching

Most "No signatures found matching the expected signature" errors are a wrong secret. Check three things:

  • Each endpoint has its own secret. The whsec_... value is shown per endpoint in the Dashboard. The Stripe CLI (stripe listen) prints a different one. Test mode and live mode also differ.
  • Config caching. After php artisan config:cache, calls to env() outside config files return null. Always read the secret through config():
// config/services.php
'stripe' => [
    'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'),
],
  • Stale cache after a deploy. Run php artisan config:clear and then config:cache after you change .env, and restart the PHP workers.

A middleware that changes the request body before your controller, such as one that trims or converts input, can also break the signature. Keep the webhook route free of extra middleware.

Step 5: Stop Redirects and Maintenance Mode

Stripe does not follow redirects. If your server answers 301 or 308, the delivery fails.

  • HTTP to HTTPS. The endpoint URL in Stripe must start with https://.
  • www and non-www. If your site redirects example.com to www.example.com, use the final host in Stripe.
  • Trailing slashes. A redirect from /api/stripe/webhook to /api/stripe/webhook/ also breaks delivery.
  • Maintenance mode. php artisan down returns 503 for all routes. Use --secret or the except list in the maintenance middleware, or keep deploys short so Stripe's retries can recover. Stripe retries failed deliveries for a while, but you should not rely on it.

Behind a load balancer, make sure Laravel trusts the proxy so it sees the request as HTTPS. If it does not, a forced-HTTPS rule can redirect forever, the same trap described in Express Session Cookie Not Set in Production for Node.js.

Step 6: Return 200 Fast and Process Later

Stripe expects a quick response. Heavy work inside the request causes timeouts and duplicate deliveries. Store the event and queue the work:

$exists = \DB::table('stripe_events')->where('stripe_id', $event->id)->exists();

if (! $exists) {
    \DB::table('stripe_events')->insert([
        'stripe_id' => $event->id,
        'type' => $event->type,
        'created_at' => now(),
    ]);

    \App\Jobs\HandleStripeEvent::dispatch($event->id);
}

return response('ok', 200);

Put a unique index on stripe_id. Stripe can send the same event more than once, and the unique key stops you from granting access or sending an email twice. If the queue is not running, events pile up unseen; see Laravel Queue Jobs Not Processing in Production.

For the Node.js version of these checks, including raw body handling in Express, read Stripe Webhook Failures: Debug and Fix in Production.

Step 7: Test Before You Ship

Run the Stripe CLI against your local app:

stripe listen --forward-to localhost:8000/api/stripe/webhook
stripe trigger checkout.session.completed

Add a feature test that posts to the route without a CSRF token and asserts the status is not 419. A bad signature should return 400. A good one should return 200. After each deploy, send one test event from the Dashboard and confirm the response code.

How Relia Helps

A webhook that fails in production is easy to miss, because the customer paid and nothing on your site looks broken. Relia has a Composer package for PHP and Laravel that reports unhandled exceptions with the stack trace and request. It also joins a new error, a recent deploy and the users affected into one incident in plain words, and after a deploy it checks that the fix worked. Uptime monitors and checks of key pages cover the parts of your site customers see. You can try the live demo first.

FAQ

Why does my Laravel webhook return 419?

The web middleware group includes CSRF verification. Stripe sends a plain POST with no token, so Laravel answers 419. Move the route to routes/api.php or exclude it from CSRF checks.

Is it safe to exclude a route from CSRF?

Yes, if you verify the Stripe signature. The signature check replaces the CSRF token for this route. Never leave a webhook without one of the two.

Why does signature verification fail with the right secret?

Usually the body was changed before verification, the secret belongs to a different endpoint or mode, or config caching made env() return null. Verify against $request->getContent() and read the secret with config().

Does Laravel Cashier need a CSRF exclusion?

Cashier registers its own webhook route and handles this for you. Add the exclusion only for a route you wrote yourself.

Should I return 200 before doing the work?

Yes. Save the event, queue the job and return 200 quickly. Use the event id as a unique key so repeated deliveries do nothing.

All guides