Skip to the workspace
Pebellslearn by building
Designing RESTful APIs  ›  Capstone Project

CRUD Operations

CRUD Operations 14 minCreate, read, update and delete map onto POST to the collection and GET, PATCH or PUT, and DELETE on the item, each with a predictable success status.

api design / CRUD Operations

Explore each scene at your own pace.

01 · The rule

The rule

CRUD Operations

Almost every resource in BookNook needs the same four abilities: add one, look at it, change it, remove it. That is CRUD: create, read, update, delete. Once you map these four onto HTTP the same way everywhere, a developer who has used /books can use /members and /fines without reading anything new.

The rule

Create is POST to the collection; the rest act on the item.

The standard mapping, with the responses a caller should expect:

Operation Request Success response
Create POST /v1/books with the new book 201 Created, Location: /v1/books/43, the new book
Read many GET /v1/books 200 OK with a list
Read one GET /v1/books/43 200 OK with the book, or 404
Update part PATCH /v1/books/43 with changed fields 200 OK with the updated book
Replace PUT /v1/books/43 with the full book 200 OK (or 204)
Delete DELETE /v1/books/43 204 No Content

A create request and its answer:

POST /v1/books HTTP/1.1
Content-Type: application/json

{"title": "Dune", "isbn": "9780441013593", "shelf": "C2"}

HTTP/1.1 201 Created
Location: /v1/books/43
Content-Type: application/json

{"id": 43, "title": "Dune", "isbn": "9780441013593", "shelf": "C2", "available": true}

Notice the server chose the id and filled in available. Returning the full resource saves the caller a second GET.

Details that separate a careful API from a sloppy one:

  • Validate on create and update: missing required fields → 422 with a message saying which field.
  • Read-only fields such as id and created_at are set by the server; ignore or reject attempts to change them.
  • Deleting what is not there: many APIs answer 404; others answer 204 because the end state, "gone", is already true. Pick one and document it.
  • Soft delete: a library may never truly erase a loan, for records. DELETE can mark it removed, as long as it then behaves as gone to callers.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.