Depends on what you want to build, and more importantly how you want to build it. What are the use cases and business rules?
API design: returning a sub-set of data (with example)
Getting straight to the point - I use a banking app which allows me to send money to other people's accounts. The process involves entering their account number and sort code which is then checked and returns the account 'name' or the account holder's name which helps me confirm I have got the detail correct.
This is really useful since it makes me feel sure I'm sending money to the right account but it got me thinking about the process behind the account lookup.
If there was a publicly available API for such a process, how would it work?
Presumably there would be an endpoint to get an account having provided an account number and sort code in order to provide the account name to confirm the details. However, a GET request on an account item could also be expected to return a lot more information about an account, most of which may be private to the account owner.
So would there be a best practice to deal with this an maintain a RESTful design?
Solutions (and their caveats) that spring to mind are:
-
Create a separate endpoint to return only the account name for confirmation e.g.
/accounts/lookup/{id}. Does that break REST and if so, should I be bothered? This approach also means if the request ID was an incrementing number i.e./accounts/344users could guess and therefore get the names of all accounts if they wanted so the ID would probably need to be more obscure. I can imagine scenarios where users wouldn't want the existence of their accounts to be known. I guess the bottom line is you'd need to expose at least some information in order to receive any money at all. -
Check the permissions of the user making the request to a 'standard' endpoint (
/accounts/{id}) and if they are not the owner, only provide the data required to perform the action. Would this approach require sub-resources to handle the data being returned and would this also break REST since I understand endpoints should return a consistent data model.
I hope that makes sense.
Thanks.
Please or to participate in this conversation.