roundly-consulting / http-client-rate-limits-for-laravel
Rate limit outgoing requests made with Laravel's HTTP client
Package info
github.com/roundly-consulting/http-client-rate-limits-for-laravel
pkg:composer/roundly-consulting/http-client-rate-limits-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/http: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- roundly-consulting/enums-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
HTTP Client Rate Limits for Laravel
Rate limit the outgoing requests you make with Laravel's HTTP client, so you never blow past a
third-party API's quota. When a budget is spent the request waits for the next free slot
instead of earning a 429; compound windows, per-owner budgets, server-adaptive limits and
shared stores come built in.
Installation
Requires PHP 8.4 and Laravel 12 or 13.
composer require roundly-consulting/http-client-rate-limits-for-laravel
The default InMemoryStore counts per process. With several workers, share one budget by
setting HTTP_CLIENT_RATE_LIMITS_STORE to the CacheStore, RedisStore or DatabaseStore
class (the last one needs its tables:
php artisan vendor:publish --tag="http-client-rate-limits-migrations", then migrate).
Usage
Throttle any HTTP client call with the rateLimit() macro and the RateLimits facade:
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');
Combine windows, follow the server's own limits and cap the wait:
$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
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/http-client-rate-limits-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). Please see License File for more information.