Advertisement
Advanced 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?

Short answer

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…

Save PHP mock interview
Advertisement
AModel answer
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.
Example

GET /api/v2/products // Alternative header based approach // GET /api/products // Accept: application/vnd.myapi.v2+json

Real-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.
Likely follow-ups
  • How do you handle a breaking change to an API without disrupting existing clients still using an older version?
  • How long should an API typically continue supporting an older version after releasing a new one?
Related topics