mpstr24/laravel-api-responses

Laravel package for formatted API responses.

v1.0.0 2025-03-10 22:59 UTC

This package is auto-updated.

Last update: 2025-07-12 20:52:49 UTC


README

This package provides API response functions via a trait to help improve consistency of API response codes across your projects.

GitHub Actions Workflow Status Packagist Version Packagist Downloads

Features

  • Pre-made functions of the most common API responses.

Installation

Install the package via Composer:

composer require mpstr24/laravel-api-responses

Usage/Examples

First add the trait to your controller.

use ApiResponseTrait;

All methods, except from "No Content" allow for a message and data to be returned. Custom messages also allow you to choose the status code.

Examples of each below.

Success - 200

public function success()
{
  return $this->apiSuccess();
}

Created - 201

public function created()
{
    return $this->apiCreated();
}

Accepted - 202

public function accepted()
{
    return $this->apiAccepted();
}

No Content - 204

public function noContent()
{
    return $this->apiNoContent();
}

Bad Request - 400

public function badRequest()
{
    return $this->apiBadRequest();
}

Unauthorized - 401

public function unauthorized()
{
    return $this->apiUnauthorized();
}

Forbidden - 403

public function forbidden()
{
    return $this->apiForbidden();
}

Not Found - 404

public function notFound()
{
    return $this->apiNotFound();
}

Teapot - 418

public function teapot()
{
    return $this->apiTeapot();
}

Unprocessable - 422

public function unprocessable()
{
    return $this->apiUnprocessable();
}

Too Many Requests - 429

public function tooManyRequests()
{
    return $this->apiTooManyRequests();
}

Server Error - 500

public function serverError()
{
    return $this->apiInternalServerError();
}

Custom response

public function apiCustomResponse()
{
    return $this->apiCustom('Custom', null, 201);
}

Roadmap

  • Support for resources
  • Expand into more helpful API features
  • Larastan goals
    • 5
    • 6
    • 7
    • 8
    • 9

License

MIT