Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DaniCool's avatar

Laravel throttle limit

Hello,

I have a API which I want to protect. How do I implement laravel throttle, so it is limited with x requests per ip-address per route?

I have tried to search, but cant really find a way to do it.

Thanks!

0 likes
4 replies
martinbean's avatar

I have tried to search, but cant really find a way to do it.

@danicool There’s a whole section dedicated to rate limiting in the Laravel routing docs: https://laravel.com/docs/routing#rate-limiting

The examples even use IP address as a discriminator.

Define your rate limit:

RateLimiter::for('api', function (Request $request) {
    return Limit::perMinute(60)->by($request->ip());
});

Then apply it to your routes:

Route::middleware(['throttle:api'])->group(function () {
    // These routes will be rate limited...
});
DaniCool's avatar

@martinbean I have tried that. If I do a lot of api calls I get the 429 error on my laptop. If I try to open the API on another computer with another IP the new IP also gets 429 - but it have only called the API one time?

martinbean's avatar

I have tried that. If I do a lot of api calls I get the 429 error on my laptop.

@danicool Which is a “Too Many Requests” response, which Laravel return if you exceed a rate limit. So this suggests it is working.

If I try to open the API on another computer with another IP the new IP also gets 429 - but it have only called the API one time?

If this other computer is on the same network, then it’s going to have the router’s IP address—not the individual machine’s—and therefore appear to come from the same IP address.

DaniCool's avatar

@martinbean If this other computer is on the same network, then it’s going to have the router’s IP address—not the individual machine’s—and therefore appear to come from the same IP address.

No, i did it on my cell-phone which runs on cellular, it is kinda weird...

Please or to participate in this conversation.