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

ActiveLibrary[API Development](/categories/api)

mohamed-ahmed/api-response
==========================

Reusable trait for standardized API responses

2.0.0(10mo ago)030MITPHPPHP ^8.2

Since Jul 24Pushed 10mo agoCompare

[ Source](https://github.com/mohamedgcoder/api-response)[ Packagist](https://packagist.org/packages/mohamed-ahmed/api-response)[ RSS](/packages/mohamed-ahmed-api-response/feed)WikiDiscussions main Synced today

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

Welcome to Standardized API responses Package
=============================================

[](#welcome-to-standardized-api-responses-package)

A reusable Laravel package for building **consistent JSON API responses**.
It helps you standardize your API outputs with data, pagination, exceptions, and error structures — cleanly and efficiently.

Package info
------------

[](#package-info)

**URL**:

---

📦 Installation
--------------

[](#-installation)

```
composer require mohamedahmed/api-response
```

🚀 Usage
-------

[](#-usage)

Changelog
---------

[](#changelog)

### v2.0.0

[](#v200)

- Added `DataTrait` for handling response data.
- Added `PaginationTrait` with support for Laravel pagination.
- Enabled method chaining: `->paginate($paginator)`
- Enabled method chaining: `->messages([...])->add($message)->merge([messages])`
- Enabled method chaining: `->errors([...])->add($serror)->merge([errors])`
- Enabled method chaining: `->alerts([...])->add($alert)->merge([alerts])`

**In your Laravel controller:**

```
use MohamedAhmed\ApiResponse\Traits\ResponseApi;

class SomeController extends Controller
{
    use ResponseApi;

    public function index()
    {
        $this->setData(SomeModel::all());
        return $this->apiResponse();
    }
}

### v2.0.0

$res = Response::make();
```

🧠 Available Methods
-------------------

[](#-available-methods)

**setData($data)**

Sets the main response data.

If data is paginated, pagination details are auto-included.

```
$this->setData(User::all());
$this->setData(User::paginate(10));

### v2.0.0

$res->data($items->items());
$res->paginate($items);
```

**setException(Throwable $exception)**

Stores an exception and adds error info in the response.

```
try {
    // logic
} catch (\Exception $e) {
    $this->setException($e);
    $this->setCode(500);
    return $this->apiResponse();
}

### v2.0.0

$res->exception($e);
return $this->tojson();
```

**setEc(int $code)**

Sets an internal error code, useful for debugging and frontend error mapping.

```
$this->setEc(1001); // Internal reference code
```

**setMessages(array|string $messages)**

Custom messages for the user.

```
$this->setMessages(['Created successfully']);

### v2.0.0

$res->nessages(['Created successfully']); // for alerts and errors also
$res->add('additional message);
$res->merge('[more of messages]);
```

**setCode(int $httpCode)**

Sets HTTP status code (defaults to 200).

```
$this->setCode(201); // HTTP Created

### v2.0.0
$res->code(201);
```

**apiResponse()**

Builds and returns the full JSON response.

```
return $this->apiResponse();

### v2.0.0
return $res->tojson();
```

✅ Example Response
------------------

[](#-example-response)

```
{
  "code": 200,
  "responseStatus": true,
  "messages": [],
  "response": {
    "dataLength": 3,
    "pagination": {
      "path": "/api/products?page=1",
      "total": 50,
      "perPage": 10,
      "currentPage": 1,
      "lastPage": 5
    },
    "data": [{}, {}, {}]
  },
  "error": {
    "errorCode": null,
    "line": null,
    "errorMessage": null,
    "file": null
  }
}
```

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance54

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

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 ~35 days

Total

2

Last Release

308d ago

Major Versions

1.0.0 → 2.0.02025-08-29

### Community

Maintainers

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

---

Top Contributors

[![mohamedgcoder](https://avatars.githubusercontent.com/u/39387545?v=4)](https://github.com/mohamedgcoder "mohamedgcoder (13 commits)")

### Embed Badge

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

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

###  Alternatives

[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.3k3](/packages/defstudio-telegraph)[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)[rapidez/core

Rapidez Core

1823.5k72](/packages/rapidez-core)

PHPackages © 2026

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