API Versioning
How Zomato adds new features without breaking old apps on 1M phones.
The Problem: You change
Restaurant model. Add IsVegOnly field. Old Zomato app v1.0 on user phones crashes because it didn't expect that field. 1 star reviews.1. The Zomato Versioning Rule
v1: GET /api/v1/restaurants โ returns { id, name }
v2: GET /api/v2/restaurants โ returns { id, name, isVegOnly, rating }
Result: Old app keeps hitting v1. Works fine. New app hits v2. Gets new features. Zero breaking changes.
๐ฏ Why Versioning = Senior Skill
Junior breaks APIs. Senior supports 3 versions at once. Companies pay 25+ LPA for devs who don't break production for old clients.
2. Setup Versioning in.NET 8 - 4 Steps
Step 1: Install Package
dotnet add package Asp.Versioning.Mvc
Step 2: Program.cs
builder.Services.AddApiVersioning(options => {
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true; // Adds api-supported-versions header
options.ApiVersionReader = ApiVersionReader.Combine(
new UrlSegmentApiVersionReader(), // /api/v1/restaurants
new HeaderApiVersionReader("X-Api-Version"), // Header versioning
new QueryStringApiVersionReader("api-version") //?api-version=1.0
);
}).AddMvc(); // Needed for controllers
Step 3: Version Your Controllers
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("1.0")]
public class RestaurantsController : ControllerBase {
[HttpGet]
public IActionResult GetV1() => Ok(new { Id = 1, Name = "Pizza Hut" });
}
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("2.0")]
public class RestaurantsV2Controller : ControllerBase {
[HttpGet]
public IActionResult GetV2() => Ok(new { Id = 1, Name = "Pizza Hut", IsVegOnly = false });
}
Step 4: Test
GET /api/v1/restaurants โ v1 response
GET /api/v2/restaurants โ v2 response
GET /api/restaurants โ defaults to v1 because AssumeDefaultVersionWhenUnspecified = true
3. Swagger + Versioning = Pro Setup
Without this, Swagger shows all versions mixed. Fix:
dotnet add package Asp.Versioning.Mvc.ApiExplorer
builder.Services.AddApiVersioning(...)
.AddMvc()
.AddApiExplorer(options => {
options.GroupNameFormat = "'v'VVV"; // v1, v2
options.SubstituteApiVersionInUrl = true;
});
Result: Swagger UI has dropdown: "v1" | "v2". Clean.
Career-Killer Mistake: Breaking changes without versioning.
You rename
Rule: Never break v1. Add v2. Deprecate v1 after 6 months with warning headers. Sunset after 1 year.
You rename
Name โ RestaurantName. Old mobile apps crash. Support tickets explode. Rule: Never break v1. Add v2. Deprecate v1 after 6 months with warning headers. Sunset after 1 year.
Quick Check ๐ง
Versioning done. Next: Global Error Handling - Return clean JSON errors instead of HTML stack traces. Log everything. Make debugging easy. Continue โ
No comments yet. Be the first to share your thoughts!