PHPackages                             tequilarapido/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. tequilarapido/api-response

ActiveLibrary[API Development](/categories/api)

tequilarapido/api-response
==========================

A simple way to return well formatted json responses in a Laravel application.

v1.1.6(6y ago)413.3k↓50%[8 PRs](https://github.com/tequilarapido/api-response/pulls)MITPHPPHP ^7.1.3

Since Oct 8Pushed 4y ago3 watchersCompare

[ Source](https://github.com/tequilarapido/api-response)[ Packagist](https://packagist.org/packages/tequilarapido/api-response)[ Docs](https://github.com/tequilarapido/api-response)[ RSS](/packages/tequilarapido-api-response/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (23)Used By (0)

A simple way to return well formatted json responses in a Laravel application.

[![Latest Version on Packagist](https://camo.githubusercontent.com/58dc158b4a61dcc2a1be2f0f45ec321021bafa99f77fb85808b5ba6920361705/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74657175696c6172617069646f2f6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tequilarapido/api-response)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/eef210615964f09bf449c6aa6abf2dd0c53dd6632cf53a56a28823e0c344cf7b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f74657175696c6172617069646f2f6170692d726573706f6e73652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/tequilarapido/api-response)[![StyleCI](https://camo.githubusercontent.com/fec0b8ecf85e2c11d637bdec68cf1c7485ba623101227f09ba0c85eb631c7dc6/68747470733a2f2f7374796c6563692e696f2f7265706f732f37303236313539322f736869656c64)](https://styleci.io/repos/70261592)[![Quality Score](https://camo.githubusercontent.com/ff10f522a210600265fae280d18175a11dc27cdc0345a17f08afb1391186d89d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f74657175696c6172617069646f2f6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/tequilarapido/api-response)[![Code Coverage](https://camo.githubusercontent.com/3c86b59e04f1cfafb3ca4ca2725fb48506757cf50c0f02b95318809f92654406/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f74657175696c6172617069646f2f6170692d726573706f6e73652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/tequilarapido/api-response/?branch=master)

 [![Laravel Translation Sheet](https://camo.githubusercontent.com/d8511fc19d9c92620d4eb2ef0d1b138a363d0016c9001ec0d741d11fc0798638/68747470733a2f2f73392e706f7374696d672e6f72672f6663736d316f6538762f696c6c757374726174696f6e2e6a7067)](https://camo.githubusercontent.com/d8511fc19d9c92620d4eb2ef0d1b138a363d0016c9001ec0d741d11fc0798638/68747470733a2f2f73392e706f7374696d672e6f72672f6663736d316f6538762f696c6c757374726174696f6e2e6a7067)

Installation
------------

[](#installation)

You can install the package using composer

```
$ composer require tequilarapido/api-response
```

Add the service provider

```
Tequilarapido\ApiResponse\ApiResponseServiceProvider::class
```

Usage
-----

[](#usage)

This package comes with a helper function `api_response()`, as sugar syntax to using `app(Tequilarapido\ApiResponse\ApiResponse::class)`

### Return a response from an item (Array / Associative array / Object/)

[](#return-a-response-from-an-item-array--associative-array--object)

```
return api_response()->item(['result`' => 'success'])
```

Result :

```
    {
        "data":
        {
            "result":
            "success"
        }
    }

```

### Return a response from a collection

[](#return-a-response-from-a-collection)

```
return api_response()->item(collect(['Apple', 'Orange', 'Kiwi']))
```

Result :

```
    {
        "data":
            {
                "Apple",
                "Orange",
                "Kiwi",
            }
    }

```

### Return a response from a paginated collection

[](#return-a-response-from-a-paginated-collection)

```
return api_response()->item(collect(['Apple', 'Orange', 'Kiwi']))
```

Result :

```
    {
        "data": [1, 2, 3],
        "meta": {
            "pagination": {
                "count": 3,
                "current_page": 1,
                "links": {
                    "next": "/?page=2"
                },
                "per_page": 3,
                "total": 15,
                "total_pages": 5
            }
         }
    }

```

### Transformers

[](#transformers)

You can use transformers along with `item()`, `collection()`, and `paginatedCollection` methods to transform results before returning the responses.

To learn more about the concept behind transformers, please read the [League\\Fractal documentation](http://fractal.thephpleague.com/transformers/).

Put in a simple way, a Transformer class - must extends `League\Fractal\TransformerAbstract`- and contains a method `transform()` that take the raw value/object as an argument and transform it the way we want.

Let's assume that we need to return an api response containing a collection of books retreived from database, and we want to format/enhance/restrict results.

```
class BookTransformer extends TransformerAbstract
{
    public function transform($book)
    {
        return [
            'id' => (int)$book->id,
            'title' => strtoupper($book->title),
            'price' => '$' . $book->price,
            'published_at' => $book->published_at->format('d/m/Y'),
            'url' => 'https://store.books.com/books/' . $book->id
        ];
    }
}
```

```
Route::get('/books', function () {

    // ie. Book::all()
   $books = collect([
       (object)['title' => 'An awesome book', 'id' => '1', 'price' => 10, 'published_at' => Carbon::createFromFormat('Y-m-d', '2016-10-01')],
       (object)['title' => 'A second awesome book', 'id' => '2', 'price' => 100, 'published_at' => Carbon::createFromFormat('Y-m-d', '2016-10-02')],
   ]);

   return api_response()->collection($books, new BookTransformer);
});
```

Result:

```
{
    "data": [
        {
            "id": 1,
            "price": "$10",
            "published_at": "01/10/2016",
            "title": "AN AWESOME BOOK",
            "url": "https://store.books.com/books/1"
        },
        {
            "id": 2,
            "price": "$100",
            "published_at": "02/10/2016",
            "title": "A SECOND AWESOME BOOK",
            "url": "https://store.books.com/books/2"
        }
    ]
}

```

### Attaching cookies

[](#attaching-cookies)

For `item()`, `collection()`, and `paginatedCollection` methods the returned result is a built `Illuminate\Http\Response` object.

To be able to attach cookies you need to instruct `api_response()` methods to not build the response by setting the $built argument to false, attach the cookie and then build the response.

```
return api_response()
    ->item(['result`' => 'success'], null, null, false)
    ->withCookie(new \Symfony\Component\HttpFoundation\Cookie('name', 'value'))
    ->build();
```

### Attaching headers

[](#attaching-headers)

For `item()`, `collection()`, and `paginatedCollection` methods the returned result is a built `Illuminate\Http\Response` object.

To be able to attach headers you need to instruct `api_response()` methods to not build the response by setting the $built argument to false, attach the header and then build the response.

```
return api_response()
    ->item(['result`' => 'success'], null, null, false)
    ->withHeader('X-CUSTOM', 'customvalue')
    ->build();
```

### Other useful methods

[](#other-useful-methods)

MethodUsage`api_response()->noContent()`Return an empty response with 204 No Content header.`api_response()->errorNotFound()`Return a 404 Not found.`api_response()->errorBadRequest()`Return a 404 Bad Request.`api_response()->errorForbidden()`Return a 403 Forbidden.`api_response()->errorUnAUthorized()`Return a 401 Unauthorized.`api_response()->errorInternal()`Return a 500 Internal Error.`api_response()->error()`Return a more customizable error (Accept MessageBag instance, staus code ...)Changelog
---------

[](#changelog)

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

Testing
-------

[](#testing)

```
$ composer test
```

Security
--------

[](#security)

If you discover any security related issues, please email :author\_email instead of using the issue tracker.

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

[](#contributing)

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

Credits
-------

[](#credits)

- [Nassif Bourguig](https://github.com/nbourguig)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 98% 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 ~103 days

Recently: every ~91 days

Total

13

Last Release

2267d ago

Major Versions

0.0.2 → 1.0.02017-01-25

PHP version history (2 changes)0.0.1PHP &gt;=5.6.4

1.1.0PHP ^7.1.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/2357415d73716a023601d0bc95812c5160ff977f17da9d43fbdc40b047a05c50?d=identicon)[nbourguig](/maintainers/nbourguig)

---

Top Contributors

[![nbourguig](https://avatars.githubusercontent.com/u/276832?v=4)](https://github.com/nbourguig "nbourguig (49 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

laravel-package

### Embed Badge

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

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

###  Alternatives

[darkaonline/l5-swagger

OpenApi or Swagger integration to Laravel

2.9k34.0M112](/packages/darkaonline-l5-swagger)[echolabsdev/prism

A powerful Laravel package for integrating Large Language Models (LLMs) into your applications.

2.3k388.3k10](/packages/echolabsdev-prism)[yajra/laravel-datatables-fractal

Laravel DataTables Fractal Plugin.

966.9M29](/packages/yajra-laravel-datatables-fractal)[sburina/laravel-whmcs-up

WHMCS API client and user provider for Laravel

271.3k](/packages/sburina-laravel-whmcs-up)

PHPackages © 2026

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