In API design, if I request a list of objects by ID, and one of those IDs is invalid/deleted, should the request fail

apiapi-design

Let's say I'm requesting a list of IDs for which I'd like to receive relevant DB entities (e.g., my-restaurant-api.com?restaurants=1,2,3).

In this example, let's say ID 3 has been deleted and should not be returned.

Should the API response:

  1. return a 404
  2. return a 200 and include the entities that exist
  3. return a 404 and include entities that exist
  4. return a 200 and include an exception message and entities that exist

Or, in this case, should it be up to the consumer to determine what's best for the application? Is there a right answer?

Best Answer

Return 200 OK and (optionally) include some status information in your JSON indicating that some of the entities no longer exist.

If you return no entities, you force your user to create a new request that excludes the missing entities. Just note in your documentation that your endpoint will not return entities that no longer exist, which is the most intuitive behavior anyway.