Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

Turn any feature into a verb on a noun you could list. "Water plant 7" sounds like a verb, but a watering is a thing with a date, and you could list a plant's waterings. So watering means creating a watering: POST /api/plants/7/waterings. For the response, first decide whose fault it is (2, 4 or 5), then pick the exact code.

Designing "Water now"Step 1 of 4
  1. Find the noun

    The event is a watering. It has a date and could be listed as a history, so it is a resource.

Drills

Answer fast — trust the rule.

quick drill

Which route follows REST style for deleting plant 7?

quick drill

Why must a GET request never change data?

quick drill

Someone registers with an email that already has an account. Which status fits best?

quick drill

A response comes back 503. Whose side is the problem on?

Remember

  • Nouns in the path, verbs in the method, the outcome in the status.
  • Shortcut: make the feature a verb on a listable noun; for status, decide whose fault first.
  • Trap: verbs in paths and GET routes that change data.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainRedesign the notes API

Sprout's old notes API has POST /api/addNote, GET /api/notesForPlant?plant=7 and POST /api/removeNote?id=3. Redesign each in REST style, and give the status code each returns on success and one it might return on failure.