Quick answer
Build a REST contract around resources the product owns, then define authorization, validation, transitions, and failure behavior for each operation. HTTP method names and neat URLs cannot compensate for missing ownership checks. A useful endpoint lets a client complete a task, interpret errors, and retry safely where the contract permits it.
Start with one resource and a small set of supported operations. For a hypothetical activity application, a session belongs to a user and may transition from recording to complete. Keep this illustrative model separate from WatersportGPS's public feature description: its actual API and schema are not provided by the portfolio.
Practical implementation steps
Define resource boundaries
Name identifiers, owner or tenant, editable fields, immutable fields, and allowed transitions. Derive the requester from a verified session; never accept ownerId from a body as authority. Query within the requester's scope rather than loading an arbitrary record and hoping the interface hid other IDs. Treat every endpoint, including list and export operations, as an authorization boundary.
Use method semantics deliberately
GET retrieves without requesting a state change. POST commonly creates or triggers an operation. PUT describes replacement, while PATCH describes a partial modification contract. Idempotence concerns the intended server effect of repeated identical requests, not whether response bodies match. A DELETE can return a different status after the first deletion while keeping the same intended effect.
Make errors actionable
Return a stable error code, a safe explanation, and field details when appropriate. Distinguish invalid input, missing authentication, insufficient permission, unavailable resources, and conflicting state. Decide consistently whether a missing and inaccessible record should share a response to avoid revealing existence. Do not return stack traces, database errors, tokens, or confidential values.
Specify list and retry behavior
Bound page size, choose stable ordering with a unique tie-breaker, and encode a cursor that preserves the filter contract. Offset pagination can be adequate for small stable lists; changing datasets often need cursors. For non-idempotent creation, define whether an idempotency key is supported and bind it to the requester and request payload. A header alone does not prevent duplicates without atomic server storage.
Implementation example
This TypeScript example describes a response contract, not a complete server. It gives clients explicit success and error branches. The implementation still needs runtime validation, authentication, scoped database queries, and request-size limits.
type SessionSummary = { id: string; status: "recording" | "complete" };
type ApiError = {
code: "INVALID_INPUT" | "NOT_FOUND" | "CONFLICT";
message: string;
requestId: string;
};
type CreateSessionResponse =
| { ok: true; data: SessionSummary }
| { ok: false; error: ApiError };
function parsePageSize(raw: string | null): number {
const value = raw === null ? 20 : Number(raw);
if (!Number.isInteger(value) || value < 1 || value > 100) {
throw new Error("Invalid page size");
}
return value;
}
Types document the contract but do not validate JSON from a caller. Translate validation failures into the chosen client error envelope and status. Generate request IDs independently, keep internal diagnostics on the server, and ensure the request ID itself contains no sensitive input.
Tradeoffs and alternatives
Resource endpoints are easy to understand when workflows match CRUD. A domain action such as completing a session may deserve its own endpoint when it validates a transition, writes several records, and triggers work. Avoid hiding that operation inside an ambiguous generic patch. Keep behavior stable for existing clients and version only the incompatible contract, rather than every implementation change.
Common mistakes
- Trusting a body-supplied user or tenant ID.
- Returning unbounded lists and arbitrary sorting expressions.
- Retrying POST blindly after a timeout.
- Using HTTP 200 for every failure without a documented contract.
- Breaking existing consumers by renaming fields without migration.
Production checklist
- Every operation checks authentication and scoped ownership.
- Runtime schemas and size limits reject invalid input.
- Methods, statuses, and error codes are documented.
- Pagination has bounded size and stable ordering.
- Retries and concurrent writes have a defined contract.
- Compatibility is checked against current client expectations.
FAQ
Does REST require one URL per database table?
No. The public resource model should fit the client task and access boundary. Database joins and storage details can remain implementation choices. Avoid leaking internal columns simply because serializing the whole record is convenient.
Can clients retry any failed request?
Not without understanding the operation. A lost response may follow a successful server write. Use method semantics and a documented deduplication contract, then apply bounded retries only to the failures that contract permits.
Conclusion
Review an API through one complete client task: successful operation, invalid input, unauthorized access, conflict, and retry. When all five have a coherent contract, route naming and framework choices become easier to assess.
Primary sources
Related guides and examples
Need this implemented in a real product?
One connected implementation across the interface, application logic, APIs, and data layer.