API Rate Limiting in Laravel controls how many requests a user or client can make to an API within a specified timeframe. This prevents abuse, ensures fair usage, and protects server resources.
API Rate Limiting is built into Laravel's middleware system, with features introduced to secure APIs and enhance user experience by throttling requests.
Apply rate limiting to an API route:
Route::middleware('throttle:60,1')->group(function () {
Route::get('/api/data', [ApiController::class, 'index']);
});
Customize rate limiting in RouteServiceProvider
:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
RateLimiter::for('global', function (Request $request) {
return Limit::perMinute(100)->by($request->ip());
});
This ensures your API is protected while maintaining optimal performance and fairness.