PHPackages                             spatie/guzzle-rate-limiter-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. spatie/guzzle-rate-limiter-middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

spatie/guzzle-rate-limiter-middleware
=====================================

A rate limiter for Guzzle

2.1.0(1y ago)1675.0M↓11.1%1720MITPHPPHP ^7.1|^8.0

Since Jan 15Pushed 3mo ago8 watchersCompare

[ Source](https://github.com/spatie/guzzle-rate-limiter-middleware)[ Packagist](https://packagist.org/packages/spatie/guzzle-rate-limiter-middleware)[ Docs](https://github.com/spatie/guzzle-rate-limiter-middleware)[ RSS](/packages/spatie-guzzle-rate-limiter-middleware/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (12)Used By (20)

A rate limiter middleware for Guzzle
====================================

[](#a-rate-limiter-middleware-for-guzzle)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9e3cc52223434618b0f69d54d002c9589e19737761c7bee351624e5f926b63c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f67757a7a6c652d726174652d6c696d697465722d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/guzzle-rate-limiter-middleware)[![Build Status](https://camo.githubusercontent.com/524f5fae07a937b2bcd8a0ae9e65b78899cacd003f04aa811a4ef3aa2e9ab6e2/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f67757a7a6c652d726174652d6c696d697465722d6d6964646c65776172652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/guzzle-rate-limiter-middleware)[![Quality Score](https://camo.githubusercontent.com/721c1b0b4d2966ffceed7a5488e35e1fa0c8f6700ae3580db6063115bb7d2899/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f67757a7a6c652d726174652d6c696d697465722d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/guzzle-rate-limiter-middleware)[![StyleCI](https://camo.githubusercontent.com/9ce0523ea3f2ce84b7f4f94f51f5be9f451caf2f4f86c3d7fc6ebf053b8b6389/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3136353639333635372f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/165693657)[![Total Downloads](https://camo.githubusercontent.com/9ad8b72066d74be7de582f6046872165cc3df3ded747c100d2b7a7bbc83ae204/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f67757a7a6c652d726174652d6c696d697465722d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/guzzle-rate-limiter-middleware)

A rate limiter middleware for Guzzle. Here's what you need to know:

- Specify a maximum amount of requests per minute or per second
- When the limit is reached, the process will `sleep` until the request can be made
- Implement your own driver to persist the rate limiter's request store. This is necessary if the rate limiter needs to work across separate processes, the package ships with an `InMemoryStore`.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/3c7d63c2b0ec0fe2791043c4c4776091b69c3f4ff024453971dc2c6aed0e1e05/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f67757a7a6c652d726174652d6c696d697465722d6d6964646c65776172652e6a70673f743d31)](https://spatie.be/github-ad-click/guzzle-rate-limiter-middleware)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/guzzle-rate-limiter-middleware
```

Usage
-----

[](#usage)

Create a [Guzzle middleware stack](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) and register it on the client.

```
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware;

$stack = HandlerStack::create();
$stack->push(RateLimiterMiddleware::perSecond(3));

$client = new Client([
    'handler' => $stack,
]);
```

You can create a rate limiter to limit per second or per minute.

```
RateLimiterMiddleware::perSecond(3); // Max. 3 requests per second

RateLimiterMiddleware::perMinute(5); // Max. 5 requests per minute
```

Custom stores
-------------

[](#custom-stores)

By default, the rate limiter works in memory. This means that if you have a second PHP process (or Guzzle client) consuming the same API, you'd still possibly hit the rate limit. To work around this issue, the rate limiter's state should be persisted to a cache. Implement the `Store` interface with your own cache, and pass the store to the rate limiter.

```
use MyApp\RateLimiterStore;
use Spatie\GuzzleRateLimiterMiddleware\RateLimit;

RateLimiterMiddleware::perSecond(3, new RateLimiterStore());
```

A Laravel example of a custom `Store`:

```
