Search by

jongoly / api-standardizer

Standardized JSON API responses and global exception handling for Laravel.

Maintainers

Package info

github.com/jongoly/api-standardizer

pkg:composer/jongoly/api-standardizer

Transparency log

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.1 2026-09-04 19:41 UTC

This package is auto-updated.

Last update: 2026-09-04 23:52:02 UTC


README

provides a unified trait to standardizes success, error payload. Latest Version on Packagist Total Downloads License

A lightweight Laravel package to force consistent, clean, and predictable JSON structures across your API endpoints using a single trait.

  • standardizes all success and error responses.
  • lightweight: no external dependencies outside Laravel core framework.

Installation

via Composer:

composer require jongoly/api-standardizer

Publish Configuration

php artisan vendor:publish --tag="api-standardizer-config"

This creates a config/api-standardizer.php file in your application root:

return [
    'keys' => [
        'success' => 'success',
        'data'    => 'data',
        'message' => 'message',
        'errors'  => 'errors',
        'pagination' => 'pagination'
    ],
];

Usage

namespace App\Http\Controller;
use App\Models\User;
use Jongoly\ApiStandardizer\Concerns\ApiResponse;

class UserController extends Controller
{
    use ApiResponse;

    public function index()
    {
        \$users = User::paginate(10);

        // Return a standardized success response
        return this->success(users, 'Users fetched successfully.', 200);
    }
}

Success Response Example

return this->success(data, 'Users fetched successfully.');

Output Json (Status 200):

{
    "success": true,
    "message": "Users fetched successfully.",
    "data": [
        {"id": 1, "name": "Ali"},
        {"id": 2, "name": "Malik"}
    ],
    "pagination": {
        "total": 20,
        "count": 2,
        "per_page": 10,
        "current_page": 1,
        "total_pages": 2,
        "has_more": true
    }
}

Errir Response Example

return $this->error('Validation Failed.', 422, [
    'The Email must be valid.'
]);

Output Json(Status 422):

{
    "success": false,
    "message": "Validation Failed.",
    "errors": {
        "email": [
            "The Email must be valid."
        ]
    }
}

---