Skip to the workspace
Pebellslearn by building
Auth and the API Contract  ›  Lesson

Resources, Verbs and Status Codes

RESTful APIs 14 minA RESTful API names resources with nouns in the path, uses the HTTP method as the verb, and reports the outcome with a status code whose first digit says whether it worked, the request was wrong, or the server failed.

full stack / Resources, Verbs and Status Codes

Explore each scene at your own pace.

01 · The rule

The rule

Resources, Verbs and Status Codes

The API is the contract between Sprout's two halves. When it follows predictable rules, the person writing the client can guess a route before reading any docs, and one small helper can handle every response. REST is the most widely used set of such rules, and Sprout has been following it quietly since the CRUD lesson.

The rule

Nouns in the path, verbs in the method, the outcome in the status.

Nouns in the path. A path names a thing, called a resource, usually as a plural noun: /api/plants is the collection, /api/plants/7 is one plant, and /api/plants/7/waterings is the waterings that belong to plant 7. Verbs stay out of paths.

Verbs in paths (avoid)        REST style
POST /api/createPlant         POST   /api/plants
GET  /api/getPlant?id=7       GET    /api/plants/7
POST /api/deletePlant?id=7    DELETE /api/plants/7
POST /api/waterPlant?id=7     POST   /api/plants/7/waterings

Verbs in the method. GET reads, POST creates, PATCH changes part, PUT replaces, DELETE removes. GET must never change anything, because browsers, caches and link previews feel free to send GET requests on their own, sometimes more than once.

The outcome in the status. The first digit tells the client whose problem it is before it reads a word of the body:

  • 2xx, it worked: 200 OK, 201 Created, 204 No Content.
  • 4xx, the request was wrong: 400 malformed, 401 not logged in, 403 not allowed, 404 not found, 409 conflicts with existing data, 422 failed validation.
  • 5xx, the server failed: 500 for an unexpected error, 503 for temporarily unavailable.

REST also asks each request to be self-contained. Sprout's server does not remember what your last request was; each one carries its cookie and everything the handler needs.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.