Introduction

API Toolkit is a Laravel package that gives you everything you need to build JSON:API compliant REST APIs. It handles resource serialization, query parameters, pagination, filtering, sorting, error responses, and even generates your OpenAPI documentation automatically.

Features

  • JSON:API Resources — Serialize Eloquent models into spec-compliant responses with relationships, links, and metadata
  • Query Builder — Fluent API for filtering, sorting, including relationships, and paginating
  • Built-in Filters — Exact, partial, date, and scope-based filters out of the box, with support for custom filters
  • Pagination — Offset and cursor-based pagination with configurable page sizes
  • Error Handling — Automatic exception rendering in JSON:API format, including validation errors with field pointers
  • OpenAPI Generation — Auto-generate OpenAPI 3.1 specs from your routes and resources
  • Testing Utilities — Chainable assertions purpose-built for JSON:API responses
  • Middleware — Force JSON:API content type on all responses

Requirements

  • PHP 8.4+
  • Laravel 13.0+

Quick Example

Define a resource:

use BlueBeetle\ApiToolkit\Resources\Resource;

class ProductResource extends Resource
{
    public function attributes($model): array
    {
        return [
            'name'  => $model->name,
            'price' => $model->price,
            'sku'   => $model->sku,
        ];
    }

    public function relationships(): array
    {
        return [
            'category' => CategoryResource::class,
        ];
    }
}

Use it in a controller:

use BlueBeetle\ApiToolkit\Http\Response;
use BlueBeetle\ApiToolkit\QueryBuilder;

class ProductController
{
    public function index(Request $request)
    {
        return QueryBuilder::for(Product::class, $request)
            ->fromResource(ProductResource::class)
            ->paginate();
    }

    public function show(Product $product, Response $response)
    {
        return $response->success($product, ProductResource::class);
    }
}

Response:

{
  "data": {
    "type": "products",
    "id": "prod_abc123",
    "attributes": {
      "name": "Widget Pro",
      "price": 29.99,
      "sku": "WGT-001"
    },
    "relationships": {
      "category": {
        "data": { "type": "categories", "id": "1" }
      }
    }
  }
}