Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

flynnmj's avatar

Trying to log API throttling

Been giving a task to put some information in the logs when a client hits their throttle limit. Have had some success making these changes in class RouteServiceProvider.php. The problem I'm facing now is that the response is missing the X-RateLimit* headers. Does anyone know how those headers are set? The getHeaders function in the ThrottleRequests appears to create the headers I need, but I'm not sure how to get the info for the function parameters.

    protected function configureRateLimiting()
    {
        RateLimiter::for(
            'api',
            fn (Request $request) => Limit::perMinute(60)
            ->by(optional($request->user())->id ?: $request->ip())
            ->response(function () {
              Log::info('DEBUG: Put throttle info here');
              abort(429, "Too Many Attempts.");
            })
        );
    }`
0 likes
5 replies
flynnmj's avatar

@siangboon I was inspecting my response data, that's how I knew I had a problem. :) Thanks to the other answer I now know what parameters the callback function will be called with. Now I can include the headers created by the ThrottleRequests class.

Niush's avatar
Niush
Best Answer
Level 50
use Illuminate\Http\Exceptions\ThrottleRequestsException;
   
protected function configureRateLimiting()
{
    RateLimiter::for(
        'api',
        fn (Request $request) => Limit::perMinute(60)
           ->by(optional($request->user())->id ?: $request->ip())
           ->response(function ($request, $headers) {
               Log::info('DEBUG: Put throttle info here');
               abort(429, "Too Many Attempts.", $headers);
           });
    );
}`
1 like
flynnmj's avatar

@Niush Thank you. That gives me the headers I need to send in my response. I was digging through RateLimit, Limit, ThrottleRequests and ThrottleRequestsException but didn't see the solution. With your answer I finally understand where the callback is being used and that it will be called the $request and $header as parameters.

flynnmj's avatar

I made a PR to the Laravel docs to include the 2 parameters.

Please or to participate in this conversation.