Designing a Task API: HTTP Methods, Errors and Validation
Build a small task service with predictable endpoints, correct response codes and validation before adding a database.
Key Learning Objectives
- Match CRUD operations to HTTP methods.
- Distinguish 400, 401, 403, 404 and 500 responses.
- Validate server-side inputs.
- Make repeated DELETE requests safe.
Model resources, not button clicks
Good resource-oriented endpoints express nouns such as /tasks and /tasks/:id. GET reads without changing server state; POST creates a new resource; PATCH updates part of an existing task; DELETE removes one. A newly created resource commonly returns 201 with a Location header, while a successful deletion can return 204 with no response body. The client should not infer success merely from a 200 if the payload contradicts its expectations. Document both success and failure shapes before writing UI code.
Validate on the server and return specific errors
The browser is not a trusted boundary. A user can send a request without using your form, so the API must reject an empty task title and excessively long values. Use 400 for malformed input, 401 when authentication is missing or invalid, 403 for an authenticated user lacking permission, and 404 when a requested resource is absent. Unexpected server errors are 500; do not leak stack traces, database connection strings or token values in the response.
function validateTitle(input: unknown): string {
if (typeof input !== "string") throw new Error("Title must be text");
const title = input.trim();
if (title.length < 1 || title.length > 120) {
throw new Error("Title must have 1 to 120 characters");
}
return title;
}
The function accepts unknown input and checks type and length before the application writes data.
Understand idempotency and concurrent requests
A request is idempotent when repeating it has the same intended effect on server state. Deleting task 7 twice should not delete some other task; the second response might be 404, but the task remains absent. GET and PUT are expected to be idempotent by HTTP semantics; POST generally is not. The client needs loading and retry behavior, but a retry after a network timeout may duplicate an unprotected POST. Production systems often use idempotency keys for payments and critical creates. Start by writing down what happens if a request arrives twice.
Build It: Task API contract and in-memory implementation
Build GET /tasks, POST /tasks, PATCH /tasks/:id and DELETE /tasks/:id and verify error behavior.
Project Deliverables
- An endpoint specification with sample JSON.
- Validation for title and task identifiers.
- Tests for invalid title, unknown id and duplicate DELETE.
Acceptance Criteria
- Invalid input never creates a task.
- 404 responses do not reveal internal stack traces.
- Repeated deletion cannot remove a different task.
Next Steps & Related Learning
This interactive lesson is part of the Backend Developer curriculum. Continue exploring or try another interactive lesson: