It turns a Laravel API into an end-to-end JSON:API 1.1 service: it accepts and validates requests in the standard and responds in the standard, for both success and error responses.
Status: version 0.1.0, under validation. The API described here is complete, covered by tests on Laravel 10 to 13 and tried in a real application. In 0.x the API may change between minor versions (with ^0.1 Composer installs only 0.1.x).
What is JSON:API
JSON:API is a specification, not a library: an agreement on how a server and a client exchange data as JSON over HTTP. It defines the shape of responses, of the requests that create or change data, of the relationships between resources, of the query parameters and of the errors. The full text is at jsonapi.org/format.
Because every API that follows it speaks the same way, a client or a library written for one works with another, and the team no longer has to argue about how to name fields, how to paginate or how to return an error.
Its main ideas:
- Resources: everything the API exposes is an object with a
typeand anid, and its data goes insideattributes. - Relationships: a resource points to others by their
typeandid, insiderelationships. Withincludethey can come in the same response, inincludedand without repeating. - Queries with fixed names:
includeto bring relationships,fields[type]to ask for only some fields andsortto order;pageandfilterare reserved for pagination and filtering. - Uniform errors: always an
errorsarray with the status, a code and asourcethat points to the field or parameter that failed. - Its own content type:
application/vnd.api+json, in bothAcceptandContent-Type.
This is what a response with an article and its author looks like:
{
"jsonapi": { "version": "1.1" },
"data": {
"type": "articles",
"id": "1",
"attributes": { "title": "Hello, JSON:API" },
"relationships": {
"author": { "data": { "type": "people", "id": "9" } }
},
"links": { "self": "/articles/1" }
},
"included": [
{ "type": "people", "id": "9", "attributes": { "name": "Ada" } }
]
}
What this package does
- Responds in JSON:API: resources, paginated collections, relationships,
included,linksandmeta, and every error (validation,401,404,405and500included) as an error document withsource.orpointer source..parameter - Validates what arrives: the headers (
AcceptandContent-Type), the query parameters (include,fields,sort,filter,page) and the body document, with the exact pointer of the failing member. - Applies the query to Eloquent with whitelists you write: filters, sorting, pagination and eager loading of relationships.
- Registers no routes or controllers: you decide the URLs and the logic; the package takes care of the format.
- Switches itself on on install, without touching
bootstrap/app.php,KernelorHandler.
Requirements
- PHP 8.1 or higher
- Laravel 10, 11, 12 or 13