I am trying to gather information about REST versioning. When I check forums the most preferred one seems to be to use Accept
headers.
However if I check APIs of StackExchange, Google, Twitter, Yahoo, Instagram and eBay they all use versioning through URI.
I can't find why they prefer this way over HTTP headers. I would like to know the facts, not opinions. Can anyone help with this?
Put your version in the URI. One version of an API will not always support the types from another, so the argument that resources are merely migrated from one version to another is just plain wrong. It's not the same as switching format from XML to JSON. The types may not exist, or they may have changed semantically.
APIs only need to be up-versioned when a breaking change is made. Breaking changes include: a change in the format of the response data for one or more calls. a change in the request or response type (i.e. changing an integer to a float)
URI path versioning is relatively easy to understand and implement, but if you don't keep previous versions active, replacing your old URIs with new version numbers will break any integrations that use the old version, and clients will need to update their software to work with your API.
There really is no 'right' way to do api versioning in REST. The best article I've read explaining the different 'wrong' ways to do it is this one by Troy Hunt: Your API versioning is wrong, which is why I decided to do it 3 different wrong ways
To quote the article, he writes about three options:
- URL: You simply whack the API version into the URL, for example: https://haveibeenpwned.com/api/v2/breachedaccount/foo
- Custom request header: You use the same URL as before but add a header such as
api-version: 2
- Accept header: You modify the accept header to specify the version, for example
Accept: application/vnd.haveibeenpwned.v2+json
In the comments and discussion, a few more techniques are identified:
application/vnd.haveibeenpwned+json; version=2.0
You wrote:
I would like to know the facts, not opinions.
Unfortunately there is no such thing as facts here - for all the reasons above, any decision is based on the opinion of the person responsible.
So, while there is a lot of argument one way or the other (see also this Best practices for API versioning? and other references Troy links to) I believe many of the 'big' services converge on the URI approach for one simple pragmatic reason:
It is the simplest to understand and implement for a novice client developer.
These services want to make it as easy as possible for the most number of client developers to interact with their api, with as little support required as possible - many of whom will have only been inspired to code at all by the desire to interact with this services' api.
Manipulating a string to construct the uri is a fairly trivial task in most client languages, and many novice developers will possibly have never heard of an accept header. So, you could consider it designed to suit the lowest common denominator of developer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With