API revisions

Overview & declaring revisions

Pin each client on a dated revision of your API, Stripe-style, and keep only the latest shape in the code.

An API revision is a dated version of the public shape of your API. Each change that would break a client ships in a new revision, and each client stays on the revision it was built against (Api-Version: 2026-12-01) until it moves on.

The code keeps only the latest shape of every resource, the head. Older shapes are declared on the fields that changed; the package renders them, validates include and fields in their names, eager-loads their relationships and describes them (Definitions::for($class, $revision), jsonapi:check).

Revisions come from ark4ne/laravel-api-revisions, installed with this package. An application that configures no revision has one shape, the head.

Declare the revisions

The revisions are the cases of a backed enum of your application implementing Revision. order() lists them from the oldest to the newest; the last one is the head. Keep in it the revisions your clients can be pinned on.

app/Http/ApiRevision.php
use Ark4ne\ApiRevisions\Revisions\IsRevision;
use Ark4ne\ApiRevisions\Revisions\Meta;
use Ark4ne\ApiRevisions\Revisions\Revision;

enum ApiRevision: string implements Revision
{
    use IsRevision;

    #[Meta('First published revision')]
    case Initial = '2026-09-01';
    #[Meta('Names become labels')]
    case Labels = '2026-12-01';
    #[Meta('Users replace teachers')]
    case UsersReplaceTeachers = '2027-01-15';

    public static function order(): array
    {
        return [self::Initial, self::Labels, self::UsersReplaceTeachers];
    }
}

Resolve the revision of each request

Name the enum and the header your clients send in config/revisions.php, and put the revision middleware on your routes:

Terminal
php artisan vendor:publish --tag=revisions-config
config/revisions.php
'enum' => App\Http\ApiRevision::class,
'resolvers' => [
    Ark4ne\ApiRevisions\Laravel\Http\Resolvers\HeaderResolver::class => [
        'name' => 'Api-Version',
    ],
],
routes/api.php
Route::middleware('revision')->group(function () {
    // …
});

A request without the header is served the head ('default' => 'latest'); a value the enum does not have is answered with 400. Every rendering of the request, its JsonApiFormRequest and the guards then use that revision. A form request is resolved after every middleware, so the revision is always resolved first.

What changes, and how to declare it

The changeDeclared withPage
A field is renamed or changes type->was()Changing a field
A field is added or removed->since(), ->until()Adding, removing, new types
A resource changes type#[JsonApiSince], #[JsonApiUntil]Adding, removing, new types
A value changes meaning, not shapeApiRevision::current()Guards
A request body changesbody() of a RevisionedRequestWrite requests across revisions