Rate limiting custom responses in Laravel
Using Laravel rate limiting middleware. When a request is rate limited, the default response is a 429 JSON response that does not match our API format.
How do you customize the rate limit exceeded response?
In Handler.php (or bootstrap/app.php in L11), register a custom handler for ThrottleRequestsException: $exceptions->render(function (ThrottleRequestsException $e) { return response()->json([...], 429); });
RateLimiter::for() lets you define named limiters with custom responses. The response() callback on the limit object controls what gets returned when the limit is hit.
Also include the Retry-After header in your custom response. Clients can use it to back off intelligently instead of immediately retrying.
We return different messages depending on which limiter was hit: API key rate limit vs IP rate limit vs user-level limit. The ThrottleRequestsException has a retryAfter property and the limiter name.
```php blocks are runnable.