Add Fusion Stack to an existing app
Fusion Stack is a Composer package, so it fits into the Laravel app you already have. It only changes routes you put in its fusion-stack middleware group: your web pages, your existing API routes and your authentication carry on as they are. You can then move endpoints over one at a time.
This guide adds a versioned, documented users endpoint to a typical app: one that serves web pages and has an API set up with Laravel's install:api, which uses Sanctum. You need Laravel 13 and PHP 8.4+.
Try it on a fresh app first
To follow along without touching your own app, create one like it:
composer create-project laravel/laravel my-app
cd my-app
php artisan install:api --no-interaction1. Install the package
composer require fusion-stack/laravel
php artisan api:installconfig/api.php … published
routes/api.php … exists, add the fusion-stack middleware
bootstrap/app.php … API routes already registeredapi:install changes as little as it can:
- It publishes
config/api.php, with JSON:API error rendering limited to your API's paths (errors.pathsis['api/*']), so web pages keep Laravel's error pages. - It leaves an existing
routes/api.phpalone. Without one, it creates it and registers it inbootstrap/app.php, asinstall:apiwould. - It offers to run the package's migrations, for API keys, version usage and webhooks. They only add tables of their own.
- It asks whether to install the MCP server. You can add it later.
2. Add a v1 group
Fusion Stack's features come from the fusion-stack middleware group: request IDs, API keys and scopes, rate limits, idempotency, HTTP caching, versioning, JSON:API errors and the API reference. Put the endpoints that should have them in a group under v1, next to your existing routes:
<?php
+use App\Http\Controllers\Api\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
+
+Route::prefix('v1')->middleware('fusion-stack')->group(function (): void {
+ Route::get('users', [UserController::class, 'index'])->name('users.index');
+});/api/user works exactly as before. The group has its own rate limiter, so do not add Laravel's throttle middleware to it as well. The middleware reference lists what the group runs, in order.
3. Write the endpoint
An endpoint has three parts: a resource that says what clients see, a query that says what they may filter and sort by, and a controller action.
<?php
namespace App\Http\Resources;
use FusionStack\Laravel\OpenApi\Describe;
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
#[Describe('name', 'The name the user signed up with.')]
#[Describe('created_at', 'When the user signed up.')]
final class UserResource extends JsonApiResource
{
/** @var list<string> */
public array $attributes = ['name', 'created_at'];
}The resource lists only name and created_at, so the users' emails and passwords never reach the API. #[Describe] documents each field in the API reference.
<?php
namespace App\Http\Queries;
use App\Models\User;
use FusionStack\Laravel\Query\ApiQuery;
/** @extends ApiQuery<User> */
final class UserQuery extends ApiQuery
{
protected string $model = User::class;
protected array $sorts = ['name', 'created_at'];
protected string $defaultSort = 'name';
}Clients may sort by name or created_at, and get a 400 for anything else. The queries reference covers includes, filters and pagination.
<?php
namespace App\Http\Controllers\Api;
use App\Http\Queries\UserQuery;
use App\Http\Resources\UserResource;
use FusionStack\Laravel\Auth\RequiresScope;
use FusionStack\Laravel\OpenApi\Endpoint;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
final class UserController
{
#[RequiresScope('users:read')]
#[Endpoint('List users', 'Everyone with an account, by name.')]
public function index(UserQuery $query): AnonymousResourceCollection
{
return UserResource::collection($query->paginate());
}
}#[RequiresScope] lets only clients with the users:read scope in. Others get a 401 or 403. #[Endpoint] names it in the reference.
Give your app some users to list, if it has none, and start it:
php artisan tinker --execute 'App\Models\User::factory(3)->create();'php artisan serve4. Call it with an API key
API keys are for other systems that call your API, such as a partner's integration or a reporting job. Create one with the scope the endpoint needs:
php artisan api:key:create "Reporting" --scope=users:readINFO Created API key 1. Copy it now: it is not stored and cannot be shown again.
fs_live_…
Scopes … users:readexport KEY=fs_live_…
curl -g "localhost:8000/api/v1/users?page[size]=2&sort=-created_at" -H "Authorization: Bearer $KEY"{
"data": [
{ "id": "3", "type": "users", "attributes": { "name": "Leann Williamson", "created_at": "2026-12-01T12:09:51.000000Z" } },
{ "id": "2", "type": "users", "attributes": { "name": "Catharine Hauck", "created_at": "2026-12-01T12:09:51.000000Z" } }
],
"links": {
"first": "http://localhost:8000/api/v1/users?sort=-created_at&page%5Bsize%5D=2",
"prev": null,
"next": "http://localhost:8000/api/v1/users?sort=-created_at&page%5Bsize%5D=2&page%5Bafter%5D=eyJ…"
},
"meta": { "page": { "size": 2 } }
}Without the key, the response is a JSON:API error with a stable code, as every error under /api now is:
curl localhost:8000/api/v1/users{
"errors": [
{
"status": "401",
"code": "unauthenticated",
"title": "Unauthorized",
"detail": "Authenticate to use this endpoint.",
"meta": { "request_id": "req_01m8rr5vkt88hgpgfjxgdrpb10" }
}
]
}The API reference at http://localhost:8000/docs now documents the endpoint, its sort options, scope and errors.
5. Keep your existing authentication
The fusion-stack group tries the guards in api.auth.guards in order: API keys first, then sanctum. So your users' Sanctum tokens work too, and a token's abilities are its scopes. Give the User model Sanctum's HasApiTokens trait, if it does not have it yet:
use Illuminate\Notifications\Notifiable;
+use Laravel\Sanctum\HasApiTokens;- use HasFactory, Notifiable;
+ use HasApiTokens, HasFactory, Notifiable;Then a token with the users:read ability can list users, and one without it gets a 403:
export TOKEN=$(php artisan tinker --execute 'echo App\Models\User::first()->createToken("cli", ["users:read"])->plainTextToken;')
curl -s -o /dev/null -w '%{http_code}\n' localhost:8000/api/v1/users -H "Authorization: Bearer $TOKEN"200export TOKEN=$(php artisan tinker --execute 'echo App\Models\User::first()->createToken("cli", ["posts:read"])->plainTextToken;')
curl localhost:8000/api/v1/users -H "Authorization: Bearer $TOKEN"{
"errors": [
{
"status": "403",
"code": "insufficient_scope",
"title": "Insufficient scope",
"detail": "This request needs the [users:read] scope.",
"meta": { "required_scope": "users:read", "request_id": "req_01m8rr6bnh2exfheb46t74s916" }
}
]
}A first-party single-page app signed in with Sanctum's cookies has every scope, as Sanctum's tokenCan() does. Inside the group, $request->user() is whoever authenticated: a User for Sanctum, an ApiKey for an API key.
Other guards, such as Passport or your own, work the same way: add them to api.auth.guards, and say which of the user's attributes hold its scopes in api.auth.attributes. See auth.
6. Check your web pages are unchanged
Errors outside /api still get Laravel's own pages:
curl -s -o /dev/null -w '%{http_code} %{content_type}\n' localhost:8000/missing
curl -s -o /dev/null -w '%{http_code} %{content_type}\n' localhost:8000/api/v1/missing404 text/html; charset=utf-8
404 application/vnd.api+jsonIf your API lives somewhere other than /api, set errors.paths to match it.
7. Move existing endpoints over
There is no need to move everything at once. For each endpoint:
- Add its
v1version to the group, returning a resource. Clients see a different document from your old one: JSON:API, with errors in one format and rate limit and caching headers. - Test it with the testing helpers.
- Move clients over, then remove the old route once nobody calls it.
Keep old routes outside the group until then, so clients that rely on their format keep working.
8. Turn on versioning when you need it
Versioning is off, so clients need no Api-Version header and every request gets the current API. Turn it on before your first breaking change, with today's date as the baseline:
'versions' => [
- 'enabled' => false,
+ 'enabled' => true,
- 'latest' => null,
+ 'latest' => '2026-12-01',php artisan api:version:list| Version | Status | Changes | Description |
| 2026-12-01 | latest, default | 0 | |Every client gets the baseline, and new API keys are pinned to it. When you make a breaking change, describe it with php artisan api:version:make and release it with php artisan api:version:release. Clients without a pinned version, such as API keys created while versioning was off, stay on the baseline. Versioning explains how.
Next steps
- Reference: every option, attribute and command.
- Concepts: versioning, JSON:API, caching and idempotency.
- Build your first API: the whole workflow on a new app, from the first resource to deploying.