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
- Call the external API.
- Check if the response status is 429 (Too Many Requests).
- Read rate‑limit headers (if provided).
- 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) . "";
}