JP Garza
JP Garza

Reputation: 270

Is it good practice for a Web API to do more than CRUD operations?

I have a client (Blazor Server) application that gets data from a SQL database using a web API. My database contains a "Customers" table. My web API contains CRUD (create, read, update, delete) endpoints like these:

(Create customer) HTTP POST → "/api/customers"
(Read customer) HTTP GET → "/api/customers/{id}"
(Update customer) HTTP PUT → "/api/customers"
(Delete customer) HTTP DELETE → "/api/customers/{id}"

But I was wondering if adding endpoints like these (other than CRUD) are also good practice?

(Check if customer name exists) HTTP GET → "/api/customers/name-exist/{name}"
(Get customers count) HTTP GET → "/api/customers/count/"

Upvotes: 0

Views: 654

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40998

The short answer is that it is good practice to satisfy the business requirements of your application. If you need to expose that information, then that's what you need to do.

But those two examples you give are not outside of the CRUD model. They are both Reading (or GETting) information. That's entirely normal. There is no reason that is not acceptable.

The documentation for HTTP GET says:

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

That's exactly what you're doing in those two examples you give.

Upvotes: 2

Related Questions