Api Laravel 8 Laravel 9 Laravel 10 Laravel 11 Laravel 12

429 Too Many Requests in Laravel - Rate Limiting Fix

This error occurs when a client exceeds the rate limit configured for API routes or specific endpoints.

The Error

Error Message
429 Too Many Requests / Rate limit exceeded

Common Causes

  1. 1 Too many requests in short time period
  2. 2 Rate limiter configured too strictly
  3. 3 Bot or script making excessive requests
  4. 4 Shared IP affecting multiple users
  5. 5 Forgot to configure rate limiter for high-traffic endpoints

Solutions

1

Configure rate limiting in RouteServiceProvider

PHP
// In App\Providers\RouteServiceProvider
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

protected function configureRateLimiting()
{
    RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by(
            $request->user()?->id ?: $request->ip()
        );
    });
}
2

Create custom rate limiters

PHP
// Different limits for different endpoints
RateLimiter::for('uploads', function (Request $request) {
    return Limit::perMinute(10)->by($request->user()->id);
});

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

// Apply to routes
Route::middleware(['throttle:uploads'])->post('/upload', ...);
Route::middleware(['throttle:search'])->get('/search', ...);
3

Handle rate limit in frontend

JavaScript
axios.get('/api/data')
    .catch(error => {
        if (error.response.status === 429) {
            const retryAfter = error.response.headers['retry-after'];
            console.log(`Rate limited. Retry after ${retryAfter} seconds`);
            // Show user-friendly message
        }
    });
4

Disable rate limiting for testing

PHP
// In routes/api.php temporarily
Route::withoutMiddleware('throttle:api')
    ->get('/test', function () {
        return 'No rate limit';
    });

Need Help With Your Laravel Project?

I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.

Currently available for 2-3 new projects

Hafiz Riaz

About Hafiz

Full Stack Developer from Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.

View Portfolio

Related Errors