5

Should an API be aware of a client version?

The idea of that really goes against the grain for me, but it seems like it is being pushed for in my current project.

Am I wrong? Can I get some reasons for or against this that I can present or understand why it might be good.

The API is a RESTful API.

8
  • Why does your API need to be aware of the client? Is there some reason beyond tracking who logs in? Mar 25, 2014 at 15:41
  • The argument given is so the consumer doesn't have to deal with data it doesn't know how to. So the API only gives it data it can deal with.
    – Nalum
    Mar 25, 2014 at 15:42
  • 2
    If the consumer doesn't know how to deal with the data, it will either ignore it, or display it as raw data. That is a concern for the consumer application, not the web api. Mar 25, 2014 at 15:44
  • That is pretty much my thoughts. If it gets something it doesn't know how to handle it should ignore it.
    – Nalum
    Mar 25, 2014 at 16:34
  • Don't web applications have to deal with the various versions of web browsers using it? So I see a precedent. Another way to handle this is for your API to support its own versioning and the client makes use of the version it knows how to handle.
    – Dunk
    Mar 25, 2014 at 22:19

4 Answers 4

16

An API should be as consistent as possible. Making the same call to the API should always do the same thing. Responding differently based on the client version would be unexpected and confusing. It may lead to subtle errors, as someone who upgrades a client would get different results and not know why.

Of course, you can make functions that provide different levels of data available to meet different needs. But they should be offered to everyone, and the users of the API can decide what level they want.

5

Almost without exception, no. Methods represent behaviors. Behaviors generally aren't supposed to change depending on who the consumer is. For example, when you walk into a restaurant, do you expect to get treated differently because of your gender or your skin color? Of course not. The implicit contract that every restaurant makes with the world is: "You come into my restaurant, I hand you a menu from which to pick things, and you eat the thing you selected." The fact that I might not know what one of the foods is is irrelevant. That's MY problem, not the restaurant's problem. The restaurant can't become a hair salon, simply because that's what I, as a client, want or expect. That's one of the ways we end up with the God Object antipattern. Clearly defining client and producer responsibilities is super important in OOAD.

So, that's the rule. There are some corner-case exceptions to it, but at the moment they aren't coming to mind.

2

The problem that you can run into without the idea of supporting client version is that it becomes very difficult to change your data representations required and sent by the API. Representation changes can force client applications to change and leave them in a broken state until they do.

Not including a client version scheme makes sense if the API is reasonably static and its methods are not subject to modification, just addition.

Ideally you shouldn't have to choose between not being able to modify the service contract and breaking clients.

The underlying code base does not necessarily have to care about the client version if you have version specific deploy points managed via an url path scheme (ie http://my.org/myservice/v1, http://my.org/myservice/v2, etc). You would have to manage multiple copies of the code with this scheme however.

1
  • 1
    "Not including a client version scheme makes sense if the API is reasonably static and its methods are not subject to modification, just addition." That should be your default stance when updating a system -- systems should be "open to extension" and "closed to change". en.wikipedia.org/wiki/Open/closed_principle Why? Tying clients and providers together is, over time, a bad thing. It violates "SOLID". Providers should provide their services TO THE WORLD (or at least to an interface), not to any particular client.
    – Calphool
    Mar 31, 2014 at 19:26
1

Although I agree with @dan1111 that a server has no business acting different according to the client version, I can see a case where you add a feature to one of the APIs and:

  • You don't want to change its name/end-point/whatever just to differentiate it from the previous version, because you plan to deprecate the previous version, and don't want the API to suffer from long term readability or cumbersome naming for all eternity
  • You want to roll-out your new feature, but during the roll-out you have both the new client, and the old client, who does not know how to handle the new feature
  • Your planned roll-out is short term enough that you know that within X days, the old client will be (for all practical purposes) extinct

In this case, I think it is a viable strategy to add your feature in a defensive way, adding ad-hoc code to support the old client as to not break it, until your roll-out is complete.
After that period, you can safely remove the ad-hoc code, so you don't end up with a patchy, encumbered code.

1
  • To avoid exactly this most APIs are versioned from day 1.
    – Darkhogg
    Mar 25, 2014 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.