Difficulty: Beginner
How do you design clean REST URLs? What are the rules for naming resources, nesting, and actions that do not fit CRUD?
Let me start with the mental model: a URL is an address for a thing, not a command. If you think of a library, the URL points to a shelf or a book, and the HTTP method tells the librarian what to do with it. That is why we prefer nouns in paths and let GET, POST, PUT, PATCH and DELETE carry the verb.
Use plural nouns for collections: /orders for the collection and /orders/42 for one order. Plural everywhere is more consistent than mixing singular and plural. Use lowercase and hyphens for multi-word segments, such as /delivery-partners, since URLs are case sensitive in the path and underscores can be hidden by link underlining. Avoid file extensions and avoid trailing slashes inconsistently; pick one and redirect the other.
Bad: /getOrder?id=42, /createUser, /deleteOrder/42. Good: GET /orders/42, POST /users, DELETE /orders/42. The bad forms tunnel verbs through URLs, which duplicates what HTTP already gives you and breaks caching and tooling expectations.
Express hierarchy through nesting only when a child cannot exist without its parent: /restaurants/9/menu-items or /orders/42/items. Keep nesting to two levels at most. Deep chains like /users/1/orders/2/items/3/reviews/4 become brittle and hard to document. A good compromise is to expose the child at the top level as well, so /reviews/4 works, and use query filters such as /reviews?order_id=2 for relationships.
Use query strings for filtering, sorting, pagination and searching, not for identity: /orders?status=paid&sort=-created_at. Path parameters identify a specific resource; query parameters shape a collection view.
Now the awkward part: actions that are not clean CRUD. Cancel an order, publish a post, retry a payment. There are three accepted approaches. First, model the state change as a field update: PATCH /orders/42 with status cancelled. This is simple but hides business rules. Second, model the action as a sub-resource, POST /orders/42/cancellation, which creates a cancellation record that can itself hold reason and timestamps. Third, use a controller-style verb segment such as POST /orders/42/cancel. Stripe and Google both use this pragmatic verb form for genuine actions. The interviewer mostly wants to see that you choose deliberately and stay consistent.
Other conventions worth mentioning: do not expose auto-increment integers if enumeration is a concern; use opaque ids or UUIDs. Keep the URL stable when the implementation changes; a URL is a public contract. Do not put secrets like API keys or tokens in the URL because they land in logs and browser history. And version the API in a predictable place such as /v1.
One more edge case: singleton resources. The current user's profile can be /me or /users/me, which is friendlier than forcing the client to know its own id. Search that crosses resource types is often a top level /search endpoint.
If you remember one line for the interview: nouns for resources, HTTP methods for verbs, query params for views, and a deliberate, consistent pattern for the few things that are truly actions.
# Avoid
POST /createOrder
GET /getOrdersByUser?userId=7
POST /deleteOrder/42
# Prefer
POST /v1/orders
GET /v1/orders?user_id=7&status=paid&sort=-created_at
DELETE /v1/orders/42
POST /v1/orders/42/cancel
Resource nouns plus methods for CRUD, and a single explicit action sub-path for cancel.
URL design, resources, nouns, nesting