REST API – Proper Way to Handle Non-CRUD Items

apiapi-designrest

So, there are many times where I have to do things with my REST API that seem outside the boundaries of REST. In this particular instance I need to have an endpoint that checks whether a member is eligible for free training or not.

I could possibly add this information onto the Read endpoint but that means that its going to get returned on every read call. This is fine this time, but what happens when there are 100 checks and now we return 100 fields that are only needed at certain times? It just gets bloated.

I wouldn't mind making an endpoint like /member/{id}/eligibleForFreeTraining and just return a true or false but that is not CRUD or REST.

Where do these kinds of calls fit into a REST'ish API?

Best Answer

Add it on to the resource. If you end up with so many of these "does this special promotion apply" questions that the resource ends up huge, split them out into related resources. For instance, GET /member/{id}/promotions. Rather than relying on URL structure, you may also wish to try using Link Headers to communicate via the /member/{id} resource where its promotions are located, or putting all promotions in a top level /promotions collection that can be filtered by member (e.g. /promotions?member=/member/{id}).

In general, when you have related additional information that's only useful in certain contexts, represent it as a separate resource that interested parties can reach from the first resource. Additionally, don't think "I need to check X", think "I need a resource that represents the result of checking X". The /member/{id}/promotions resource represents the server checking what promotions apply to a specific member and giving them to you. The server does the checking and logic. The client just asks for resources.

Related Topic