Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Views be used in RESTful APIs

Tags:

rest

I guess this is relative to API's best practices. I am looking at implementing API and not really sure if I need to use views in API design or leave them for user's frontend implementation. I don't think is a good idea to implement views in API, but it would be nice to know what usually is done. I think API calls should always return either JSON or XML; adding views complicates everything...

Do you use views in APIs, yes/no, why?

like image 622
user1863635 Avatar asked Oct 15 '25 04:10

user1863635


1 Answers

API stands for Application Programming Interface. It is something exposed by your app that can be called to interact with it. REST is a philosophy which does not tell you how to design your application, just defines a communication structure (resources, representations, verbs, nouns).

A view is not a concept in REST, but in the most common case, a view will be a representation of a resource in REST. Although this view representation can be directly returned by the server, most developer prefer to return only the data and separate the visualization.

If your API returns HTML, that would not violate REST, but it would introduce coupling between the user interface and data of your application. I recommend that you don't do it, not because of REST, but because you'd violate basic software design principles.

like image 190
Slavo Avatar answered Oct 17 '25 19:10

Slavo