New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

tobyz/json-api-server

A fully automated JSON:API server implementation in PHP.
62,196 69
Install
composer require tobyz/json-api-server
Latest Version:v0.2.1
PHP:>=7.3
License:MIT
Last Updated:Oct 20, 2026
Links: GitHub  ·  Packagist
Maintainer: tobyzerner

json-api-server

Pre Release License

json-api-server is a JSON:API server implementation in PHP.

It allows you to build a feature-rich API by defining resource schema and connecting it to your application's database layer.

Based on your schema definition, the package will serve a complete API that conforms to the JSON:API specification, including support for:

  • Showing individual resources (GET /articles/1)
  • Listing resource collections (GET /articles)
  • Sorting, filtering, pagination, and sparse fieldsets
  • Compound documents with inclusion of related resources
  • Creating resources (POST /articles)
  • Updating resources (PATCH /articles/1)
  • Deleting resources (DELETE /articles/1)
  • Content negotiation
  • Error handling
  • Localization for customizing error messages
  • Extensions including Atomic Operations
  • Generating OpenAPI definitions

Documentation

Read the documentation

Example

The following example uses an Eloquent model in a Laravel application. However, json-api-server can be used with any framework that can deal in PSR-7 Requests and Responses. Custom behavior can be implemented to support other ORMs and data persistence layers.

use App\Models\User;
use Tobyz\JsonApiServer\Laravel;
use Tobyz\JsonApiServer\Laravel\EloquentResource;
use Tobyz\JsonApiServer\Laravel\Filter;
use Tobyz\JsonApiServer\Endpoint;
use Tobyz\JsonApiServer\Schema\Field;
use Tobyz\JsonApiServer\Schema\Type;
use Tobyz\JsonApiServer\JsonApi;

class UsersResource extends EloquentResource
{
    public function type(): string
    {
        return 'users';
    }

    public function newModel(Context $context): object
    {
        return new User();
    }

    public function endpoints(): array
    {
        return [
            Endpoint\Show::make(),
            Endpoint\Index::make()->paginate(),
            Endpoint\Create::make()->visible(Laravel\can('create')),
            Endpoint\Update::make()->visible(Laravel\can('update')),
            Endpoint\Delete::make()->visible(Laravel\can('delete')),
        ];
    }

    public function fields(): array
    {
        return [
            Field\Attribute::make('name')
                ->type(Type\Str::make())
                ->writable()
                ->required(),

            Field\ToOne::make('address')->includable(),

            Field\ToMany::make('friends')
                ->type('users')
                ->includable(),
        ];
    }

    public function filters(): array
    {
        return [Filter\Where::make('id'), Filter\Where::make('name')];
    }
}

$api = new JsonApi();

$api->resource(new UsersResource());

/** @var Psr\Http\Message\ServerRequestInterface $request */
/** @var Psr\Http\Message\ResponseInterface $response */
try {
    $response = $api->handle($request);
} catch (Throwable $e) {
    $response = $api->error($e);
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

648,266,899 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982
laravel/tinker

Powerful REPL for the Laravel framework.

512,508,631 7,443
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

429,221,767 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

407,699,310 4,663

Version History

Version Released PHP License
v0.2.1 >=7.3 MIT
v0.2.0 >=7.1 MIT
Pre-releases (16)
Version Released PHP License
v1.0.0-rc.1 >=8.1 MIT
v1.0.0-beta.6 >=8.1 MIT
v1.0.0-beta.5 >=8.1 MIT
v1.0.0-beta.4 >=8.1 MIT
v1.0.0-beta.3 >=8.1 MIT