pharaonic / laravel-api-idempotency
Opt-in HTTP idempotency for Laravel routes: Route::post(...)->idempotent() with atomic database or Redis reservation, request fingerprinting, safe response replay and fail-closed recovery.
Package info
github.com/Pharaonic/laravel-api-idempotency
pkg:composer/pharaonic/laravel-api-idempotency
Requires
- php: >=8.3 <8.6
- illuminate/console: ^13.0
- illuminate/contracts: ^13.0
- illuminate/database: ^13.0
- illuminate/http: ^13.0
- illuminate/redis: ^13.0
- illuminate/routing: ^13.0
- illuminate/support: ^13.0
- pharaonic/php-fingerprint: ~8.3.0 || ~8.4.0 || ~8.5.0
Requires (Dev)
- laravel/pint: ^1.20
- orchestra/testbench: ^11.0
- pestphp/pest: ^4.0
- phpstan/phpstan: ^2.1.33
- phpunit/phpunit: ^12.5.8
- predis/predis: ^1.1.10
Suggests
- ext-redis: For the redis store with the phpredis client (predis/predis works too).
Provides
None
Conflicts
None
Replaces
None
README
Opt-in Idempotency-Key handling for Laravel routes: retries replay the stored response instead of running twice.
Installation
composer require pharaonic/laravel-api-idempotency
The service provider is registered automatically. The default database store needs its table; publish the migration and run it (skip this when you only use Redis):
php artisan vendor:publish --tag=api-idempotency-migrations php artisan migrate
Publish the configuration when you need to change it:
php artisan vendor:publish --tag=api-idempotency-config
Usage
use App\Http\Controllers\OrderController; use App\Http\Controllers\PaymentController; use Illuminate\Support\Facades\Route; Route::middleware('auth:api')->group(function () { Route::post('orders', [OrderController::class, 'store'])->idempotent(); Route::post('payments', [PaymentController::class, 'store'])->idempotent(ttl: 604800); });
Clients send an Idempotency-Key header. A retry with the same key and request gets the stored response with Idempotency-Replayed: true; the same key with another request, or while the first one still runs, gets 409. This prevents duplicate execution of one key; it is not exactly-once delivery of external side effects.
Documentation
Full documentation: Laravel API Idempotency Documentation.
Support & Security
See SUPPORT.md and SECURITY.md. Contributions are welcome; see CONTRIBUTING.md.
License
MIT. See LICENSE.
