Skip to content

Versioning ​

Fusion Stack uses date-based versioning, the model Stripe made popular. Your code only ever implements the latest version of the API. Each client is pinned to the version it was built against, and small version change classes translate requests up to the latest version and responses back down to the client's. You can change your API without breaking anyone, and you never keep old controllers around.

Versioning is optional. It is off in new projects: every client gets the current API and nothing below applies. Turn it on when you are about to make your first breaking change after clients depend on the API.

Versions ​

A version is a date, YYYY-MM-DD, optionally followed by a release name: 2027-01-15.atlas.

  • Additive changes need no version: new endpoints, new attributes, new filters, new values in a list documented as open. Ship them whenever you like.
  • Breaking changes each get a version change class, registered against the version that introduces them. Batch them into named releases: the first version of a release, such as 2027-01-15.atlas, carries its breaking changes, and later versions with the same name are additive only. Upgrading within a release name is then always safe. Pick names in alphabetical order, so the order of releases is obvious.

Which version a request gets ​

  1. The Api-Version header, if the request sends one.
  2. Otherwise the version its API key is pinned to. New keys are pinned to the latest version when they are created, and php artisan api:key:pin (or laravel api:key:pin in Rust) moves them.
  3. Otherwise default in config/api.php (Versions::default_version in Rust).

Every response says which version it used in its own Api-Version header. An unknown version is a 400:

json
{
  "errors": [
    {
      "status": "400",
      "code": "invalid_api_version",
      "title": "Invalid API version",
      "detail": "[2099-01-01] is not a version of this API. The latest version is 2026-11-30.atlas.",
      "meta": {
        "latest_version": "2026-11-30.atlas",
        "request_id": "req_01m8r9qw891hstwzh5ykz4ptrb"
      }
    }
  ]
}

Turning it on ​

php
// config/api.php
'versions' => [
    'enabled' => true,
    'latest' => '2027-01-15.atlas',
    // Clients without an Api-Version header or a pinned key keep today's behaviour.
    'default' => '2026-12-01',
    'changes' => [
        '2026-12-01' => [],
        '2027-01-15.atlas' => [\App\Api\Versions\RenameCustomerNameToFullName::class],
    ],
],
rust
use std::sync::Arc;

use fusion_stack::Config;
use fusion_stack::versioning::Versions;

// Passing Versions turns versioning on.
Config::new().versions(
    Versions::new("2027-01-15.atlas")
        // Clients without an Api-Version header or a pinned key keep today's behaviour.
        .default_version("2026-12-01")
        .version("2026-12-01", vec![])
        .version("2027-01-15.atlas", vec![Arc::new(RenameCustomerNameToFullName)]),
)

A version with only additive changes is registered with an empty list, like 2026-12-01 above. php artisan api:version:list (or laravel api:version:list in Rust) shows every version, its status and its changes.

Version changes ​

Say customers had a name attribute and you want to call it full_name. Change the model, resource and form request to full_name, as if no old clients existed, then describe the difference. In PHP, api:version:make creates the class and registers it; Rust has no such command, because versions are declared in code: write a type implementing VersionChange and add it to Versions::version, as above.

sh
php artisan api:version:make RenameCustomerNameToFullName --date=2027-01-15.atlas
php
final class RenameCustomerNameToFullName extends VersionChange
{
    public string $description = 'customer.name renamed to customer.full_name';

    public array $resources = ['customers'];

    // Requests from older clients, on their way to your controller.
    public function transformRequest(array $payload, RequestContext $context): array
    {
        return $this->renameAttribute($payload, 'name', 'full_name');
    }

    // Responses from your controller, on their way back to older clients.
    public function transformResponse(array $document, ResponseContext $context): array
    {
        return $this->renameAttribute($document, 'full_name', 'name');
    }
}
rust
use fusion_stack::versioning::{VersionChange, document};
use serde_json::{Map, Value};

pub struct RenameCustomerNameToFullName;

impl VersionChange for RenameCustomerNameToFullName {
    fn description(&self) -> &str {
        "customer.name renamed to customer.full_name"
    }

    fn resources(&self) -> &[&'static str] {
        &["customers"]
    }

    // Requests from older clients, on their way to your handler.
    fn transform_request(&self, payload: &mut Map<String, Value>) {
        let mut body = Value::Object(std::mem::take(payload));
        document::rename_attribute(&mut body, self.resources(), "name", "full_name");
        if let Value::Object(map) = body {
            *payload = map;
        }
    }

    // Responses from your handler, on their way back to older clients.
    fn transform_response(&self, document: &mut Value) {
        document::rename_attribute(document, self.resources(), "full_name", "name");
    }
}

The same customer, as two clients see it:

json
{
  "data": {
    "id": "1",
    "type": "customers",
    "attributes": {
      "name": "Katelynn Connelly",
      "email": "malinda.marks@example.com"
    }
  }
}
json
{
  "data": {
    "id": "1",
    "type": "customers",
    "attributes": {
      "full_name": "Katelynn Connelly",
      "email": "malinda.marks@example.com"
    }
  }
}

How the changes run:

  • A request is migrated up through every change newer than its version, oldest first. Its response is migrated down through the same changes, newest first.
  • $resources (resources() in Rust) lists the resource types, or route names such as orders.*, a change applies to. Laravel for Rust routes have no names, so Rust uses the method and URI pattern instead, such as GET /v1/orders/*. Other requests skip it, so they cost nothing. A client on the latest version skips every change.
  • Changes apply to data and every resource in included, and to the field names in query parameters (fields, filter, sort and include). Validation errors point at the field names the client sent: an old client that leaves out name gets "pointer": "/data/attributes/name".
  • Transforms work on arrays (serde_json values in Rust) only. They must not touch the database or the service container, so they are fast and easy to test.

The base class has helpers for the common changes: renameAttribute, removeAttribute, addAttribute, moveAttribute, renameType, renameRelationship, mapEnumValues, nestAttributes and flattenAttributes. Rust's versioning::document module has rename_attribute, remove_attribute and add_attribute, and the others are short map_attributes callbacks. A chain of 20 changes on a 100-item collection adds about 2 ms; CI checks it stays under 5 ms.

Test a change on its own with VersionChangeTestCase (fusion_stack::testing::assert_round_trip in Rust), or check each version's response with assertVersionShape() (api_version(v) then assert_shape in Rust).

Endpoints that change ​

A version change can also change which endpoints exist:

PHP propertyRust methodOlder versions get
$addedRoutesadded_routes()A 404, as the route did not exist yet.
$removedRoutesremoved_routes()The route, which this version and newer get a 404 for.
$unavailableToOlderVersionsunavailable_to_older_versions()A 410 endpoint_unavailable_for_version, for responses that cannot be translated to the old shape. The rest of the API keeps working for them.
$legacyControllerslegacy_handlers()A frozen copy of the old action. A last resort: you have to keep that code working.

Releasing a version ​

Rust has no api:version:release: change the version passed to Versions::new in code to make it the latest.

sh
php artisan api:version:release 2027-01-15.atlas   # makes it the latest
php artisan api:changelog                          # Markdown changelog from the change descriptions
sh
# Versions::new("2027-01-15.atlas") in code makes it the latest.
laravel api:changelog                              # Markdown changelog from the change descriptions

The API reference at /docs (mounted with fusion_stack::docs::routes in Rust) has a switcher between released versions, and php artisan api:openapi --api-version=2026-06-01 (or laravel api:openapi --api-version 2026-06-01 in Rust) writes the OpenAPI document as that version sees it.

Retiring old versions ​

By default the two newest released versions are supported (keep, Versions::keep in Rust) and older ones are deprecated: their responses carry Deprecation, Sunset and a Link to your changelog. With track_usage (Config::track_usage in Rust), php artisan api:version:usage (or laravel api:version:usage) shows which clients still use each version, so you can contact them.

php
'deprecated' => [
    '2026-06-01' => [
        'sunset' => '2027-06-01',
        // Short windows that reject the version with a 410, so clients notice before the sunset.
        'brownouts' => [['start' => '2027-05-04 14:00', 'end' => '2027-05-04 15:00']],
    ],
],
rust
use fusion_stack::versioning::{Deprecated, Versions};

Versions::new("2027-01-15.atlas")
    // ...
    .deprecated(
        "2026-06-01",
        Deprecated::new()
            .sunset("2027-06-01")
            // Short windows that reject the version with a 410, so clients notice before the sunset.
            .brownout("2027-05-04 14:00", "2027-05-04 15:00"),
    )

After its sunset a version is retired, and requests for it get a 410 api_version_retired linking to upgrade_guide_url (Versions::upgrade_guide_url in Rust).

Routes live under /v1 from the start. If the dated chain ever becomes too much to carry, a /v2 can start a new one, with /v1 frozen until its sunset.

Released under the MIT License.