Designing a Versioning Strategy for Your Laravel API
Most Laravel APIs don't get versioned until the first breaking change is already unavoidable — a field needs to be renamed, a response shape needs to change — and by then it's a scramble. This tutorial sets up a versioning structure before you need it: route groups per version, versioned API Resources, and deprecation headers that give consumers real warning.
Step 1: Version-prefixed route groups
Put every version behind its own prefix and route file, loaded from RouteServiceProvider or bootstrap/app.php:
// routes/api_v1.php
Route::prefix('v1')->name('api.v1.')->group(function () {
Route::get('/orders/{order}', [V1\OrderController::class, 'show']);
});
// routes/api_v2.php
Route::prefix('v2')->name('api.v2.')->group(function () {
Route::get('/orders/{order}', [V2\OrderController::class, 'show']);
});
Namespacing controllers per version (App\Http\Controllers\Api\V1, App\Http\Controllers\Api\V2) keeps each version's request handling independent — v2 can extend v1's controller and override only what changed, rather than branching on version inside a single shared controller.
Step 2: One resource class per version
The response shape is usually where breaking changes actually live, so version the API Resource, not just the route:
// app/Http/Resources/V1/OrderResource.php
class OrderResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'total' => $this->total_cents / 100,
'status' => $this->status,
];
}
}
// app/Http/Resources/V2/OrderResource.php
class OrderResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'total' => [
'amount' => $this->total_cents,
'currency' => $this->currency,
],
'status' => $this->status,
'fulfillment' => new FulfillmentResource($this->whenLoaded('fulfillment')),
];
}
}
Here, v2 changes total from a plain decimal to a structured amount/currency object and adds a new fulfillment relationship — exactly the kind of change that would break every v1 consumer if it happened in place instead of behind a version boundary.
Step 3: Deprecation headers that get noticed
A version being "deprecated" only matters if consumers actually see it. Middleware applied to the whole v1 group can attach a standard deprecation header to every response:
class MarkDeprecatedVersion
{
public function handle(Request $request, Closure $next, string $sunsetDate)
{
$response = $next($request);
$response->headers->set('Deprecation', 'true');
$response->headers->set('Sunset', $sunsetDate);
$response->headers->set('Link', '</docs/api/v2>; rel="successor-version"');
return $response;
}
}
Route::prefix('v1')
->middleware(['deprecated:Wed, 01 Jul 2027 00:00:00 GMT'])
->group(function () {
// ...
});
Deprecation and Sunset are real, standardized HTTP headers (RFC 8594 for Sunset), and API client libraries increasingly surface them automatically — some log a warning, some fail CI in a consuming team's test suite if they see a Sunset date in the past. That's a far more reliable notification path than an email to a mailing list nobody reads.
Step 4: Testing both versions stay correct independently
it('returns the v1 order shape', function () {
$order = Order::factory()->create(['total_cents' => 4200]);
$this->getJson("/v1/orders/{$order->id}")
->assertJson(['total' => 42.00]);
});
it('returns the v2 order shape', function () {
$order = Order::factory()->create(['total_cents' => 4200, 'currency' => 'USD']);
$this->getJson("/v2/orders/{$order->id}")
->assertJson(['total' => ['amount' => 4200, 'currency' => 'USD']]);
});
Running both suites on every deploy is what actually prevents "fixing" v2 from silently breaking v1 — without dedicated tests per version, it's easy to refactor a shared underlying query and only notice the v1 response changed shape when a customer's integration breaks in production.
Choosing a versioning signal
URL-prefixed versions (/v1/orders) are the most common approach and the easiest for API consumers to understand and bookmark against, which is why this tutorial uses it — but header-based versioning (an Accept: application/vnd.phparchitect.v2+json header) is a legitimate alternative if you'd rather keep URLs stable and version the content negotiation instead. Whichever you pick, the harder-won lesson is the same: decide on a versioning boundary before your first breaking change forces one on you, because retrofitting version boundaries onto an API that's already shipped unversioned means finding every existing consumer first.