Getting started

Quick start

Declare a resource once, and see it rendered in three revisions.

This page builds an Order API with three revisions: genesis, winter-25 and spring-26, the head. The resource declares the spring-26 shape, and the two older shapes come from the history of the fields that changed.

Declare the revisions

app/Api/ApiRevision.php
enum ApiRevision: string implements Revision
{
    use IsRevision;

    #[Meta('First published revision', publishedAt: '2025-06-01')]
    case Genesis = 'genesis';

    #[Meta(
        '`paid` becomes `state`, totals become decimal strings',
        publishedAt: '2025-12-01',
        deprecatedAt: '2026-11-01',
    )]
    case Winter25 = 'winter-25';

    #[Meta('`state` renamed `status`, totals in cents, `name` split, tags')]
    case Spring26 = 'spring-26';

    public static function order(): array
    {
        return [self::Genesis, self::Winter25, self::Spring26];
    }
}

Declare the head of the resource

The resource implements Revisioned, uses RevisionedResource, and returns descriptors from toArray().

app/Http/Resources/OrderResource.php
use Ark4ne\ApiRevisions\Laravel\Resources\RevisionedResource;
use Ark4ne\ApiRevisions\Schema\Revisioned;
use Ark4ne\ApiRevisions\Schema\Schema;

final class OrderResource extends JsonResource implements Revisioned
{
    use RevisionedResource;

    public function toArray(Request $request): array
    {
        return [
            'title' => Schema::string(),
            'status' => Schema::enum(OrderStatus::class)
                ->was(ApiRevision::Winter25, name: 'state')
                ->was(
                    ApiRevision::Genesis,
                    name: 'paid',
                    schema: Schema::bool(),
                    down: fn(OrderStatus $status): bool
                        => $status === OrderStatus::Paid,
                ),
            'total' => Schema::int('total_cents')
                ->was(
                    ApiRevision::Winter25,
                    schema: Schema::string(),
                    down: fn(int $cents): string
                        => number_format($cents / 100, 2, '.', ''),
                )
                ->was(
                    ApiRevision::Genesis,
                    schema: Schema::float(),
                    down: fn(string $amount): float => (float) $amount,
                ),
            'lines' => Schema::list(Schema::object([
                'sku' => Schema::string(),
                'quantity' => Schema::int()
                    ->was(ApiRevision::Genesis, name: 'qty'),
            ])),
            'customer' => Schema::resource(CustomerResource::class)
                ->whenLoaded('customer'),

            ...Schema::since(ApiRevision::Spring26, [
                'first_name' => Schema::string(),
                'last_name' => Schema::string(),
                'tags' => Schema::list(Schema::string()),
            ]),
            ...Schema::until(ApiRevision::Winter25, [
                'name' => Schema::string(
                    fn($value, Order $order): string
                        => "{$order->first_name} {$order->last_name}",
                ),
            ]),
        ];
    }
}

Read each history from the head downwards: status was named state up to winter-25 included, and was a boolean paid up to genesis included. first_name, last_name and tags exist from spring-26; name exists up to winter-25.

Render it in each revision

The controller returns the resource as usual. The client picks its revision with the Api-Revision header:

{
  "data": {
    "title": "Lamp",
    "status": "paid",
    "total": 1250,
    "lines": [{ "sku": "L-1", "quantity": 2 }],
    "customer": { "name": "Ada Lovelace", "email": "ada@example.com" },
    "first_name": "Ada",
    "last_name": "Lovelace",
    "tags": ["gift"]
  }
}

Each response carries an Api-Revision header naming its revision. The winter-25 response also carries a Deprecation header, from the deprecatedAt of its #[Meta]. A request without the header gets the head.

Accept a request in each revision

A form request declares the head of its body the same way, in body(). Older clients send the old shape: it is validated with their names, then upgraded.

app/Http/Requests/StoreOrderRequest.php
final class StoreOrderRequest extends FormRequest implements Revisioned
{
    use RevisionedRequest;

    protected function body(): array
    {
        return [
            'title' => Schema::string()->required()->max(255),
            'status' => Schema::enum(OrderStatus::class)
                ->was(ApiRevision::Winter25, name: 'state')
                ->was(
                    ApiRevision::Genesis,
                    name: 'paid',
                    schema: Schema::bool(),
                    up: fn(bool $paid): OrderStatus => $paid
                        ? OrderStatus::Paid
                        : OrderStatus::Pending,
                ),
            'total' => Schema::int()->required()->min(0)
                ->was(
                    ApiRevision::Winter25,
                    schema: Schema::string(),
                    up: fn(string $amount): int
                        => (int) round(((float) $amount) * 100),
                )
                ->was(ApiRevision::Genesis, schema: Schema::float()),
            'tags' => Schema::list(Schema::string())
                ->since(ApiRevision::Spring26, default: []),

            ...Schema::since(ApiRevision::Spring26, [
                'first_name' => Schema::string()->required(),
                'last_name' => Schema::string()->required(),
            ], default: null),
            ...Schema::until(ApiRevision::Winter25, [
                'name' => Schema::string()->required(),
            ]),
        ];
    }

    protected function upgrade(Revision $to, array $input): array
    {
        if (
            $to === ApiRevision::Spring26
            && is_string($input['name'] ?? null)
        ) {
            [$input['first_name'], $input['last_name']]
                = explode(' ', $input['name'], 2) + [1 => ''];
        }

        return $input;
    }
}
POST
/orders
Api-Revision: genesis
{ "title": "Lamp", "paid": true, "total": 12.5, "name": "Ada Lovelace" }
Response$request->validated()
{
  "title": "Lamp",
  "status": "paid",
  "total": 1250,
  "tags": [],
  "first_name": "Ada",
  "last_name": "Lovelace"
}

The controller reads validated() and only ever handles the head.

Check the declarations

Terminal
php artisan revision:check
   INFO  The revisions of 3 classes are consistent.

From here: