alamindev27/laravel-api-responder

A simple and consistent API response helper for Laravel.

Maintainers

Package info

github.com/alamindev27/laravel-api-responder

pkg:composer/alamindev27/laravel-api-responder

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

1.0.0 2026-01-14 18:54 UTC

This package is auto-updated.

Last update: 2026-04-15 16:25:34 UTC


README

A simple, consistent, and customizable API response helper for Laravel applications.
It provides standard JSON responses for success, error, validation errors, and paginated data.

Features

  • Standardized JSON API responses
  • Success responses with optional custom messages
  • Error responses with HTTP status codes
  • Validation error responses
  • Paginated data responses
  • Configurable via config/api-responder.php
  • Supports Laravel 10, 11, 12
  • Easy to use Facade

Installation

Install via Packagist: https://packagist.org/packages/alamindev27/laravel-api-responder

OR

Require the package using Composer:

composer require alamindev27/laravel-api-responder

Local development: If you are developing locally, use a path repository in your Laravel project composer.json:

"repositories": [
    {
        "type": "path",
        "url": "packages/laravel-api-responder"
    }
]

Then run:

composer update

Publish Config (Optional)

You can publish the config file to customize messages and response structure:

php artisan vendor:publish --provider="Alamindev27\ApiResponder\ApiResponderServiceProvider" --tag=config

This will create config/api-responder.php. You can change default success/error messages, status keys, and pagination keys there.

Usage

Success Response

use Alamindev27\ApiResponder\Facades\ApiResponder;

$datas = [
    'name' => 'MD Al-Amin', 'email' => 'alamindev27@gmail.com', 'age' => 26, 'profession' => 'Web Developer'
];

$meta = [
    "request_id" => "64f8a2c1e4d7a"
];

$message = 'Data fatch Successfully';

return ApiResponder::success($datas, $message, $meta, 200);

Output:

{
    "status": 200,
    "message": "Data fatch Successfully",
    "data": {
        "name": "Al-Amin",
        "email": "alamindev27@gmail.com",
        "age": "27"
    },
    "meta": [
        "request_id":"64f8a2c1e4d7a"
    ]
}

Error Response

    return ApiResponder::error('Something went wrong', [], 400);

Output:

{
    "status": 400,
    "message": "Something went wrong",
    "data": [],
    "meta": []
}

Validation Error Response

return ApiResponder::validationError([
    'email' => ['The email field is required.']
]);

Output:

{
    "status": 422,
    "message": "Validation Error",
    "errors": {
        "email": ["The email field is required."]
    }
}

Paginated Response

$data = User::paginate(10);
$meta = [
    "request_id" => "64f8a2c1e4d7a"
];

$message = 'Pagination data fatch Successfully';

return ApiResponder::paginate($data, $message, $meta, 200);

Output:

{
    "status": 200,
    "message": "Pagination data fatch Successfully",
    "data": [ /* user items */ ],
    "pagination": {
        "total": 100,
        "per_page": 10,
        "current_page": 1,
        "last_page": 10
    }
}

Configuration

The config file config/api-responder.php allows you to customize default response structure:

return [
    'success_status' => true,
    'success_message' => 'Success',
    'error_status' => false,
    'error_message' => 'Something went wrong',
    'pagination_keys' => [
        'data' => 'data',
        'total' => 'total',
        'per_page' => 'per_page',
        'current_page' => 'current_page',
        'last_page' => 'last_page',
    ],
    "meta": [
        "request_id":"64f8a2c1e4d7a"
    ]
];

Testing

This package includes PHPUnit tests using Orchestra Testbench Run tests:

vendor/bin/phpunit

Requirements

PHP 8.1+

Laravel 10, 11, 12

Contributing

Feel free to fork this repository and submit pull requests. Please follow PSR-12 coding standards and write tests for new features.

License

MIT License © MD Al-Amin