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

TMcCann's avatar

Rate Limiting/Throttling Outgoing API Requests

Hey everyone. I have an laravel app that sends data to an external API that has a rate limit of 1000 requests per minute. My database query returns roughly 20,000 records which I then loop through and send each as a POST request. What would be the best way to handle this?

0 likes
1 reply
Snapey's avatar

I was not really sure how to implement Laravel's rate limiter so I wrote this in tinkerwell

use Illuminate\Support\Facades\RateLimiter;

    foreach(range(0,1000) as $i) {
      echo $i;

      RateLimiter::hit('testing');

      if (RateLimiter::tooManyAttempts('testing', $perMinute = 50)) {
        sleep(RateLimiter::availableIn('testing'));
      }

    }

This might give you idea of how you could incorporate the rate limiter into your loop

'testing' is a unique name for the limiter that allows you to group limits to a service from multiple sources in your code.

the hit function counts the accesses. It will count the requests and limit it to the number per minute.

Then we can test if the limit was reached, and sleep until some more requests are possible.

1 like

Please or to participate in this conversation.