Kritim Yantra
Jun 25, 2025
“Your API is live, and clients are using it. Now what happens when you need to change something without breaking existing apps?”
That's where API versioning comes in. As your project grows, you’ll want to improve your APIs without breaking older versions. In this blog, you’ll learn how to version APIs in Laravel 12, why it’s important, and how to do it the right way — with real examples and clean structure.
API versioning lets you create new versions of your API as your app evolves — without affecting users relying on older versions.
📦 Think of it like releasing different editions of a book — same story, but improved over time.
Laravel lets you version your API in several ways:
/api/v1/posts
/api/v2/posts
Accept: application/vnd.myapp.v1+json
For most Laravel apps, URL-based versioning is the easiest and most maintainable.
Inside routes/
, keep using the default api.php
, but we’ll organize controllers by version:
app/
└── Http/
└── Controllers/
└── API/
├── V1/
│ └── PostController.php
└── V2/
└── PostController.php
In your routes/api.php
, group your routes like this:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\API\V1\PostController as V1PostController;
use App\Http\Controllers\API\V2\PostController as V2PostController;
Route::prefix('v1')->group(function () {
Route::apiResource('posts', V1PostController::class);
});
Route::prefix('v2')->group(function () {
Route::apiResource('posts', V2PostController::class);
});
🔁 This way, both versions can coexist — no conflicts!
Each version of the controller can have different logic:
namespace App\Http\Controllers\API\V1;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return Post::all(); // raw response
}
}
namespace App\Http\Controllers\API\V2;
use App\Models\Post;
use App\Http\Resources\PostResource;
class PostController extends Controller
{
public function index()
{
return PostResource::collection(Post::paginate(10));
}
}
Now v1/posts
returns raw data, while v2/posts
is more polished — without breaking old clients.
Here are some best practices to follow:
V1
, V2
, etc.)You have an endpoint:
GET /api/v1/products
Returns:
[
{ "id": 1, "name": "T-shirt", "price": 100 }
]
Now marketing wants to show discounts and ratings. You create:
GET /api/v2/products
Returns:
[
{
"id": 1,
"name": "T-shirt",
"price": 100,
"discounted_price": 80,
"rating": 4.5
}
]
Older mobile apps using v1 keep working, while newer apps use v2. ✅
/v1/
, /v2/
) for clarity and controlNo comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google