yared/api-response

Advanced API response formatter for Laravel

Maintainers

Package info

github.com/yared-ayele-debela/Laravel-api-response

pkg:composer/yared/api-response

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-03-06 14:32 UTC

This package is auto-updated.

Last update: 2026-05-06 15:34:31 UTC


README

Advanced API response formatter for Laravel. Provides consistent, professional JSON responses for your API.

Installation

composer require yared/api-response

Or for local development, add to your Laravel project's composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "../laravel-api-response"
        }
    ]
}

Then:

composer require yared/api-response

Publish Config

php artisan vendor:publish --tag=api-response-config

Usage

Success Response

use Yared\ApiResponse\Facades\ApiResponse;

return ApiResponse::success($user, "User fetched successfully");

Error Response

return ApiResponse::error("User not found", 404);

Pagination

return ApiResponse::paginate(User::paginate(10));

Validation Errors

return ApiResponse::error("Validation failed", 422, $validator->errors());

Example Output

{
    "success": true,
    "message": "Users retrieved",
    "data": [...],
    "meta": {
        "request_id": "f24e4f21-...",
        "timestamp": "2026-03-05T22:50:00+00:00"
    }
}

Configuration

Edit config/api-response.php to customize:

  • success_key - Key for success flag
  • message_key - Key for message
  • data_key - Key for data payload
  • meta_key - Key for meta information
  • request_id - Include unique request ID
  • include_timestamp - Include response timestamp

Middleware

Add FormatApiResponse middleware to API routes for automatic response formatting. Register in app/Http/Kernel.php:

protected $middlewareAliases = [
    // ...
    'api.format' => \Yared\ApiResponse\Middleware\FormatApiResponse::class,
];

License

MIT