Laravel / PHP PHP Free

Laravel: Rate-Limit API Routes with Throttle

Protect your Laravel API from abuse with the built-in throttle middleware — limit requests per minute per user or IP.

16 lines · Updated Nov 26, 2026

Preview of Laravel: Rate-Limit API Routes with Throttle
laravel-api-rate-limit-throttle.php
// routes/api.php
use Illuminate\Support\Facades\Route;

// 60 requests per minute, per authenticated user
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
    Route::get('/posts', [PostController::class, 'index']);
});

// Stricter limit for login: 5 attempts per minute, per IP
Route::post('/login', [AuthController::class, 'login'])
    ->middleware('throttle:5,1');

// Named limiter in bootstrap/app.php for a custom rule
// ->withMiddleware(function (Middleware $middleware) {
//     $middleware->throttleApi(); // 60/min for all API routes
// });

About this snippet

Any public API will eventually be hit by a buggy client in a loop, a scraper, or someone guessing passwords. Without limits, one bad actor can slow down the app for everybody — or run up your server bill. Laravel's built-in throttle middleware limits how many requests a user or IP address can make per minute, and returns a clean 429 Too Many Requests response when they go over.

How it works

  1. Each request increments a counter stored in your cache (Redis, database or file).
  2. The counter key is the authenticated user's ID, or the IP address for guests.
  3. When the limit is reached, Laravel stops the request and returns 429 with a Retry-After header telling the client when to try again.

How to use it

  • Apply a general limit to your API route group, for example 60 requests per minute.
  • Use stricter, named limiters for sensitive endpoints such as login, password reset, OTP and contact forms — for example 5 per minute per email and IP.
  • Give trusted or paying users higher limits by returning a different limit based on the user's plan.

Best practices

  • Use Redis as the cache store in production for fast, accurate counters across multiple servers.
  • Return friendly JSON errors so mobile apps can show "please wait a moment".
  • Log repeated 429s — they often reveal a bug in your own front end.

Common problems

  • Everyone shares one limit: behind a proxy or Cloudflare, configure trusted proxies so Laravel sees the real client IP.

FAQ

Does rate limiting stop DDoS attacks? It helps against abuse from individual clients, but large attacks should be stopped earlier, at a CDN or firewall.

Related: Sanctum token abilities · OWASP Top 10 explained

How to install

Paste the route code into routes/api.php. Change 60,1 to your limit and window in minutes. For a named limiter, register it in bootstrap/app.php with RateLimiter::for().

Share this snippet

See all
Chat with us