Course outline · 0% complete

0/29 lessons0%

Course overview →

Resources and Verbs

lesson 4-1 · ~10 min · 11/29

Quiz

Warm-up from lesson 3-2. In the pattern /users/:id, what does :id do?

REST in one paragraph

REST is a set of conventions for shaping an API around resources: the nouns of your system (users, orders, posts). Each resource collection gets a URL, and the HTTP method supplies the verb. You never invent action names like /createUser, the method already says it.

IntentMethod + pathSuccess code
list usersGET /users200
read oneGET /users/42200
createPOST /users201
replace / updatePUT /users/42 or PATCH /users/42200
deleteDELETE /users/42204

Five operations, two URL shapes (/users and /users/:id). Every resource in your API repeats this exact grid, which is why developers can guess a well-designed REST API without reading its docs.

Code exercise · javascript

Your turn. Complete endpoint(action, resource, id) so it produces the conventional method and path for each action. "list" and "create" are done, add "get", "update" (use PUT), and "remove".

/v1/users/42/posts?status=draftversioncollectionidsub-collectionquery string: filters, sorting, paging
Anatomy of a REST URL: nouns in the path identify things, the query string refines the question.

URL design rules

The conventions that make an API guessable:

  • Plural nouns: /users, not /user or /userList.
  • No verbs in paths: the method is the verb. POST /users beats /createUser.
  • Nest for ownership: /users/7/posts reads as "the posts of user 7". Keep nesting shallow (one level is usually plenty).
  • Query string for options: filtering, sorting, and paging are refinements of a GET, so they go after ?: /posts?status=draft&sort=newest.
  • Lowercase, hyphens if needed: /blog-posts, never /BlogPosts.

An API that follows these reads like a sentence: GET /users/7/posts?status=draft is "get user 7's draft posts".

Problem

A teammate designed the endpoint GET /getUserPosts?user=7. Redesign the path portion in proper REST style (nested resource, no verbs, no query string needed). Answer with just the path, starting with /.