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

ActiveLibrary[API Development](/categories/api)

masudrana/api-response
======================

Simple Laravel package to return Json responses.

v0.1.1(2y ago)016↓100%MITPHPPHP ^7.3|^8.0|^8.2

Since Nov 30Pushed 2y agoCompare

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

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

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

[](#laravel-api-response)

[![Build Status](https://camo.githubusercontent.com/4bd874bfaef2e91a8f9fb4d13c20f97af82ecc3491f110babb1a7254f12b5b60/68747470733a2f2f7472617669732d63692e6f72672f6d6173756472616e612f6170692d726573706f6e73652e7376673f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/4bd874bfaef2e91a8f9fb4d13c20f97af82ecc3491f110babb1a7254f12b5b60/68747470733a2f2f7472617669732d63692e6f72672f6d6173756472616e612f6170692d726573706f6e73652e7376673f6272616e63683d6d6173746572)[![StyleCI](https://camo.githubusercontent.com/54bc105059e4f6640afc326e77202e432a68ddf20770d521c9f0ed38bf52284e/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3230363938313135372f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/206981157)[![Packagist](https://camo.githubusercontent.com/4b51e717016ef872b055cd082314f07ccf0dea4b43e638eedaff8a874518ac57/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d6173756472616e612f6170692d726573706f6e7365)](https://camo.githubusercontent.com/4b51e717016ef872b055cd082314f07ccf0dea4b43e638eedaff8a874518ac57/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d6173756472616e612f6170692d726573706f6e7365) [![Packagist Version](https://camo.githubusercontent.com/83f53361d08378e1320b98599809ef1ac38467264ccff6055a10e45917d9dae2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6173756472616e612f6170692d726573706f6e7365)](https://camo.githubusercontent.com/83f53361d08378e1320b98599809ef1ac38467264ccff6055a10e45917d9dae2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6173756472616e612f6170692d726573706f6e7365)

Simple Laravel API response wrapper.

---

[![API response code sample](https://camo.githubusercontent.com/59774559b15693c991a07ff64e308f837a5c8921245e52268b985bf5982b7783/68747470733a2f2f692e6962622e636f2f5335624c774b632f636172626f6e2e706e67)](https://camo.githubusercontent.com/59774559b15693c991a07ff64e308f837a5c8921245e52268b985bf5982b7783/68747470733a2f2f692e6962622e636f2f5335624c774b632f636172626f6e2e706e67)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#)

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

[](#installation)

1. Install the package through composer:

    `$ composer require masudrana/api-response`
2. Register the package service provider to the providers array in `app.php` file:

    `MasudRana\API\ApiResponseServiceProvider::class`
3. Register the package facade alias to the aliases array in `app.php` file:

    `'API' => MasudRana\API\Facades\API::class,`
4. And finally you can publish the config file:

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

Note: You could also include "`use MasudRana\API\Facades\API;`" at the top of the class, but we recommend not to.

Basic usage
-----------

[](#basic-usage)

There are to ways of utilizing the package: using the `facade`, or using the `helper` function. On either way you will get the same result, it is totally up to you.

#### Facade:

[](#facade)

```
use API;

...

public function index()
{
    $users = User::latest()->take(5)->get();

    return API::response(200, 'Latest 5 Users', $users);
}
```

Note: If you decide not to register the service provider and the facade, alias then you need to include `use MasudRana\API\Facades\API;` at the top of the class, but we recommend not to.

#### Helper function:

[](#helper-function)

```
public function index()
{
    $users = User::latest()->take(5)->get();

    return api()->response(200, 'Latest 5 Users', $users);
}
```

Advanced usage
--------------

[](#advanced-usage)

The `response()` method accepts three mandatory parameters:

- `int $status`
- `string $message`
- `string | array $data`

For example, in the below example we are calling the `response()` method thru the facade and we are passing the following parameters: `200` as the status code, `User list` as the message and `$users` (a collection of users) as the data.

```
use API;

...

public function index()
{
    $users = User::latest()->take(5)->get();

    return API::response(200, 'Latest 5 Users', $users);
}
```

This is the result:

```
{
    "STATUS": 200,
    "MESSAGE": "Latest 5 Users",
    "DATA": [
        {"name": "Obay Adam", ...}
    ]
}
```

If you need more data other than the defaults `STATUS`, `MESSAGE`, and `DATA` attributes on your json response, you could pass as many parameters as you need after `$data`. However, we do recommend formating the extra parameters as a `$key => $value` array.

As you can see in the below example, we are calling the `api()` helper and we are passing the following parameters: `200` as the status code, `User list` as the message, `$users` (a collection of users) as the data, `$code` as a key value array and `$error` as another key value array.

```
public function index()
{
    $users = User::latest()->take(5)->get();
    $code = ['code' => 30566];
    $error = ['error' => 'ERROR-2019-09-14'];

    return api()->response(200, 'Latest 5 Users', $users, $code, $error);
}
```

This is the result:

```
{
    "STATUS": 200,
    "MESSAGE": "Latest 5 Users",
    "DATA": [
        {"name": "Obay Adam", ...}
    ],
    "code": 30566,
    "error": "ERROR-2019-09-14"
}
```

Another way of creating a response is by calling `api()` method and passing the parameters directly to the helper function. Again, it is up to you how you better want to use it.

Check the below code example.

```
public function index()
{
    $users = User::latest()->take(5)->get();

    return api(200, 'Latest 5 Users', $users);
}
```

This is the result:

```
{
    "STATUS": 200,
    "MESSAGE": "Latest 5 Users",
    "DATA": [
        {"name": "Obay Adam", ...}
    ]
}
```

Helper functions
----------------

[](#helper-functions)

The package ships with a group of functions that will help you to speed up your development process. For example, you could call directly `api()->ok()` if the response was successful, instead of building the response.

#### `function ok()`

[](#function-ok)

The `ok()` function can be used without passing any parameters, it will defaulted the status code to `200` and use the default message from the configuration file.

```
return api()->ok();
```

Result:

```
{
    "STATUS": 200,
    "MESSAGE": "Process is successfully completed",
    "DATA": {}
}
```

Or you could pass to the function a custom message and the data you need. In this case, as mentioned before, the `ok()` status code will be defaulted to 200.

```
$users = User::latest()->take(5)->get();

return api()->ok("User list", $users);
```

Result:

```
{
    "STATUS": 200,
    "MESSAGE": "User list",
    "DATA": [
        {"name": "Obay Adam", ...}
    ]
}
```

#### `function notFound()`

[](#function-notfound)

The `notFound()` function, as its name states, should be used for the case when the resource is not found and the status code will be defaulted to `404`. You could pass a custom message to this function otherwise it will use the default message from the configuration file.

```
return api()->notFound();
```

#### `function validation()`

[](#function-validation)

The `validation()` function can be used on the case of a validation error exist, throwing a 422 status code by default. It accepts two mandatory parameters: a message and an array of errors, and as many extra parameters you need (we recommend a key value array format). If the message is empty, then the default message will be used instead.

```
return api()->validation('These fields are required.', ['name', 'lastName']);
```

#### `function error()`

[](#function-error)

The `error()` function can be used when an internal server error occurs throwing a 500 status code by default. It accepts two mandatory parameters: a message and an array of errors, and as many extra parameters you need (we recommend a key value array format). If the message is empty, then the default message will be used instead.

Configuration
-------------

[](#configuration)

### JSON Response Labels

[](#json-response-labels)

If you need to customize the default messages or the json response labels, you can do it directly on the `api.php` configuration file.

methoddefault status codechange codemessage`ok()`200`config('api.codes.success)``trans('api-response::messages.success)``notFound()`404`config('api.codes.notfound)``trans('api-response::messages.notfound)``validation()`422`config('api.codes.validation)``trans('api-response::messages.validation)``error()`500`config('api.codes.error)``trans('api-response::messages.error)`### Matching Status Codes

[](#matching-status-codes)

By default, all API responses return a 200 OK HTTP status header. If you'd like the status header to match the Response's status, set the `matchstatus` configuration to `true`

### Include The Count Of Data

[](#include-the-count-of-data)

You can optionally include the count of the `DATA` portion of the response, by setting `includeDataCount` to `true` in the `api.php` configuration file. You can also override the label, if desired, by updating the label in the`keys` array of the configuration file.

```
{
    "STATUS": "200",
    "MESSAGE": "User Profile data",
    "DATA": [
        ...
    ],
    "DATACOUNT": 6
}
```

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

[](#contributing)

We will be happy if we see PR from you.

License
-------

[](#license)

The API Response is a free package released under the MIT License.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.7% 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 ~0 days

Total

2

Last Release

895d ago

### Community

Maintainers

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

---

Top Contributors

[![obiefy](https://avatars.githubusercontent.com/u/16496932?v=4)](https://github.com/obiefy "obiefy (109 commits)")[![beauraines](https://avatars.githubusercontent.com/u/2186673?v=4)](https://github.com/beauraines "beauraines (16 commits)")[![SamyOteroGlez](https://avatars.githubusercontent.com/u/8644532?v=4)](https://github.com/SamyOteroGlez "SamyOteroGlez (7 commits)")[![boudydegeer](https://avatars.githubusercontent.com/u/753439?v=4)](https://github.com/boudydegeer "boudydegeer (4 commits)")[![khatabwedaa](https://avatars.githubusercontent.com/u/38974561?v=4)](https://github.com/khatabwedaa "khatabwedaa (4 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![masudrana03](https://avatars.githubusercontent.com/u/63516873?v=4)](https://github.com/masudrana03 "masudrana03 (2 commits)")[![sunxyw](https://avatars.githubusercontent.com/u/31698606?v=4)](https://github.com/sunxyw "sunxyw (1 commits)")[![Mdhesari](https://avatars.githubusercontent.com/u/28428724?v=4)](https://github.com/Mdhesari "Mdhesari (1 commits)")[![ramnzys](https://avatars.githubusercontent.com/u/19354043?v=4)](https://github.com/ramnzys "ramnzys (1 commits)")[![seifodev](https://avatars.githubusercontent.com/u/4215535?v=4)](https://github.com/seifodev "seifodev (1 commits)")[![ashish-patidar-rau](https://avatars.githubusercontent.com/u/41135198?v=4)](https://github.com/ashish-patidar-rau "ashish-patidar-rau (1 commits)")

---

Tags

responsephpjsonapilaravelRESTful APIrestful

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[obiefy/api-response

Simple Laravel package to return Json responses.

17324.6k](/packages/obiefy-api-response)[dragon-code/laravel-json-response

Automatically always return a response in JSON format

1118.6k1](/packages/dragon-code-laravel-json-response)[wayofdev/laravel-symfony-serializer

📦 Laravel wrapper around Symfony Serializer.

2113.6k](/packages/wayofdev-laravel-symfony-serializer)[djurovicigoor/larajsonresponse

Laravel API wrapper for returning JSON response.

116.8k](/packages/djurovicigoor-larajsonresponse)

PHPackages © 2026

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