mahbubhelal / record-api-request
A Laravel package for recording API requests via middleware with a built-in dashboard endpoint.
Requires
- php: ^8.5
- laravel/framework: ^13.35
Requires (Dev)
- driftingly/rector-laravel: ^2.7
- larastan/larastan: ^3.13
- laravel/pint: ^1.32.1
- orchestra/testbench: ^11.3
- pestphp/pest: ^5.3.4
- pestphp/pest-plugin-laravel: ^5.0.1
- pestphp/pest-plugin-phpstan: ^5.2.2
- pestphp/pest-plugin-rector: ^5.0.6
- pestphp/pest-plugin-type-coverage: ^5.0.3
- phpstan/phpstan-strict-rules: ^2.1
- rector/rector: ^2.8
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A Laravel package for recording API requests via middleware, with a built-in dashboard endpoint for browsing them.
Each request that passes through the middleware is persisted with its timestamp (to millisecond precision), path, status, response time, response length, IP, authenticated user, query string (with secret values redacted), and user agent. A paginated, sortable listing endpoint is registered at GET /api-requests.
Requirements
- PHP
^8.5 - Laravel
^13.35 - The dashboard endpoint defaults to being protected by Laravel Sanctum with an
access-api-requeststoken ability. The middleware stack is configurable — see Configuring the dashboard endpoint if you use a different auth driver.
Installation
composer require mahbubhelal/record-api-request
Publish and run the migration:
php artisan vendor:publish --tag=record-api-request-migrations php artisan migrate
Optionally publish the config file if you want to customise the dashboard route path, the middleware stack, or the redacted query parameters:
php artisan vendor:publish --tag=record-api-request-config
Recording requests
Apply the RecordApiRequest middleware to the routes you want to track. The middleware records the request during Laravel's terminate phase, so it does not add latency to the response.
For example, to track every route in your api group, add it to bootstrap/app.php:
use Mahbub\RecordApiRequest\Http\Middleware\RecordApiRequest; ->withMiddleware(function (Middleware $middleware) { $middleware->api(append: [ RecordApiRequest::class, ]); })
Or attach it to individual routes:
Route::middleware(RecordApiRequest::class)->get('/orders', ...);
Viewing recorded requests
The package registers GET /api-requests behind auth:sanctum and ability:access-api-requests by default. Issue a Sanctum token with that ability to the users who should be allowed to view the dashboard:
$token = $user->createToken('dashboard', ['access-api-requests']);
Configuring the dashboard endpoint
The middleware stack and route path are driven by config/record-api-request.php:
return [ 'route' => [ 'path' => 'api-requests', 'middleware' => [ 'api', 'auth:sanctum', 'ability:access-api-requests', ], ], 'redacted_query_parameters' => [ 'access_token', 'api_key', 'apikey', 'client_secret', 'password', 'refresh_token', 'secret', 'signature', 'token', ], ];
Replace middleware with whatever stack fits your app (e.g. ['web', 'auth'] for a session-authenticated dashboard, or a custom gate middleware) and set path if you want to mount the endpoint somewhere other than /api-requests.
Redacting secrets from recorded query strings
The value of any parameter named in redacted_query_parameters is recorded as [REDACTED], so ?page=2&token=abc is stored as page=2&token=[REDACTED]. Names match case-insensitively, and on any segment of an array or nested name (token[], auth[token]). The rest of the query string is stored exactly as sent.
Add the names your API takes secrets under, as the list replaces the defaults once you publish the config. Setting it to [] turns redaction off. Rows recorded before this was in place keep their original query strings.
Query parameters
| Parameter | Description |
|---|---|
sort |
Sort field. Prefix with - for descending. Allowed: time, status, response_time, response_length. Defaults to -time. |
per_page |
Page size, at least 1. Defaults to 15. |
page |
Page number. Defaults to 1. |
An unknown sort field or an invalid per_page is rejected with a 422 validation error.
Response shape
{
"data": {
"per_page": 15,
"current_page": 1,
"total": 120,
"last_page": 8,
"requests": [
{
"time": "2026-04-09 14:23:11.482",
"status": 200,
"response_time": "42ms",
"response_length": "1.2kB",
"ip": "203.0.113.7",
"user": "Ada Lovelace (ada@example.com)",
"url": "https://example.test/api/orders?status=active",
"user_agent": "Mozilla/5.0 ..."
}
]
}
}
Development
The package ships with a Docker-based dev environment. Common commands:
./dc buildup # build and start the containers ./dc composer install # install dependencies ./ci # every check: rector, pint, phpstan, type and line coverage (both 100%) ./ci all # apply rector and pint fixes, then run phpstan, type coverage and tests ./ci pest --filter=Name # run tests; also ./ci rector|pint|phpstan|types|coverage
License
The MIT License (MIT). See LICENSE.