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

ActiveLibrary[API Development](/categories/api)

chhw/api-response
=================

This package gives you an easier way to return RESTful API response.

1.3.0(4y ago)131MITPHPPHP &gt;=5.4

Since Jul 25Pushed 4y agoCompare

[ Source](https://github.com/wangchristine/api-response)[ Packagist](https://packagist.org/packages/chhw/api-response)[ RSS](/packages/chhw-api-response/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependencies (3)Versions (12)Used By (0)

Laravel Api Response
====================

[](#laravel-api-response)

> This package gives you an easier way to return RESTful API response.

Installation
============

[](#installation)

Install by composer

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

If you are under Laravel 5.5, please add this code in `config/app.php` below.

```

```

\[Option\] Get response config to customize debug message.

```
    $ php artisan vendor:publish --provider="CHHW\ApiResponse\ApiResponseServiceProvider"
```

Also support Lumen now!!
------------------------

[](#also-support-lumen-now)

In `bootstrap/app.php`, you should:

1. add `$app->register(CHHW\ApiResponse\ApiResponseServiceProvider::class);`

\[Option\] Get response config to customize debug message.

```
    $ cp vendor/chhw/api-response/src/config/response.php config/response.php
```

Usage
=====

[](#usage)

> Should be used in your controller.

### Define:

[](#define)

You can choose what you prefer:

1. construct: `$this->response = new ApiResponse`
2. injection: `ApiResponse $response`

### Example:

[](#example)

> General

```
 // You can set header and option in construct.
$this->response->setHeader(["lang" => "en"])->setOption(JSON_UNESCAPED_UNICODE);

// Basic usage.
return $this->response->success([1, 2])->json();
return $this->response->error("Oh no")->json();

// Custom status and code.
return $this->response->success([1, 2], 201, "code201")->json();
return $this->response->error("Oh no", 501, "code501")->json();
```

> Inline

```
return $this->response->success([1, 2])->setHeader(["lang" => "en"])->setOption(JSON_UNESCAPED_UNICODE)->json();
return $this->response->error("Oh no", 501, "code501")->setHeader(["lang" => "en"])->setOption(JSON_UNESCAPED_UNICODE)->json();
```

### \[Option\] Custom debug message:

[](#option-custom-debug-message)

If you have custom code, by default it will match `config/response.php` settings for debug message:

```
// Controller
return $this->response->error([1, 2], 500, 'custom500')->json();

// config/response.php
return [
    'code' => [
        'custom500' => 'Internal Server Error',
    ]
];
```

### Response:

[](#response)

> Success

```
 {
    "success": true,
    "detail": {
        "status": 200,
        "code": "200",
        "message": "OK"
    },
    "data": {
        ...
    },
    "link": null,
    "meta": null
 }
```

> Error

```
 {
    "success": false,
    "detail": {
        "status": 404,
        "code": "404",
        "message": "Something went wrong."
    },
    "error": {
        ...
    },
    "link": null,
    "meta": null
 }
```

Supported methods
=================

[](#supported-methods)

> $data can be array, string or object etc.

> $code for someone who want to custom internal http code.

```
public function success($data = [], $status = 200, $code = "200");
public function error($data = [], $status = 500, $code = "500");
public function setHeader($headers);
public function setOption($options);
```

Log tracing
===========

[](#log-tracing)

> Response header with an unique UUID4: `X-Trace-Id`

Collection Paginator
====================

[](#collection-paginator)

> Collection Paginator usage just like model !

> You can use Api Response and Collection Paginator together.

### Example:

[](#example-1)

```
collect([1,2,3,4,5])->paginate(10)
collect([1,2,3,4,5])->simplePaginate(15)
```

### Supported methods:

[](#supported-methods-1)

```
paginate($perPage = 15, $pageName = 'page', $page = null);
simplePaginate($perPage = 15, $pageName = 'page', $page = null);
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~54 days

Recently: every ~132 days

Total

11

Last Release

1578d ago

PHP version history (3 changes)1.0.1PHP ^7.0.0

1.0.2PHP ^5.4

1.0.4PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/b269ecb6624f0ccd1b958966fd8e45d2c1c5f81e5b0aae2696bb3666fab9e8b5?d=identicon)[wangchristine](/maintainers/wangchristine)

---

Top Contributors

[![wangchristine](https://avatars.githubusercontent.com/u/13899296?v=4)](https://github.com/wangchristine "wangchristine (20 commits)")

---

Tags

laravelresponserestful-apiresponseapilaravellumenrestful

### Embed Badge

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

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

###  Alternatives

[flugger/laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal and the elegancy of Laravel.

8901.5M5](/packages/flugger-laravel-responder)[mollie/laravel-mollie

Mollie API client wrapper for Laravel &amp; Mollie Connect provider for Laravel Socialite

3624.1M28](/packages/mollie-laravel-mollie)[api-ecosystem-for-laravel/dingo-api

A RESTful API package for the Laravel and Lumen frameworks.

3121.5M10](/packages/api-ecosystem-for-laravel-dingo-api)[obiefy/api-response

Simple Laravel package to return Json responses.

17324.6k](/packages/obiefy-api-response)[specialtactics/l5-api

Dependencies for the Laravel API Boilerplate package

3672.8k2](/packages/specialtactics-l5-api)[dragon-code/laravel-json-response

Automatically always return a response in JSON format

1118.6k1](/packages/dragon-code-laravel-json-response)

PHPackages © 2026

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