How to Check External API Throttle Limit in Laravel

Published: (January 10, 2026 at 12:02 AM EST)
1 min read
Source: Dev.to

Source: Dev.to

The Idea

  1. Call the external API.
  2. Check if the response status is 429 (Too Many Requests).
  3. Read rate‑limit headers (if provided).
  4. Handle throttling gracefully.

Implementation

Basic throttle check

use Illuminate\Support\Facades\Http;

public function checkApiThrottle()
{
    $response = Http::withOptions([
        // Prevent exceptions on HTTP errors
        'http_errors' => false,
    ])->get({API-ENDPOINT});

    // Check if API is throttling
    if ($response->status() === 429) {
        return response()->json([
            'throttled'   => true,
            'message'     => 'API rate limit exceeded',
            'retry_after' => $response->header('Retry-After'),
        ], 429);
    }

    // Read rate‑limit headers (if available)
    $rateLimit = [
        'limit'     => $response->header('X-RateLimit-Limit'),
        'remaining' => $response->header('X-RateLimit-Remaining'),
        'reset'     => $response->header('X-RateLimit-Reset'),
    ];

    return response()->json([
        'throttled'          => false,
        'rate_limit_headers'=> $rateLimit,
        'data'               => $response->json(),
    ]);
}

Alternative approach with a maximum call count

use Illuminate\Support\Facades\Http;

public function checkApiThrottle()
{
    $maxCall = 10;
    $count   = 0;
    $responses = [];

    while ($count status();
        $count++;

        $responses[] = [
            'statusCode' => $statusCode,
            'count'      => $count,
        ];

        // Stop if the request is not successful (e.g., throttled)
        if ($statusCode !== 200) {
            break;
        }
    }

    echo "" . print_r($responses, true) . "";
}
Back to Blog

Related posts

Read more »

Day 1 - class-booking-system Project

Day 1: Starting a Laravel Class Booking System What I Did Today I decided to build a class booking system with Laravel. I haven't used Laravel in production be...