When you develop your application for consumption by other applications, you do so by exposing your application's endpoints. This endpoint could emit XML or JSON data depending on whether SOAP or REST protocol is supported. As reality would have it, the domain keeps evolving keeping up with the demands of the consumer applications and the challenge is not all consumer applications want the same kind/structure of data or that they aren't keeping up with the pace of your evolution. So that brings the need for versioning your API endpoints, to meet the backward compatibility of at least some of your consumer applications. This leaves us with the question, as to how do we do versioning of our endpoints? And this post is about versioning the REST endpoints.
There are 4 below ways in which you can version your REST APIs:
Option 1: URI Versioning
Option 2: URL Query Request Parameter Versioning
Option 3: Custom Request Header Versioning
- Any headers beginning with X- are custom headers, and are not included in the HTTP spec..
- There are a few interesting bits in the response headers. As expected, the Content-Type is application/json.
Option 4: Media Type Request Header Versioning
- Accept header in http advertises which content types, expressed as MIME types, the client is able to understand. Using content negotiation, the server then selects one of the proposals, uses it and informs the client of its choice with the Content-Type response header.
- The vendor prefix (vnd.) indicates that it is custom for this vendor.
- The +json indicates that it can be parsed as JSON, but the media type (defined by Content-Type header) should define further semantics on top of JSON.
- Do you (or your consumer applications) see versions in URL as noise polluting it? If so, you can rule out options 1 and 2 in the list above.
- Do you (or your consumer applications) see versions in custom Headers as noise polluting it? If so, you can rule out option 3 and potentially even option 4, if you are too finicky about it.
- Which approach is the easiest to implement? An answer to this depends a lot on your choice of framework and architecture. Does your choice of framework support routing for sophisticated URL Versioning?
- How does it affect caching? For instance, header based versioning (options 3 and 4) affects URL based caching, because all the versions share same URL; you will have to change your caching strategy to accommodate request headers in them.
- Do you document your API for publishing? What is its implication on the choice of tool for documentation to accommodate versioning strategy to how it gets published.
- URI Versioning : Twitter, Twilio, Plivo, Google. Salesforce, CircleCI, Paypal, Zomato etc
- URL Query Request Parameter Versioning : Microsoft, etc
- Custom Request Header Versioning : Microsoft, Github etc
- Media Type Versioning : Microsoft, etc