Simple and human friendly library for api servers (PHP serving out JSON)

v2.0.0-beta 2019-02-17 18:15 UTC


Simple and friendly library for api servers (PHP serving out JSON).

It generates json output according to the standard, but aims to be easy to understand for people without knowledge of the jsonapi standard.

Getting started

A small example:

use alsvanzelf\jsonapi;

$user = new stdClass();
$user->id = 42;
$user->name = 'Zaphod Beeblebrox';
$user->heads = 2;

$jsonapi = new jsonapi\resource($type='user', $user->id);

Which will result in:

    "links": {
        "self": "/examples/resource.php"
    "data": {
        "type": "user",
        "id": 42,
        "attributes": {
            "name": "Zaphod Beeblebrox",
            "heads": 2
        "links": {
            "self": "/examples/resource.php"

For a collection response, data is an array of resources. Errors can also be send as response, even automatically by exceptions.

Examples for all kind of responses are in the /examples directory.


Use Composer. And use require to get the latest stable version:

composer require alsvanzelf/jsonapi

To Do

Right now, this library handles all the basics:

  • generating single resources
  • generating resource collections
  • adding to-one and to-many relationships
  • handling error responses
  • sending out the json response with correct http headers

Plus some handy tools:

  • easy turning thrown exceptions into jsonapi responses
  • constants for easy setting http status codes
  • sending out redirect locations

Plans for the near and later future include:

  • import a database array as a collection response (#2)
  • handle creating, updating and deleting resources (#5)


Pull Requests or issues are welcome!