marcin-orlowski/laravel-api-response-builder
Build consistent, structured JSON API responses in Laravel. Standardize success and error payloads, map custom internal codes to HTTP statuses, and keep every endpoint returning the same predictable, documented format.
Architecture fit
data).success, code, message, data, errors — which is a contract decision, not just a library choice. This is a good fit for internal or partner-facing REST APIs consumed by first-party mobile/web clients that benefit from a single predictable shape.ApiCode implementations) becomes the shared error taxonomy between backend and client teams.Integration feasibility
vendor:publish for the config, optional facade alias. A single endpoint can be converted in an afternoon; no schema or infrastructure changes required.ApiCode registry/enum for business errors, (b) wiring the package into Laravel's exception handler so uncaught exceptions and validation failures render through the same builder, and (c) writing contract tests to freeze the envelope.Technical risk
bootstrap/app.php with withExceptions(). Verify the supported version matrix against our Laravel/PHP targets before committing; a version gap here blocks adoption entirely.success boolean, and code semantics are client-visible. Upstream minor releases that alter defaults (or a config file we haven't published) can silently change output.ResponseBuilder facade and a DI-friendly ApiResponse contract. If we standardize on the deprecated path, we inherit a future migration.Key questions
bootstrap/app.php-style exception configuration?ValidationException and Laravel's Handler::render, or do we need a custom renderable to avoid duplicate envelopes?ApiCode interface the supported extension point for our business error taxonomy, and is it stable across the versions we'd pin?APP_DEBUG=true — does it suppress stack traces and internal messages, or does it leak them into errors?Stack fit
JsonResponse — resources become the data payload rather than replacing the builder.Migration path
ApiCode values per bounded context, define the numeric ranges/reservation policy, and publish the envelope + code list to client teams before any code ships.ValidationException, AuthenticationException, ModelNotFoundException, and a base ApiException. Anything not routed here becomes a leak./v2) for public consumers.Compatibility
composer.json and record the Laravel/PHP support window in our dependency register; treat a Laravel major upgrade as a blocking dependency review.ApiController base class, or Handler::render customization; two competing envelope implementations is the most common failure mode here.Sequencing
Maintenance
Support
ApiCode, how to change the envelope, who approves client-visible response changes.Scaling
Failure modes
APP_DEBUG=true may leak stack traces and config values into the response.data/message key names without a test failure.code field: teams conflating HTTP status and business code, producing clients that branch on the wrong signal.success: false with HTTP 200 hides errors from status-code-based alerting; add structured logging on the API code field.Ramp-up
response()->json().How can I help you explore Laravel packages today?