REST APIs And Their Versioning – Interview Question

REST APIs And Their Versioning – Interview Question

March 5, 2019 Uncategorized 0


What are REST APIs and how they are versioned.

Let’s see how to answer this question in a technical interview.

To begin with, what are REST APIs?

The term REST stands for Representational State Transfer. Web services that adhere to REST architectural standards are called RESTful APIs. The REST APIs have a:
i) A base URI
ii) A Media Type
iii) Standard HTTP methods
REST is the most widely adopted way of implementing APIs throughout the industry.

Why Do You Need Versioning?
You need to version the APIs when you make breaking changes. With change in requirements, we might have to change the request parameters or response fields or any other changes.
If we don’t want to break existing clients using our APIs, we should create a new version while keeping the old version intact. And then once our clients start using newer version then we can deprecate the older versions in future, if needed.

Next is, How do you version the REST APIs?
There are various ways in which you can version the REST APIs. Let’s discuss them briefly how they look like:

URI Versioning
Under this method, the we add the version number in the URI itself. It looks like this:
/api/v1/users
/api/v2/users

Query String
/api/users?v=1
/api/users?v=2

Version Header
In the request headers you add a custom header like this which represents the version number:
/api/users
API-Version:1

/api/users
API-Version:2

Media Type Header
URL: /api/users
Request Header: Content-Type: Application/json; version=1

/api/users
Content-Type: Application/json; version=1

Here are couple of more uncommon ways of versioning APIs as well. I will just mention them here for the sake of completeness

Domain Versioning
Here you change the domain name itself.
Apiv1.example.com/users
Apiv2.example.com/users
This is a crude way to do it and I have never built or consumed any API versioned this way.

Date Versioning
In this technique, the server stores info about each client when they first hit the API. At that time, they save the current version number in the DB. Next time, the request comes from same client they keep giving responses based on the version number stored on the server DB. The client can specifically update or request an update to the version number.
This is again, in my opinion, difficult to maintain and I have never seen anyone using this technique so far.

CONCLUSION
To understand how these are built & versioned, I have the link to my code sample right here. I will recommend you to try implementing versioning using difference techniques, on your own before looking at the working code.
If you are able to explain this much about the REST APIs then I would say that you have successfully answered the interview question. For more such questions and their answers, please subscribe to my channel. Until next time, Happy Coding 😊