Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST api versioning (only version the representation, not the resource itself)

I had a look at Best practices for API versioning?, but am not quite convinced of the answer, so I am question the versioning part again with a more specific example. I am having two URIs (one with versioning as part of the URI and one without):

http://xxxx/v1/user/123    -> favored solution in discussed thread
http://xxxx/user/123             

I am having my doubts whether the first link expresses the idea of REST. I find http://xxxx/v1/user/123 confusing as it suggests that there will be a higher api-version someday like http://xxxx/v2/user/123. But this does not make sense in REST terms, the api version itself is HTTP 1.0 or 1.1, which is already sent inside the HTTP request. This REST resource centric view differs very from other api-interfaces like SOAP or Java-interfaces (where it is common to have api-versions in qualified names).

At REST the only thing where versioning makes sense is the representation of that resource (e.g. new fields are added or removed). This versioning belongs to the part of content-negotiation like:

http://xxx/user/123 + HTTP 'Accept' Header -> Content negotation through header
http://xxx/user/123?v=1                    -> for perma-links/hyperlinks

One could also argue that such version content-negotiation could be part of the URI inside the path, but I find it counter-intuitive, because you could end-up with different URIs for the same resource and have to maintain redirects at some point.

To sum-up: In REST URIs there is no api-versioning, only versioning of the resource's representation. Representation version-info belongs to content-negotiation (as queryParam or HTTP 'Accept').

What do you think? In which things would you disagree/agree?

like image 313
manuel aldana Avatar asked Jan 07 '10 23:01

manuel aldana


People also ask

What is the most important thing about versioning an API?

The most common reason for API versioning is honoring contracts with your existing API customers. Their apps may rely upon the way your API functions when they set it up. Just because you need to update your API doesn't mean they're ready to do the same for their apps.

What is v1 and v2 in REST API?

The content structure. The main difference between the API v1 and v2 is the way that the content is structured. The content structure for API v1 is more complex and meant to be used with one of our development kits.


3 Answers

I completely agree; a URI expresses identity, identity doesn't change when a new version is introduced. There might be new URIs for additional concepts, of course, and existing URIs might redirect … but including a "v2" in the URI smells RPCish to me.

Note that this has got nothing to do with REST, really, as from a REST perspective it's all just characters.

like image 51
Stefan Tilkov Avatar answered Sep 28 '22 13:09

Stefan Tilkov


You could listen for an X-API-Version HTTP request header. If the header exists, then the server must use that version of the API. If the header does not exist, the server may use the latest version of the API.

> GET /user/123 HTTP/1.1 > Host: xxx > X-API-Version: >=1.5.1, <2.0.0 > Accept: application/json >  < HTTP/1.1 200 OK < X-API-Version: 1.6.12 < < { "user": { "id": 123, "name": "Bob Smith" } } < 
like image 29
yfeldblum Avatar answered Sep 28 '22 12:09

yfeldblum


For what it is worth, I agree with you Manuel. You can see my reasoning in this question How to version REST URIs

There are plenty of people that seem to disagree but I would not worry. What your url looks like really does not have a big impact on your client as long as you respect the hypertext constraint.

like image 21
Darrel Miller Avatar answered Sep 28 '22 11:09

Darrel Miller