PHP › RESTful API Development with PHP
How should a RESTful API handle versioning as its design evolves over time, and what are the tradeoffs between common approaches like URL based versioning and header based versioning?
AdvancedAnswer
URL based versioning, such as including a version number directly in the path like slash api slash v2 slash products, is simple and highly visible but means every version essentially lives at a different URL, while header based versioning, using a custom request header to specify the desired version, keeps URLs cleaner and more stable over time but is less discoverable and requires clients to correctly set that header on every request, meaning the right choice often depends on your specific API consumers' needs and how frequently you anticipate needing breaking changes.
GET /api/v2/products
// Alternative header based approach
// GET /api/products
// Accept: application/vnd.myapi.v2+jsonReal-world example A public API initially launches with URL based versioning for its simplicity and clear visibility, giving external developers immediate, unambiguous confidence about exactly which version of the API their integration is calling.