Versioning an API

I’m starting a side project, the first stage will be a building a web application with MVC, in later stages we will be adding clients for mobile platforms. My thought was to create one API that all of the applications (web and mobile) go through to get/save data.

Because these different platforms will be on different release cycles I’ll need a way for, say IPhone, to work with one version of the API while the website is using an updated version. What’s the best way to do that?

My ideas so far are:

  • Create a separate project to host the MVC Web API and host that in a subdomain or in a subfolder of the root site. Then either reference the DLL directly or reference it through the web (seems like an unnecessary http call)
  • House the API within the MVC project that will be the website and try to version it based on url in there. I did some quick testing with that this morning and wasn’t able to get it to work, it always resided at api (I couldn’t get it to reside at api_v2)

In a large service with multiple consumers and integrated services, here’s some principles and guidelines we followed to be RESTful over our web api. If you’re not trying to be RESTful etc… and just want a simple http api, then it may not apply.

Key Principles:

  • A resource is identified by a RESTful URL (site.com/api/user/6).
  • That URL needs to be a permalink (always refers to that resource). External services and consumers of the API can persist that permalink
  • Different versions of the client may ask for site.com/api/user/6 and depending on version, may get a different representation (version) of the resource.
  • We try hard to get it right, review APIs, etc… but we realize we won’t get it right sometimes and want the ability to be able to version it and get it right.

We considered putting the version in the URL, but that breaks key RESTful principles.

https://stackoverflow.com/questions/389169/best-practices-for-api-versioning

Good read on web api design and versioning

What we did (basically, waving hands over details):

  • Put version of request in accept/content type header using MIME type properties: application/json;version=2 (we considered vendor MIME types which BTW, github uses). We also support passing in querystring for web mashup scenarios.
  • We customized the routing so a request for version 2 of the resource request would route to the (Resource)2Controller.
  • On an http OPTIONS request, the server would iterate over the routes and expose the resources, available route templates and min/max version (our custom attribute put on the controller/classes).
  • The client knows what version it is so the first thing the client did is an OPTIONs request and negotiated the max version the client and server understands then it would send that in the header.
  • Since the server has multiple controllers, it’s capable of handing back different versions of that resource.
  • Even though we’re capable of versioning the resource, we refrain and take an purely additive approach if possible (older clients ignore extra properties on deserialization). iOS will do this fine since it’s just extra data in the dictionaries.
  • One benefit we got from the OPTIONs exposing routes, was we actually avoid path math on the client. It knows what version it is – the http client passes in a dictionary and urls are formatted using the server advertised routes.

I am considering writing a blog and sharing how we integrated into web api to version our resources. If I do, I will follow up here with a link.

3

Your API shouldn’t care about who/what is consuming it. The API provides a contract and fulfills it. It is up to the consumer to know what to do with the result. I don’t see why your iPhone and web app would need a different contract.

1

I concur with what Jean-Bernard said. You should have one API so everybody can access it.

But that said, you will eventually have more than one version. You would manage that on different branches on your Source Control system. If you ever need to have more than one version published I recommend using subdomains:

  • http://api.yoursite.com
  • http://apiv2.yoursite.com

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật