An API is more than a collection of endpoints. It is a contract shared by mobile applications, web clients, integration partners, and background jobs. A field-name change that looks harmless on the server can break an older mobile release. Versioning is therefore not about creating a new URL for every edit; it is a lifecycle for managing incompatible changes deliberately.
Classify the change first
Adding an optional field is usually backward compatible. Removing a field, changing its type, making it required, or assigning a new meaning to an existing HTTP status can break consumers. For example, converting a numeric total field into formatted text may improve presentation but breaks clients that perform calculations. Add a separate formattedTotal field instead.
A URL-versioning example
var v1 = app.MapGroup("/api/v1/orders");
v1.MapGet("/{id:int}", async (int id, AppDbContext db) =>
{
var order = await db.Orders.FindAsync(id);
return order is null ? Results.NotFound()
: Results.Ok(new { order.Id, order.Total });
});
var v2 = app.MapGroup("/api/v2/orders");
v2.MapGet("/{id:int}", async (int id, AppDbContext db) =>
{
var order = await db.Orders.FindAsync(id);
return order is null ? Results.NotFound()
: Results.Ok(new { order.Id, amount = order.Total, currency = order.CurrencyCode });
});
Version 1 is not removed immediately. Record its traffic with client identity and application version, publish a migration guide, and communicate a final support date. Removal should happen only after remaining consumers are known.
Lock the contract with tests
[Fact]
public async Task V1_order_response_keeps_total_as_number()
{
var response = await _client.GetAsync("/api/v1/orders/42");
response.EnsureSuccessStatusCode();
using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
Assert.Equal(JsonValueKind.Number, json.RootElement.GetProperty("total").ValueKind);
}
This test protects the external contract rather than internal implementation. Comparing the generated OpenAPI document with an approved baseline in CI can also detect deleted properties, narrower enums, and new required fields before merge.
A safe release sequence
- Add version 2 without changing version 1 behavior.
- Test authentication, error responses, pagination, and rate limits on both versions.
- Migrate one consumer and compare production metrics.
- Track remaining version 1 traffic by owner and client release.
- Archive documentation and prepare rollback before final removal.
Conclusion
Good API versioning is product discipline, not merely a routing preference. Compatibility classification, observable usage, contract testing, and an explicit deprecation schedule allow the API to improve without interrupting existing users.
0 Comments