PHPackages                             creativecrafts/laravel-api-response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. creativecrafts/laravel-api-response

ActiveLibrary[API Development](/categories/api)

creativecrafts/laravel-api-response
===================================

Laravel API Response is a powerful and flexible package that provides a standardized way to structure API responses in Laravel applications. It offers a range of features to enhance your API development experience, including consistent response formatting, conditional responses, pagination support, rate limiting, and more.

2.0.6(1y ago)11.4k[5 PRs](https://github.com/CreativeCrafts/laravel-api-response/pulls)MITPHPPHP ^8.3|^8.2CI passing

Since Dec 6Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/CreativeCrafts/laravel-api-response)[ Packagist](https://packagist.org/packages/creativecrafts/laravel-api-response)[ Docs](https://github.com/CreativeCrafts/laravel-api-response)[ RSS](/packages/creativecrafts-laravel-api-response/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (13)Versions (23)Used By (0)

Laravel API Response
====================

[](#laravel-api-response)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3801fbc7dfaf18f0cdd70b008c46de70b90aef08eb0f51ffe9e3821bd9a57fd8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63726561746976656372616674732f6c61726176656c2d6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/creativecraft/laravel-api-response)[![GitHub Tests Action Status](https://camo.githubusercontent.com/5f52328ae7784a08da1a31ac1c0f961d6fa458e01f2ee644287398f39ccb8337/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63726561746976656372616674732f6c61726176656c2d6170692d726573706f6e73652f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/creativecraft/laravel-api-response/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/c3fb5674144110b5727d38f9070585bfd08208e240692f18ef10cb6e0230edab/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f63726561746976656372616674732f6c61726176656c2d6170692d726573706f6e73652f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/creativecraft/laravel-api-response/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Code Coverage](https://camo.githubusercontent.com/8450919d8ba54ca78a07682cf1f289f41cd093c1660883805c0dd6c64e55179a/68747470733a2f2f636f6465636f762e696f2f67682f63726561746976656372616674732f6c61726176656c2d6170692d726573706f6e73652f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/creativecrafts/laravel-api-response)[![Total Downloads](https://camo.githubusercontent.com/9675611ed2605ebca59852fc9019820036f1bbece3d66beb248ab39a363b064c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63726561746976656372616674732f6c61726176656c2d6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/creativecrafts/laravel-api-response)

Laravel API Response is a powerful and flexible package that provides a standardized way to structure API responses in Laravel applications. It offers a range of features to enhance your API development experience, including consistent response formatting, conditional responses, pagination support, rate limiting, and more.

Table of Contents
=================

[](#table-of-contents)

```
1. [Installation](#installation)
2. [Configuration](#configuration)
3. [Basic Usage](#basic-usage)
4. [Feature](#feature)
    - [Success Response](#success-response)
    - [Error Response](#error-response)
    - [Conditional Response](#conditional-response)
    - [Pagination](#pagination)
    - [Rate Limiting](#rate-limiting)
    - [Response Compression](#response-compression)
    - [Localization](#localization)
    - [HATEOAS Links](#hateoas-links)
    - [Logging](#logging)
5. [Advanced Usage](#advanced-usage)
6. [Testing](#testing)
7. [Changelog](#changelog)
8. [Contributing](#contributing)
9. [Security Vulnerabilities](#security-vulnerabilities)
10. [Credits](#credits)
11. [License](#license)

```

1. Installation
---------------

[](#1-installation)

You can install the package via composer:

```
composer require creativecrafts/laravel-api-response
```

2. Configuration
----------------

[](#2-configuration)

The package comes with a default configuration file that you can publish to your application using the following command:

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

This will create a laravel-api-response.php file in your config directory. You can customize various aspects of the package behavior in this file.

3. Basic Usage
--------------

[](#3-basic-usage)

There are two ways to use the Laravel API Response in your controllers:

### Using Dependency Injection

[](#using-dependency-injection)

You can inject the LaravelApi class:

```
use CreativeCrafts\LaravelApiResponse\LaravelApi;

class UserController extends Controller
{
    protected $api;

    public function __construct(LaravelApi $api)
    {
        $this->api = $api;
    }

    public function index()
    {
        $users = User::all();
        return $this->api->successResponse('Users retrieved successfully', $users);
    }
}
```

### Using the Facade

[](#using-the-facade)

For a more Laravel-like experience, you can use the LaravelApiResponse facade:

```
use CreativeCrafts\LaravelApiResponse\Facades\LaravelApiResponse;

class UserController extends Controller
{
    public function index()
    {
        $users = User::all();
        return LaravelApiResponse::successResponse('Users retrieved successfully', $users);
    }
}
```

4. Feature
----------

[](#4-feature)

The Laravel API Response package provides a range of features to help you build robust and reliable APIs. Here are some of the key features:

### Success Responses

[](#success-responses)

To return a success response:

```
return $this->api->successResponse('Operation successful', $data);
```

### Error Responses

[](#error-responses)

To return an error response:

```
return $this->api->errorResponse('An error occurred', $errorCode, $statusCode);
```

### Conditional Responses

[](#conditional-responses)

For responses that support caching and conditional requests:

```
return $this->api->conditionalResponse($data, 'Data retrieved successfully');
```

This method automatically handles ETag and Last-Modified headers for efficient caching.

### Pagination

[](#pagination)

The package supports Laravel's pagination:

```
$users = User::paginate(15);
return $this->api->paginatedResponse('Users retrieved successfully', $users);
```

### Rate Limiting

[](#rate-limiting)

Rate limiting is automatically applied to API routes. You can configure the rate limit in the laravel-api-response.php config file:

```
'rate_limit_max_attempts' => env('API_RATE_LIMIT_MAX_ATTEMPTS', 60),
'rate_limit_decay_minutes' => env('API_RATE_LIMIT_DECAY_MINUTES', 1),
```

### Response Compression

[](#response-compression)

Response compression can be enabled or disabled in the config:

```
'enable_compression' => env('API_RESPONSE_COMPRESSION', true),
```

### Localization

[](#localization)

The package supports message localization. Use the localize method to translate messages:

```
$message = $this->api->localize('messages.welcome');
```

### HATEOAS Links

[](#hateoas-links)

You can include HATEOAS links in your responses:

```
$links = [
    'self' => ['href' => '/api/users/1'],
    'posts' => ['href' => '/api/users/1/posts'],
];

return $this->api->successResponse('User retrieved', $userData, 200, [], $links);
```

### Logging

[](#logging)

API responses are logged to a dedicated channel. You can configure the channel in the config file:

```
'log_channel' => 'api',
```

### Exception Handling

[](#exception-handling)

The package includes a custom exception handler that automatically formats exceptions into consistent API responses. This is enabled by default and can be configured in the config file:

```
'use_exception_handler' => env('API_USE_EXCEPTION_HANDLER', true),
```

When enabled, the exception handler will automatically catch exceptions and format them into API responses with appropriate status codes. For example:

- Validation exceptions will be formatted as validation error responses
- Model not found exceptions will be formatted as 404 error responses
- Authentication exceptions will be formatted as 401 error responses
- Authorization exceptions will be formatted as 403 error responses

This ensures consistent error responses across your API without requiring extra code in your controllers.

5. Advanced Usage
-----------------

[](#5-advanced-usage)

The Laravel API Response package provides a range of advanced features to help you build robust and reliable APIs. Here are some of the key features:

### Custom Response Structure

[](#custom-response-structure)

You can customize the response structure in the config file:

```
'response_structure' => [
    'success_key' => 'success',
    'message_key' => 'message',
    'data_key' => 'data',
    'errors_key' => 'errors',
    'error_code_key' => 'error_code',
    'meta_key' => 'meta',
    'links_key' => '_links',
    'include_api_version' => true,
],
```

### Filtering Response Fields

[](#filtering-response-fields)

You can filter the fields returned in the response:

```
$fields = ['id', 'name', 'email'];
return $this->api->successResponse('User data', $userData, 200, [], [], $fields);
```

### Custom Status Codes

[](#custom-status-codes)

You can specify custom HTTP status codes for your responses:

```
return $this->api->successResponse('Resource created', $newResource, 201);
```

### Extending with Custom Methods

[](#extending-with-custom-methods)

The LaravelApi class uses Laravel's Macroable trait, allowing you to add custom response methods at runtime:

```
use CreativeCrafts\LaravelApiResponse\LaravelApi;
use Illuminate\Support\Facades\Response;

LaravelApi::macro('teapotResponse', function ($message = "I'm a teapot") {
    return Response::json(['message' => $message], 418);
});

// Then in your controller:
return $this->api->teapotResponse();
// Or using the facade:
return LaravelApiResponse::teapotResponse("Custom teapot message");
```

This makes it easy to extend the package with your own custom response types without modifying the core code.

### Version 2: Breaking Changes

[](#version-2-breaking-changes)

Version 2 of the package introduces one breaking change.

- createdResponse method has been removed. Use successResponse instead.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Godspower Oduose](https://github.com/rockblings)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance67

Regular maintenance activity

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 74% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~39 days

Total

15

Last Release

388d ago

Major Versions

0.0.7 → 1.0.02024-03-17

1.0.0 → 2.0.02025-01-07

PHP version history (3 changes)0.0.1PHP ^8.1

1.0.0PHP ^8.2

2.0.4PHP ^8.3|^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b5e5ba42a2029e0f2fa45bd8d874c98599af2d2a1e77bff012ea07eeb0b65bc?d=identicon)[rockblings](/maintainers/rockblings)

---

Top Contributors

[![rockblings](https://avatars.githubusercontent.com/u/5190259?v=4)](https://github.com/rockblings "rockblings (57 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (11 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (9 commits)")

---

Tags

laravellaravel api responsecreativeCrafts

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/creativecrafts-laravel-api-response/health.svg)

```
[![Health](https://phpackages.com/badges/creativecrafts-laravel-api-response/health.svg)](https://phpackages.com/packages/creativecrafts-laravel-api-response)
```

###  Alternatives

[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.1k11.2M100](/packages/dedoc-scramble)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.3k3](/packages/defstudio-telegraph)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5021.9k](/packages/simplestats-io-laravel-client)[lettermint/lettermint-laravel

Official Lettermint driver for Laravel

1190.2k1](/packages/lettermint-lettermint-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
