PHPackages                             nishit/laravel-idempotent - 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. nishit/laravel-idempotent

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

nishit/laravel-idempotent
=========================

Laravel middleware to make POST, PUT, PATCH requests idempotent.

00PHP

Since Apr 8Pushed 3mo agoCompare

[ Source](https://github.com/nishitsureka/laravel-idempotent)[ Packagist](https://packagist.org/packages/nishit/laravel-idempotent)[ RSS](/packages/nishit-laravel-idempotent/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Idempotent
==================

[](#laravel-idempotent)

[![License: MIT](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://opensource.org/licenses/MIT)

Laravel Idempotent is a **lightweight middleware package** to make POST, PUT, and PATCH requests idempotent.
It prevents duplicate processing (e.g., double orders, repeated payments, or accidental form resubmissions).

---

Features
--------

[](#features)

- Automatic generation of **idempotency keys** based on request path + payload.
- Optional manual idempotency via `Idempotency-Key` header.
- Configurable **modes**:
    - `replay` – return cached response for duplicate requests
    - `block` – return HTTP status 409 or configured status
- Cache-based storage (supports `cache` or `redis`).
- Route-specific middleware; no need to apply globally.
- Logging support for debugging duplicate requests.

---

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

[](#installation)

Install via Composer:

```
composer require nishit/laravel-idempotent
```

Publish the configuration file:

```
php artisan vendor:publish --provider="Nishit\LaravelIdempotent\LaravelIdempotentServiceProvider" --tag=config
```

---

Configuration
-------------

[](#configuration)

Edit the published `config/idempotent.php`:

```
return [
    'ttl' => 3600,                   // Cache duration in seconds
    'storage' => 'cache',            // 'cache' or 'redis'
    'duplicate_response' => 409,     // HTTP status for duplicates in block mode
    'enable_logging' => env('APP_DEBUG', false),
    'mode' => 'replay',              // 'replay' or 'block'
];
```

KeyDescription`ttl`How long the request response will be cached.`storage`Choose your cache driver (`cache` or `redis`).`duplicate_response`HTTP status returned for duplicates in block mode.`enable_logging`Log duplicate requests for debugging.`mode``replay` returns cached response, `block` returns an error.---

Usage
-----

[](#usage)

Apply middleware to selected routes:

```
use App\Http\Controllers\OrderController;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;

// Single route
Route::post('/submit', function (\Illuminate\Http\Request $request) {
    return response()->json(['message' => 'Processed', 'data' => $request->all()]);
})->middleware('idempotent');

Route::post('/order', [OrderController::class, 'create'])->middleware('idempotent');

// Or group routes
Route::middleware(['idempotent'])->group(function () {
    Route::post('/order', [OrderController::class, 'create']);
});
```

> Only routes using the middleware will have idempotency applied. Other routes remain unaffected.

---

How It Works
------------

[](#how-it-works)

1. Middleware generates a unique key per request using:

    - Request path
    - Request payload (POST / PUT / PATCH)
    - Optional `Idempotency-Key` header
2. If the key **exists** in cache:

    - **Replay mode** → returns cached response
    - **Block mode** → returns HTTP 409 or configured status
3. If the key **does not exist**, the request is processed and the response is cached for future duplicates.

---

Testing
-------

[](#testing)

Use `curl` to test idempotency:

```
# First request
curl -X POST http://127.0.0.1:8000/api/submit \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com"}'

# Repeat request with same payload or manual Idempotency-Key header
curl -X POST http://127.0.0.1:8000/api/submit \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 123456" \
  -d '{"name":"John","email":"john@example.com"}'
```

- In **replay** mode, the second request returns the cached response.
- In **block** mode, the second request returns HTTP `409` (or the configured status).

---

Logging
-------

[](#logging)

Enable logging in `config/idempotent.php`:

```
'enable_logging' => true,
```

Duplicate requests will be logged in `storage/logs/laravel.log`:

```
[2026-04-07 09:24:00] local.INFO: duplicate found {"key":"idempotent_"}

```

---

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT License](https://opensource.org/licenses/MIT).

###  Health Score

18

—

LowBetter than 7% of packages

Maintenance54

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/259904?v=4)[Nishit](/maintainers/Nishit)[@nishit](https://github.com/nishit)

---

Top Contributors

[![nishitsureka](https://avatars.githubusercontent.com/u/65530839?v=4)](https://github.com/nishitsureka "nishitsureka (4 commits)")

### Embed Badge

![Health badge](/badges/nishit-laravel-idempotent/health.svg)

```
[![Health](https://phpackages.com/badges/nishit-laravel-idempotent/health.svg)](https://phpackages.com/packages/nishit-laravel-idempotent)
```

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25026.1M82](/packages/php-http-cache-plugin)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87965.9k122](/packages/httpsoft-http-message)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69127.2k](/packages/serpapi-google-search-results-php)[swoft/websocket-server

swoft websocket server component

16135.7k5](/packages/swoft-websocket-server)[thesis/nats

Async (fiber based) client for Nats.

754.4k](/packages/thesis-nats)[jasny/http-signature

Implementation of the IETF HTTP Signatures draft RFC

10104.7k](/packages/jasny-http-signature)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
