Navrattan Yadav
Navrattan Yadav

Reputation: 2113

Rest API Design for Order Management System

I am working on a project which is using micro-services architecture.

There is two services:

  1. UserAPI : all thing related to user come here.
  2. OMS : All things related to Order Come here.

I need to provide orders based on following filter :

  1. By User Id
  2. By Date
  3. By Status
  4. By User Phone Number
  5. mix of above

So i create an API

path/orders?date=12/11/2016&status=delivered&phone=1111111111

Now i need to provide Orders for User by user ID. So which is good rest design:

  1. Add user ID in query param in existing API like path/orders?user_id=1
  2. Create a Separate API path user/{user_id}/orders

Upvotes: 1

Views: 1391

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209112

Both of your options are OK. But there are different semantics.

path/orders?user_id=1

This is looking up by orders. Orders maybe looked up for example to do some statistical analysis. The orders can be filtered by different parameters, the user id being one of them. For this (when the orders is the main interest) the above URI strategy is fine.

Now on the other hand you may want to look up a user and see their orders. Maybe to do some analysis on their ordering habits. Here you want user information along with their orders. This is where your second URI scheme would be better

user/{user_id}/orders

These are the orders that belong to the user. So it is a relationship. This is where this URI scheme works better.

So really there's nothing wrong with having both options. You just need to follow the semantics of when each should be used.

Upvotes: 1

Related Questions