Anush
Anush

Reputation: 41

Api naming in microservices design

Let's say that there are two microservices representing the resources orders(/orders) and customers(/customers). My requirement is to get all the orders made by a customer. Had it been a monolithic application, I would have modeled my uri as /customers/{id}/orders. This would have hit the customers resource and made an in-memory service call to get the corresponding orders. Now, in case of microservices, this isn't possible. So, is the only way to get the orders is to make a remote service call or is there a better way of doing it? Can we create another resource with the representation /ordersByCustomers/{customerid}?

Upvotes: 3

Views: 3251

Answers (2)

Opal
Opal

Reputation: 84854

In general you should beware of using endpoint that are more or less similar to the one you suggested:

/ordersByCustomers/{customerid}

Why? Because this is not RESTful in general (even in microservices environment) and make the API difficult to understand and you by the consumers. What if you need orderByWhatever? Will you be introducing new endpoint every single time you need a new set of data? Try to avoid so opinionated endpoints.

What @Augutsto suggested is fully correct. If you're afraid of having a complicated logic in GET request this is the situation where you can break REST rules. I mean introducing:

POST /orders/filter/

Where all the filtering logic will be passed in requests body - so it's easier to carry complicated logic as well.

Upvotes: 1

Augusto
Augusto

Reputation: 29977

You can pass some query parameters as filters (this is the most common way I've seen). For example

/orders?customerId=123

I think that's quite clear, that you want to retrieve all customer orders filtered by customer id. In the same way you can add pagination or other filters.

The important thing to remember is that you want the order resource, so the URL should remain the same. I'm mentioning this, because this has been the most difficult thing for me to change... to think about resources rather than remote calls.

Upvotes: 2

Related Questions