Building a REST API is only part of creating a secure Laravel application. You also need to control how frequently clients can access your endpoints. Laravel API rate limiting helps prevent excessive requests, API abuse, automated traffic, and unnecessary server load.
Laravel includes built-in API rate limiting, so in many applications you can protect endpoints without installing an additional package.
Why Use API Rate Limiting?
Without request limits, a user or automated script could send hundreds or thousands of requests within a short period.
A good Laravel REST API security strategy uses rate limiting to:
- Prevent excessive API requests
- Reduce automated API abuse
- Protect server resources
- Improve API availability
- Control requests from individual users or IP addresses
Basic Laravel Rate Limiting
You can apply Laravel's throttle middleware directly to an API route:
Route::middleware('throttle:60,1')->group(function () { Route::get('/profile', [ProfileController::class, 'show']);});Here:
60 = maximum requests1 = time window in minutesThis allows a client to make up to 60 requests per minute.
You can also protect a single endpoint:
Route::get('/reports', [ReportController::class, 'index']) ->middleware('throttle:30,1');This limits the endpoint to 30 requests per minute.
Handling the 429 Response
When a client exceeds the configured limit, Laravel returns an HTTP:
429 Too Many RequestsAPI clients should handle this response properly instead of repeatedly retrying the request immediately.
Rate Limiting Authenticated Users
For authenticated APIs, limits can also be associated with individual users. This is particularly useful when building Laravel APIs using Sanctum or other authentication systems.
A sensible Laravel API security setup combines authentication, validation, authorization, and rate limiting rather than relying on a single security mechanism.
Common Rate Limiting Problems
One common problem is setting limits too low. Legitimate users may start receiving 429 Too Many Requests responses during normal application usage.
Another issue occurs when developers protect some endpoints but forget expensive routes such as search, reports, exports, or external API integrations.
Choose limits according to the cost and expected usage of each endpoint.
Conclusion
Laravel API rate limiting is a simple but important way to protect a REST API from excessive traffic and abuse. Laravel's built-in throttle middleware makes it easy to define request limits while keeping your API responsive and secure.
For production applications, combine API rate limiting, authentication, authorization, validation, logging, and monitoring to build a stronger Laravel REST API.