Looking to hire Laravel developers? Try LaraJobs

laravel-api-response maintained by sevaske

Description
The package for building clean, consistent, and predictable JSON API responses in laravel applications.
Author
sevaske
Last update
2026/01/21 12:16 (dev-main)
License
Links
Downloads
9

Comments
comments powered by Disqus

Laravel API Response

A simple library for a simple task: building consistent JSON API responses in Laravel. Fully customizable when you need it

What this package is

  • a small abstraction over JSON responses
  • a way to standardize API responses across your app
  • minimal by default, customizable by design
  • IDE-friendly

Default response format

Out of the box, the response looks like this:

{
  "success": true,
  "message": "OK",
  "data": {
    "id": 1
  }
}

Error response:

{
  "success": false,
  "message": "Validation failed",
  "errors": {
    "email": "Invalid"
  }
}

All keys and values are fully configurable via config or by replacing the payload builder.

Installation

composer require sevaske/laravel-api-response

Optional config publishing:

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

Usage

1. Dependency Injection (recommended)

use Sevaske\LaravelApiResponse\Contracts\ApiResponseContract;

class UserController
{
    public function __construct(
        private ApiResponseContract $api
    ) {}

    public function index()
    {
        return $this->api->success('OK', [
            'id' => 1,
        ]);
    }
}

2. Via response() macros

return response()->success(
    message: 'OK',
    data: ['id' => 1],
);

return response()->error(
    message: 'Validation failed',
    errors: ['email' => 'Invalid']
);

3. Via helper

return api()->success(
    message: 'OK',
    data: ['id' => 1],
);

Pagination

Pagination follows Laravel's native JSON resource behavior.

If a JsonResource or ResourceCollection wrapping a paginator is passed as data, all pagination fields generated by Laravel are preserved automatically.

There is no custom pagination format and no additional abstraction layer — the library simply remaps the data key while keeping the rest of the response intact.

Supported paginators:

  • LengthAwarePaginator (paginate())
  • Paginator (simplePaginate())
  • CursorPaginator (cursorPaginate())
use App\Http\Resources\UserResource;
use App\Models\User;

$users = User::paginate();

return api()->success(
    data: UserResource::collection($users)
);

Customization

Change response keys:

return [
    'success_key' => 'ok',
    'message_key' => 'msg',
    'data_key'    => 'results',
    'errors_key'  => 'errors',
];

Change the "success" value format:

return [
    'success_value' => 1,
    'error_value'   => 0,
];

Extending

Bind your own response implementation

use Sevaske\LaravelApiResponse\Contracts\ApiResponseContract;

$this->app->bind(ApiResponseContract::class, MyCustomApiResponse::class);

Replace the payload builder

use Sevaske\ApiResponsePayload\Contracts\ApiResponsePayloadContract;

$this->app->bind(ApiResponsePayloadContract::class, MyPayloadBuilder::class);

This allows full control over the final response structure without touching controllers.

License

MIT