r/SoftwareEngineering • u/ChallengeFit2766 • Sep 23 '25
Cardinality between APIs and resources?
For instance say for an e-commerce application we need the following endpoints:
GET /user/{id} : Get user with "id"
POST /user : Create new user
PUT /user/{id} : Update user with "id"
DELETE /user/{id} : Delete user with "id"
GET /product/{id} : Get product with "id"
POST /product : Create new product
PUT /product/{id} : Update product with "id"
DELETE /product/{id} : Delete product with "id"
Could 'user' and 'product' endpoints be considered part of the same single API or do they have to be considered two separate APIs? Every API example I've seen out there operates on just a single resource.
5
u/Sighlence Sep 24 '25
What do you mean “part of the same single API” or “two separate APIs”?
5
u/CallMeKik Sep 25 '25
The consumer of this API shouldn’t actually care whether the interfaces are served by different services* or not. The choice is yours.
You will notice that almost every API framework supports having more than one resource because it’s a common pattern.
As someone who has built quite a lot of stuff from the ground up I would recommend starting with a single service and keeping these things relatively decoupled inside. You can then have different services later if you need to do so.
—-
*By service here I mean a collection of processes that are deployed as a whole. Not an internal “service layer”.
1
u/Grizzly_Addams Sep 23 '25
How are user and product related? I'm not seeing anything that would make sense having them in the same API as standalone entities.
Now, if you're rolling them into a cart, then you're cooking with fire, but that would be another API.
1
u/ArieHein Sep 24 '25
CRUD APIs usualy mimic the database as they rely on data at the backend.
Your database design and schema decide, in many cases, your api structure.
Would you place the users and products in same table? Usualy no. But your invoice table might have the userid and list of productid that were purchased.
Even if you dont go for pure relational design and go for nosql, you would still have a separate table for users and one for products but the invoice table design and data would be different.
1
u/_SteppedOnADuck Sep 25 '25
Single responsibility principle.
1
u/grappleshot Sep 25 '25
Depends. My company tends to put them as separate API's - as in hosted user.company.api and product.company.api and so on. But that's because we have separate teams taking care of separate domain areas. users.company.api might have 30-40 endpoints and various service bus event handlers and so on. The same and more with other "microservices". We have probably 8 teams with each team looking after between 1 and 10 api's each, each staffed by between 3-6 engineers.
Other places I've worked, there's one physical API with 30+ endpoints, maintained by just a few people for both front and backend.
1
u/somedaan Sep 28 '25
I would probably put these endpoints in the same API, especially if user and product are related. If they aren’t related you could still revoke access to the product endpoints based on the API key for example.
1
u/bin_chickens Oct 05 '25
OP, I know I'm a little late here, but some of these responses are missing the point here and semantics matter, because your question is not well specified.
It seems like you're building a RESTful API service, with multiple endpoints and actions for many resources.
In general, an API is a collection of endpoints for your application that exposes functionality for internal or external consumption. If you look at most major companies, APIs are split out only when they serve a different purpose/product or are owned separately (as per Conway's law) - e.g. Amazon S3 API vs Amazon EC2 API.
From a URL perspective you can absolutely group these under a route e.g. /api/{version}/{resource}/{id}/{optional sub resource...}... and call it one API. Note: there are other approaches to versioning but i won't cover that.
The endpoints and actions (HTTP Verbs) per resource should be are implemented as separate concerns, in one service codebase, with the shared authz logic, and per resource action logic ideally in your data/business logic laters or libraries (depending on your language, framework and conventions).
TLDR: For external consumption, it should appear and be documented as one API where the authz and business domain are the same. The implementation may vary, but don't break your codebase up unless you have a reason to.
1
u/Significant-Leek8483 Nov 06 '25
You mean same codebase/microservice?
1
u/rickosborn Nov 29 '25
I would find my five key uses cases in the system. What do most users execute. For me, that drives how to group services and api’s. IE if I need to share state between user and product quite often, possibly keep them in one service and customize the api to allow this shared state.
1
u/[deleted] Oct 01 '25
[removed] — view removed comment
1
u/[deleted] Oct 01 '25
[removed] — view removed comment
1
u/[deleted] Oct 18 '25
[removed] — view removed comment
1
u/[deleted] Oct 27 '25
[removed] — view removed comment