Intermediate 8 min

API resources and status codes

An API gives callers a predictable way to request or change information.

How it works

Write down the expected response before writing the handler. List a successful lookup, a missing ID, and an invalid request separately. This gives the caller a predictable way to distinguish a result from an error it can correct.

Walk through an example

A resource path such as /books/42 identifies an item. A GET can return its representation or a 404 if it does not exist. Creating a resource can return 201.

What to watch for

Use a consistent error shape. A status code explains the category of the result, and the response body can explain the next step.

Check your understanding

Which status fits a missing resource?

See the example and explanation
Worked example
Given A client requests a well-formed ID that is not present.
Answer 404

The request can be understood, but the requested resource does not exist.

Open practice problem
Next lessonCaching and stale data