Monthly Archives: November 2013

REST API versioning

In any client/server system, managing changes in the server can be challenging. RESTful web services are certainly no exception – especially publically available APIs. The consumers of a RESTful web service (the clients) rely on the web service to not break the contract.

Normally, the main concern will be to maintain backward compatibility. This means that if you update the server you must ensure that existing consumers will still work flawlessly.

There are several techniques to avoid disruptive changes and thus maintain compatibility – for example continuing to support existing query parameter formats, even if new ones are introduced, treating new query parameters as optional, not removing or renaming fields from resource representation bodies etc.

You should always do everything possible to avoid versioning of your web service (Cool URIs don’t change). However, even when doing your utmost, you might eventually find yourself in a situation where maintaining compatibility is impossible and some kind of versioning of your web service is inevitable.

Once you reach this point, there are two principal techniques to choose from:

  • URI versioning
  • Media type versioning

URI versioning involves including a version number in the URI. You do not necessarily have to version every resource of your service. Here is an example:

URI versioning is definitely regarded as good practice, and many well-known RESTful APIs uses URI versioning – for example LinkedIn and Groupon. However, the solution I will describe in more details is the other one: media type versioning.

Media type versioning is based on content negotiation using the HTTP headers Accept and Content-Type. The idea is that the web service, for each incoming request, has defined what version of a resource representation (aka. the media type) that it can consume and/or produce.

Likewise, when the consumer makes a request, it must include headers defining the version of the resource representation that it provides (if any) and the version of the resource representation that it expects to receive back (if any). Then the web service can easily detect whether it supports the provided/requested version of a representation. If not, it can respond with a well-defined error code.

The media types must be defined as so called vendor-specific media types. Vendor-specific media types are specialized alternatives to the standard media types such as application/xml, application/json etc. The vendor-specific media types can comprise information about the resource type (e.g. user), the version (e.g. v2) and the format (e.g. JSON):

Each GET request must comprise an Accept header defining the resource representation that the consumer expects to get back. In the below example, a request to get a representation of the user called john_doe is shown:

If the web service cannot provide the resource representation as stated in the Accept header, according to RFC2616, a response with HTTP error code 406 (Not Acceptable) shall be returned.

Each POST and PUT request providing a request body with a resource representation must provide a Content-Type header defining the resource representation. In the below example, a request to update the profile of a user called john_doe is shown:

If the web service does not accept the resource representation as stated in the Content-Type header, according to RFC2616, a response with HTTP error code 415 (Unsupported Media Type) shall be returned.

Here is a recipe on how this versioning approach can be easily applied in a Jersey (Java) based solution:

For each resource representation class, include a static field defining the vendor-specific media type. For a User class it can look for example like this:

In the web service class, use this static field when setting the media type:

The cool thing is that Jersey automatically responds with the correct HTTP error codes (406 and 415) if the media types (the versions) don’t match.

It’s my personal experience that the majority of contract-breaking changes in RESTful web services come from breaking changes in the resource representations rather than in the resource model of the web service – i.e. the URI’s (the resource identifiers) of your service. This is why, in many cases, the media-type versioning solves most of your versioning challenges – while keeping the URI’s of your service intact.