Api -Versioning

In todays world , the need for rest api’s is increasing constantly , with the increase in writing complex API’s there are scenario’s where there can be an addition of a parameter to the Rest API which changes the whole dynamic of the API’s. This results in Rework and modifying  the API”s.

To solve this we need to do API Versioning.

What is API Versioning??

Having Versions for same endpoints is Api Versioning, Say for Example we have an endpoint “/getUserDetails “with one request parameter UserId , In the future there is a requirement of adding one more request parameter Username , instead of changing the old API we write a new API with same endpoint and versioning the two api’ s as V1 and V2.

Api Versioning can be done using

1-URI Versioning

2-Versioning Using Accept Header.

 

URI Versioning : In URI versioning we use versioning in the URL address itself.

Example Code Snippets:

urlapi

Versioning Using Accept Header: 

Accept header:You modify the accept header to specify the version, for example

“Accept: application/vnd.haveibeenpwned.v2+json”.

Content negotiation may let you to preserve a clean set of URLs but you still have to deal with the complexity of serving different versions of content somewhere. This burden tends to be moved up the stack to your API controllers which become responsible for figuring out which version of a resource to send. The end result tends to be a more complex API as clients have to know which headers to specify before requesting a resource.

Example Code Snippets:

AcceptHeader

There are some advantages of versioning using accept header

  • With a URI-versioned API, resource identification and the resource’s representation is munged together. This violates the basic principles of REST; one resource should be identified by one and only one endpoint. In this regard, the Resource Header versioning choice is more academically idealistic.
  • It is easier to add versioning to the HTTP Headers for an existing project that didn’t already have versioning

 

Summary: API Versioning gives us lot of benefits in real time situations, there will be no rework of the existing code ,although sometimes there might be duplication of code.

Leave a comment