Skip to content

8. Your first breaking change, with versioning ​

The shop wants to take payment in yen, which has no cents, so the name total_cents is wrong. Renaming it to total is a breaking change: every client that reads total_cents, such as the web app or a partner's reporting tool, would break the moment you deploy.

Fusion Stack's answer is versioning. Each client is pinned to a version of the API, a date such as 2026-12-01. Your code only ever implements the newest version. For older ones, you write a small version change that translates between the two shapes, and Fusion Stack applies it to their requests and responses. Old clients keep working, unchanged, until they choose to upgrade. The versioning page explains the design.

Run the commands in the api folder:

sh
cd api

Turn versioning on ​

Versioning is off in a new project, so you can build without thinking about it. Turn it on before the first breaking change, in config/api.php, with today's API as the first version:

diff
         // example below. See app/Api/Versions/RenameTierToPlan.php.
-        'enabled' => false,
+        'enabled' => true,

-        'latest' => null,
+        'latest' => '2026-12-01',

Any date works, as long as later versions have later dates. Every response now says which version it speaks:

sh
curl -s -D - -o /dev/null localhost:8000/v1/orders/2 -H "Authorization: Bearer $KEY" | grep "^Api-Version"
text
Api-Version: 2026-12-01

The web app pins itself to the version its client was generated for, and sends it as Api-Version. Its client was generated before versioning was on, so the API now refuses it with a 400 invalid_api_version. Generate it again, so it pins 2026-12-01:

sh
cd ..
npm run api
text
Wrote packages/client/index.ts

The web app reads the version when it starts, so restart it: in the first terminal, press Ctrl+C, then run it again.

sh
npm run dev

Back in the second terminal, return to the API:

sh
cd api

Make the change ​

Create the version change, for a new version, 2027-01-15:

sh
php artisan api:version:make RenameTotalCentsToTotal --date=2027-01-15
text
INFO Created app/Api/Versions/RenameTotalCentsToTotal.php.
INFO Registered for 2027-01-15, an unreleased version. Release it with: php artisan api:version:release 2027-01-15.

Fill it in:

php
<?php

namespace App\Api\Versions;

use FusionStack\Laravel\Versioning\RequestContext;
use FusionStack\Laravel\Versioning\ResponseContext;
use FusionStack\Laravel\Versioning\VersionChange;

final class RenameTotalCentsToTotal extends VersionChange
{
    // What changed, written for API consumers. Feeds the changelog.
    public string $description = 'order.total_cents renamed to order.total, as not every currency has cents.';

    // The resource types or route names this change touches, such as ['customers'].
    public array $resources = ['orders'];

    // Old shape -> new shape: migrate payloads from older clients up to this version.
    public function transformRequest(array $payload, RequestContext $context): array
    {
        return $this->renameAttribute($payload, 'total_cents', 'total');
    }

    // New shape -> old shape: migrate responses down for clients on older versions.
    public function transformResponse(array $document, ResponseContext $context): array
    {
        return $this->renameAttribute($document, 'total', 'total_cents');
    }
}

It works in both directions. A request from an older client is translated up, so total_cents arrives in your code as total. A response is translated down, so the older client gets total_cents back. renameAttribute is one of several helpers, for renames, removals, moved and nested attributes, and changed values.

Now change your code to the new shape. Rename the column:

sh
php artisan make:migration rename_total_cents_to_total_on_orders_table
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table): void {
            $table->renameColumn('total_cents', 'total');
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table): void {
            $table->renameColumn('total', 'total_cents');
        });
    }
};
sh
php artisan migrate

Then rename total_cents to total everywhere in the app's code: the model, resource, query, form request, controller, mailable, factory and seeder. Use your editor's find and replace on those folders, or this command:

sh
perl -pi -e 's/total_cents/total/g' $(grep -rl total_cents app/Models app/Http app/Mail database/factories database/seeders)

Leave the version change, the migrations and the tests alone: they mean total_cents on purpose.

Release the version ​

sh
php artisan api:version:release 2027-01-15
text
INFO Set the default version to 2026-12-01, so clients without an Api-Version header keep their current behaviour.
INFO Released 2027-01-15 as the latest version.

2027-01-15 is now the latest version. Clients that send no Api-Version header and have no pinned key get the default, which stays 2026-12-01: a client that never heard of versions must not break either.

Try it ​

A client on the old version, such as your curl without a header, still sees total_cents:

sh
curl -g "localhost:8000/v1/orders/2?fields[orders]=total_cents,currency" -H "Authorization: Bearer $KEY"
json
{ "data": { "id": "2", "type": "orders", "attributes": { "total_cents": 4999, "currency": "GBP" } } }

Even fields[orders]=total_cents works, though the column is gone: query parameters are translated too, and so are filters and sorts. A client that asks for the new version gets total:

sh
curl -g "localhost:8000/v1/orders/2?fields[orders]=total,currency" -H "Authorization: Bearer $KEY" -H "Api-Version: 2027-01-15"
json
{ "data": { "id": "2", "type": "orders", "attributes": { "total": 4999, "currency": "GBP" } } }

The web app was not touched, and is pinned to 2026-12-01, so it still shows the totals:

sh
curl -s "localhost:3000/orders?status=paid" -b "fusion_stack_token=$KEY" | grep -o '[£$][0-9.,]*'
text
£49.99
$305.00

The API's changelog is written from your version changes' descriptions. The API reference at http://localhost:8000/docs shows it too, with a switcher between versions:

sh
php artisan api:changelog
text
## 2027-01-15
- order.total_cents renamed to order.total, as not every currency has cents.
## 2026-12-01
The first version.
A request gets a 400 invalid_api_version

The Api-Version header names a version the API does not have. The error's meta.latest_version says which is newest. Check the date, or leave the header out to get the default version.

The web app shows invalid_api_version

It is sending the version of a client generated before this one. Run npm run api in the shop folder, then restart npm run dev: the web app reads the version when it starts.

The old version shows total instead of total_cents

The version change is not registered, or not under 2027-01-15. Check changes in config/api.php: php artisan api:version:make adds it there. php artisan api:version:list lists each version with its changes.

Test it ​

When versioning is on, a new API key is pinned to the latest version when it is created, as a new integration should start on the newest API. Your key from step 2 and the reporting key from step 5 were created before, so they follow the default. apiKey() in the tests creates new keys, so tests/Feature/OrderTest.php now tests 2027-01-15, where orders have total. Update it as you did the app's code:

sh
perl -pi -e 's/total_cents/total/g' tests/Feature/OrderTest.php

Your feature tests describe the current API. Older versions get tests of their own: one that asks for each version and checks its shape, and one for the version change itself:

php
<?php

use App\Models\Order;

it('shows total from 2027-01-15, and total_cents before', function (): void {
    $order = Order::factory()->create(['total' => 2500]);
    $api = $this->api()->withKey(apiKey('orders:read'));

    $api->version('2027-01-15')->getJson("/v1/orders/{$order->id}")
        ->assertJsonPath('data.attributes.total', 2500)
        ->assertJsonMissingPath('data.attributes.total_cents');

    $api->version('2026-12-01')->getJson("/v1/orders/{$order->id}")
        ->assertJsonPath('data.attributes.total_cents', 2500)
        ->assertJsonMissingPath('data.attributes.total');
});

it('places and filters orders by total from 2027-01-15', function (): void {
    $team = Order::factory()->create(['total' => 900])->team;
    $api = $this->api()->version('2027-01-15')->withKey(apiKey('orders:read', 'orders:write'));

    $api->postJson('/v1/orders', ['data' => [
        'type' => 'orders',
        'attributes' => ['total' => 2500, 'currency' => 'GBP'],
        'relationships' => ['team' => ['data' => ['type' => 'teams', 'id' => (string) $team->id]]],
    ]])->assertCreated()->assertJsonPath('data.attributes.total', 2500);

    $api->getJson('/v1/orders?filter[total][gte]=2000')
        ->assertJsonApiCollection('orders', 1)
        ->assertJsonPath('data.0.attributes.total', 2500);
});
php
<?php

use App\Api\Versions\RenameTotalCentsToTotal;
use FusionStack\Laravel\Testing\TransformsVersionChanges;

// Version changes are plain array transforms, so they are tested without the app.
uses(TransformsVersionChanges::class);

it('shows older clients total_cents instead of total', function (): void {
    $order = ['data' => ['id' => '1', 'type' => 'orders', 'attributes' => ['status' => 'paid', 'total' => 2500, 'currency' => 'GBP']]];

    $old = $this->assertRoundTrip(new RenameTotalCentsToTotal, $order);

    expect($old['data']['attributes'])->toBe(['status' => 'paid', 'total_cents' => 2500, 'currency' => 'GBP']);
});

it('accepts total_cents from older clients', function (): void {
    $payload = $this->upgradeRequest(new RenameTotalCentsToTotal, ['data' => ['type' => 'orders', 'attributes' => ['total_cents' => 2500, 'currency' => 'GBP']]]);

    expect($payload['data']['attributes'])->toBe(['total' => 2500, 'currency' => 'GBP']);
});
sh
composer check
text
Tests: … passed (… assertions)

Upgrade the web app ​

The web app works, but it is on the old version. Upgrade it when you are ready: generate the client for the latest version, and let the type checker find what to change.

sh
cd ..
npm run api
sh
npm run typecheck
text
app/pages/orders.vue(23,…): error TS2339: Property 'total_cents' does not exist on type 'Order'.

The client describes 2027-01-15 now, where orders have total. Change the line it names:

diff
-const money = (order: Order) => new Intl.NumberFormat('en-GB', { style: 'currency', currency: order.currency }).format(order.total_cents / 100);
+const money = (order: Order) => new Intl.NumberFormat('en-GB', { style: 'currency', currency: order.currency }).format(order.total / 100);
sh
npm run typecheck

Restart the web app, so it sends the new version: in the first terminal, press Ctrl+C, then run it again.

sh
npm run dev

It now sends Api-Version: 2027-01-15, and shows the same totals:

sh
curl -s "localhost:3000/orders?status=paid" -b "fusion_stack_token=$KEY" | grep -o '[£$][0-9.,]*'
text
£49.99
$305.00

Each client upgrades in its own time, and a breaking change no longer has to break anyone. When no one uses an old version any more, php artisan api:version:usage shows you, and you can retire it.

Next: deploy.

Released under the MIT License.