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

ActiveLibrary[API Development](/categories/api)

lordsling/api-response
======================

222PHP

Since Nov 7Pushed 4y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#laravel-api-response)

> A collection of helpers for returning a response from your API more expressively

[![api-response](https://camo.githubusercontent.com/ba367a3f560748dc1804d271e8636f2a2d27798f0217369e5dde721d891221fb/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c253230417069253230526573706f6e73652e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6c6f7264736c696e672532466170692d726573706f6e7365267061747465726e3d746f706f677261706879267374796c653d7374796c655f32266465736372697074696f6e3d456173792b746f2b636f6e73756d652b524553542b4150492b4a534f4e2b726573706f6e736573266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667267769647468733d333030)](https://github.com/lordsling/api-response)

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

[](#installation)

Install this package via Composer:

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

No extra setup is required. The helper file is autoloaded via the "autoload" attributes of the `composer.json` file

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

[](#configuration)

Publish config file using Artisan command:

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

> Check `config/api-response.php` and read comments there if you need.

#### Sample Response API

[](#sample-response-api)

```
{
    "success": true,
    "payload": null,
    "errors": []
}
```

Usage
-----

[](#usage)

### 2xx Success `"success": true`

[](#2xx-success--success-true)

#### HTTP 200 OK

[](#http-200-ok)

```
/**
 * @param null $payload
 */
return ok($payload);
```

#### HTTP 200 OK

[](#http-200-ok-1)

```
/**
 * @param null $payload
 */
return error($payload);
```

#### HTTP 200 OK

[](#http-200-ok-2)

```
/**
 * @param null $payload
 */
return fail($payload);
```

#### HTTP 201 Created

[](#http-201-created)

```
/**
 * @param null $payload
 */
return created($payload);
```

#### HTTP 202 Accepted

[](#http-202-accepted)

```
/**
 * @param null $payload
 */
return accepted($payload);
```

#### HTTP 204 No Content

[](#http-204-no-content)

```
/**
 */
return noContent();
```

---

### 3xx Redirection

[](#3xx-redirection)

#### HTTP 301 Moved Permanently

[](#http-301-moved-permanently)

```
/**
 * @param $newUrl
 */
return movedPermanently($newUrl);
```

#### HTTP 302 Found

[](#http-302-found)

```
/**
 * @param $url
 */
return found($url);
```

#### HTTP 303 See Other

[](#http-303-see-other)

```
/**
 * @param $newUrl
 */
return seeOther($newUrl);
```

#### HTTP 307 Temporary Redirect

[](#http-307-temporary-redirect)

```
/**
 * @param $tempUrl
 */
return temporaryRedirect($tempUrl);
```

---

### 4xx Client Errors `"success": false`

[](#4xx-client-errors-success-false)

#### HTTP 400 Bad Request

[](#http-400-bad-request)

```
/**
 * @param null $payload
 */
return badRequest($payload);
```

#### HTTP 401 Unauthorized

[](#http-401-unauthorized)

```
/**
 * @param null $payload
 */
return unauthorized($payload);
```

#### HTTP 402 Payment Required

[](#http-402-payment-required)

```
/**
 * @param null $payload
 */
return paymentRequired($payload);
```

#### HTTP 403 Forbidden

[](#http-403-forbidden)

```
/**
 * @param null $payload
 */
return forbidden($payload);
```

#### HTTP 404 Not Found

[](#http-404-not-found)

```
/**
 * @param null $payload
 */
return notFound($payload);
```

#### HTTP 405 Method Not Allowed

[](#http-405-method-not-allowed)

```
/**
 * @param null $payload
 */
return methodNotAllowed($payload);
```

#### HTTP 406 Not Acceptable

[](#http-406-not-acceptable)

```
/**
 * @param null $payload
 */
return notAcceptable($payload);
```

#### HTTP 410 Gone

[](#http-410-gone)

```
/**
 * @param null $payload
 */
return gone($payload);
```

#### HTTP 413 Payload Too Large

[](#http-413-payload-too-large)

```
/**
 * @param null $payload
 */
return payloadTooLarge($payload);
```

#### HTTP 422 Unprocessable Entity

[](#http-422-unprocessable-entity)

```
/**
 * @param null $payload
 */
return unprocessableEntity($payload);
```

#### HTTP 426 Upgrade Required

[](#http-426-upgrade-required)

```
/**
 * @param null $payload
 */
return upgradeRequired($payload);
```

#### HTTP 429 Too Many Requests

[](#http-429-too-many-requests)

```
/**
 * @param null $payload
 */
return tooManyRequests($payload);
```

---

### 5xx Server Errors `"success": false`

[](#5xx-server-errors-success-false)

#### HTTP 500 Internal Server Errors

[](#http-500-internal-server-errors)

```
/**
 * @param null $payload
 */
return internalServerError($payload);
```

#### HTTP 501 Not Implemented

[](#http-501-not-implemented)

```
/**
 * @param null $payload
 */
return notImplemented($payload);
```

#### HTTP 502 Bad Gateway

[](#http-502-bad-gateway)

```
/**
 * @param null $payload
 */
return badGateway($payload);
```

#### HTTP 503 Service Unavailable

[](#http-503-service-unavailable)

```
/**
 * @param null $payload
 */
return serviceUnavailable($payload);
```

#### HTTP 504 Gateway Timeout

[](#http-504-gateway-timeout)

```
/**
 * @param null $payload
 */
return gatewayTimeout($payload);
```

#### HTTP 507 Insufficient Storage

[](#http-507-insufficient-storage)

```
/**
 * @param null $payload
 */
return insufficientStorage($payload);
```

Available methods
-----------------

[](#available-methods)

#### withHeaders

[](#withheaders)

```
/**
 * @param array $headers
 */
->withHeaders([
    ['HeaderName' => 'value'],
    //...
]);
```

#### withErrors

[](#witherrors)

```
/**
 * @param array $headers
 */
->withErrors([
    [
        'code' => 'code',
        'message' => 'message'
    ],
    //...
]);
```

#### withMeta

[](#withmeta)

```
/**
 * @param $request
 * @param null $callback
 */
->withMeta($request, function ($request) {
    return ['query' => $request->query()];
});
```

#### withValidation

[](#withvalidation)

`App/Exceptions/Handler.php`

```
public function render($request, Throwable $e)
{
    if ($e instanceof ValidationException) {
        return unprocessableEntity()->withValidation($e);
    }
}
```

#### withTrace

[](#withtrace)

`App/Exceptions/Handler.php`

```
public function render($request, Throwable $e)
{
    return internalServerError()
    /**
     * @param $exception
     */
    ->withTrace($e);
}
```

#### withMergeErrors

[](#withmergeerrors)

```
/**
 * @param ...$errorCodes
 */
->withMergeErrors(1000, 1001, 1002, /*...*/);
                    or
->withMergeErrors([1000, 1001, 1002, /*...*/]);
```

#### addError

[](#adderror)

```
/**
 * @param $code
 * @param string|null $message
 * @param array|null $details
 */
->addError(1000, 'Custom error message', ['details' => '...', //...]);
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

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

License
-------

[](#license)

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

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1a369203d8f832d8094a1ff4613db8a5305d8e76fac84cff3aca2716847c2290?d=identicon)[lordsling](/maintainers/lordsling)

---

Top Contributors

[![lordsling](https://avatars.githubusercontent.com/u/4235853?v=4)](https://github.com/lordsling "lordsling (3 commits)")

### Embed Badge

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

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

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

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