### Throttle HTTP client calls with rateLimit() and RateLimits facade Source: https://github.com/roundly-consulting/http-client-rate-limits-for-laravel/blob/main/README.md Use the rateLimit() macro on the HTTP client with the RateLimits facade. The first example allows 5 requests per second; the sixth waits for a free slot. The second example allows 30 per minute with a separate budget per account. ```php use Illuminate\Support\Facades\Http; use RoundlyConsulting\HttpClientRateLimits\Facades\RateLimits; // 5 requests per second; the sixth waits for a free slot, then goes out. Http::rateLimit(RateLimits::perSecond(5))->get('https://api.example.com/things'); // 30 per minute, with a separate budget per account. Http::rateLimit(30, by: 'acct-1')->get('https://api.example.com/orders'); ``` -------------------------------- ### Install package via Composer Source: https://github.com/roundly-consulting/http-client-rate-limits-for-laravel/blob/main/README.md Requires PHP 8.4 and Laravel 12 or 13. The default InMemoryStore counts per process; for shared budgets set HTTP_CLIENT_RATE_LIMITS_STORE to CacheStore, RedisStore, or DatabaseStore (the latter needs its tables published and migrated). ```bash composer require roundly-consulting/http-client-rate-limits-for-laravel ``` -------------------------------- ### Combine windows, adapt to server limits, and cap wait Source: https://github.com/roundly-consulting/http-client-rate-limits-for-laravel/blob/main/README.md Build a limit that applies both per-second and per-minute windows (strictest wins), adapts to Retry-After / X-RateLimit-* headers, and throws RateLimitExceededException after 5 seconds. Use remaining() to check requests left without sending a request. ```php $limit = RateLimits::perSecond(5) ->alongside(RateLimits::perMinute(100)) // both windows apply; the strictest wins ->adaptive() // honour Retry-After / X-RateLimit-* headers ->maxWait(5_000); // throw RateLimitExceededException past 5 s Http::rateLimit($limit)->get('https://api.example.com/report'); $limit->remaining(); // requests left in the window, without sending one ``` -------------------------------- ### Publish and migrate DatabaseStore tables Source: https://github.com/roundly-consulting/http-client-rate-limits-for-laravel/blob/main/README.md Required only when using the DatabaseStore. Run these commands to create the necessary tables for the rate limit store. ```bash php artisan vendor:publish --tag="http-client-rate-limits-migrations" ``` ```bash php artisan migrate ```