osama-98/laravel-response-api
A Laravel package for building consistent API responses. It provides helpers to return standardized JSON with success flags, messages, data, and HTTP status codes, simplifying controllers and keeping error and success formats uniform across your application.
ApiResponseServiceProvider. If your app disables package discovery, add the provider to bootstrap/providers.php or config/app.php.src/ApiResponse.php and src/ApiResponseBuilder.php. The 2.0.1 release specifically hardens these classes, so they are the best source of truth for available helpers.php artisan vendor:publish --provider="...\ApiResponseServiceProvider"
Only publish if you need to change defaults; otherwise rely on package defaults to simplify upgrades.created(), updated(), destroyed(), and pagination behavior, which makes those tests a reliable guide for expected response shapes and status codes.First use case: standardize CRUD controller responses instead of hand-writing response()->json(...).
public function store(StoreUserRequest $request)
{
$user = User::create($request->validated());
return ApiResponse::created($user);
}
public function update(UpdateUserRequest $request, User $user)
{
$user->update($request->validated());
return ApiResponse::updated($user);
}
public function destroy(User $user)
{
$user->delete();
return ApiResponse::destroyed();
}
Verify the exact status code and body for destroyed() against your API contract before rolling it out broadly.
ApiResponse::... from controllers and avoid mixing manual response()->json() calls with package responses. A consistent envelope is more valuable than any individual helper.created() after successful inserts, updated() after successful updates, and destroyed() after deletes. Keep the mapping in one place so status codes and JSON envelopes do not drift between controllers.data, meta, and links keys match your frontend or API client expectations.ApiResponseBuilder for non-trivial responses. When you need a custom message, status, additional metadata, or headers, build the response fluently rather than constructing arrays by hand. This keeps custom responses aligned with the package’s default envelope.bootstrap/app.php or your exception handler, render API exceptions through the same envelope. This prevents successful responses and error responses from using different JSON structures.ApiResponder service that delegates to ApiResponse. This isolates future package upgrades and gives you one place to enforce project-specific conventions.assertStatus() and assertJsonStructure() around endpoints that use created(), updated(), destroyed(), and pagination. The package’s own 100% coverage is useful, but your application still needs to verify its expected API shape.destroyed() response semantics matter. Confirm whether your version returns an empty 204 No Content response or a JSON body with 200 OK. Clients and tests often assume one or the other.created() adds a Location header. If your API contract requires it, add it explicitly through the builder or response headers and cover it with a test.ApiResponseServiceProvider is registered explicitly.php artisan config:clear during development and redeploy with php artisan config:cache in production.ApiResponse in your own service. Directly subclassing internal classes makes future upgrades harder.JsonResponse with getStatusCode(), getContent(), and headers. This is faster than guessing whether the helper or your controller added the unexpected value.How can I help you explore Laravel packages today?