Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

Simplifying webhook client responses

Especially useful with high volume projects

Joel Clermont
Joel Clermont
2024-07-22

From our daily Laravel tips email. Get the next one in your inbox.

I really like the Laravel webhook client package (from Spatie, of course).

On a recent project, I was implementing it to receive a potentially high volume of webhooks, so I was thinking a bit more about performance.

One admittedly "micro" optimization I identified was that we didn't need to start a session or return a cookie when returning a response to an incoming webhook.

In order to avoid this, you can exclude three middleware from the webhook receiving route:

  • StartSession
  • ShareErrorsFromSession
  • VerifyCsrfToken

Here's what that would look like in your routes file:

Route::webhooks('url-path', 'config-name')
    ->withoutMiddleware([
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ]);

So now the responses are a bit more lightweight. And as a bonus, I no longer need to exclude this webhook route from CSRF verification.

For most projects, this may be overkill, but it's worth considering if you're expecting a lot of inbound webhooks.

Here to help,

Joel

P.S. Are you dealing with performance bottlenecks in your Laravel application? Let's talk!

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.