Skip to main content
  1. Posts/

RESTful API Versioning

·505 words·3 mins
Chris Ayers
Author
Chris Ayers
I am a father, nerd, gamer, and speaker.

I’ve been a developer for a long time, writing APIs and clients to consume them. When an API is around long enough, it needs to change. I’ve versioned APIs in the past using a number of different techniques. Some successful, some painful. Now I realize this discussion is like the VI/Emacs conflict, the Tab/Space wars, and the Spanish Inquisition, but it is a good topic to look at. There are a few main styles when it comes to API versioning:

  • URL
  • Query Parameter
  • Accept header
  • Custom request header

URL
#

This is where a lot of people start, you might have seen it out there. You can easily see the version of the API you are calling. And a lot of times behind the scenes, the base API points to the latest version. The issue a number of people have with this, myself included, is that a URL in RESTful APIs should represent an entity, not a version of an entity. For example, /api/v1/users/27 and /api/v2/users/27 are not two different users. As you add new versions, you break permalinks to resources. And you’re tying the URL of a resource, not to a version of it.

/api/users  -> /api/v2/users
/api/v1/users
/api/v2/users

Query Parameter
#

Another way people can version APIs is by Query Parameter. This keeps the URL consistent, but adds an extra parameter. This is fine by my book, you’re adding extra info to the request, not the resource. It can change per request, and doesn’t change the permalink or URL of the resource.

/api/users -> /api/users?api-version=1.0
/api/users?api-version=1.0
/api/users?api-version=2.0

Accept Header
#

The third way I’ve seen API versioning is an accept header on the http(s) request. Accept headers describe how you’ld like to receive the data. Being part of the http headers, this can be taken into effect during caching, and if not specified explicitly can return the latest version. I like this method quite a bit, the client is telling the server how it wants to receive the data, the format and the version.

GET api/users HTTP/1.1
host: localhost
accept: application/json;v=2.0

Custom Header
#

The last way I’ve seen is to define a custom http header. Just adding the requested version as a header to the request. This isn’t as easily compatible with clients as the other methods.

api-version: 1.0

Implementation
#

In the past, I’ve had to roll my own. I started with URL versioning. As I worked with RESTful APIs, I realized there was a better way and started moving towards either the Query parameter or an Accept Header. There are some pretty good libraries that will handle a lot of this for you today.

Microsoft has a library that works with .Net Framework 4.5 and .Net Core. It’s called ASPNet API Versioning, https://github.com/microsoft/aspnet-api-versioning . They also released libraries to work with Swagger for API documentation. Honestly, its pretty nice and supports a number of methods for versioning that you can customize pretty well. This follows the Microsoft REST API Guidelines.

There are some more references to check out at http://apistylebook.com/design/topics/versioning

Related

ARM - Part 3: Hook up the Pipes

·211 words·1 min
I’ve got a template straight from Microsoft. I want this wired into a CI/CD pipeline to I can play around and get quick feedback. I’m going to use Azure DevOps to help make all this possible. Let’s get those templates into a repository to get started. New repository, initialize it, add new files. Next, I’m going to create a new resource group to play around with my web app resources.

ARM - Part 2: Azure Quickstart Templates

·528 words·3 mins
Time to Dive in # I’m one of those guys that likes to learn by doing. Reading the documentation is great, and I do that a lot. But for me to really grok something, I need to play with it, run it, and probably blow it up. If you missed part 1, read along and come back. I need a WebApp setup for my sample project. I realized I can do it a few ways. Some of the ways are very manual, some are repeatable, but one stood out to me.

ARM - Part 1: Azure Resource Manager

The Journey Begins # I’ve been an azure developer for years. Originally I worked with “Classic Mode” and Cloud Services. Then I moved to ARM and Web Apps. Lately I’ve been doing DevOps but I only recently started working with ARM Termplates. First, let’s dive into a little history. History # Azure has grown and changed since it was first introduced. Originally, it was a research project called, “Project Red Dog”. Azure has been commercially available since 2010. For four years, there was a limited way to interact with Azure, ASM the Azure Service Manager.