Search code examples
restlanguage-agnosticapi-design

REST API response should return only id or full object?


At work every time I deal with creating CRUD api, I always return the full object for create and update.

Now I'm developing a personal project and I was wondering if it can be a good idea instead to return only id.

This means that if frontend need the full data, will call the exposed API findById.


Solution

  • Not returning this data is totally fine. You should at a minimum return a Location header pointing to the newly created resource with a 201 Created status

    Returning a full object can remove one roundtrip. In those cases you should return a Content-Location header. You can also let the client decide if they want it with a Prefer: return=representation header.

    General design advice is stop thinking about ids in routes. The entire URL is already unique id.