Track 06 · Lesson 2 of 8APIs and servers

Track 06 · APIs and servers

REST and resources

Lesson 2 of 8· 6 min

REST organises an API around things, not actions. URLs name resources (orders, customers); HTTP methods say what to do with them. Learn the pattern once and you can guess most endpoints of an API you’ve never seen.

Design the endpoints

Pick a method and a URL for each task

  1. 1.List every order

  2. 2.Create a new order

  3. 3.Look at order 41

  4. 4.Change only the size of order 41

  5. 5.Cancel order 41

  6. 6.List the orders of customer 7

0 of 6 spot on

The pattern#

  • List orders: GET /orders
  • Create one: POST /orders
  • Read one: GET /orders/41
  • Replace one: PUT /orders/41
  • Change part of one: PATCH /orders/41
  • Delete one: DELETE /orders/41

Collections are plural nouns; one item is the collection plus its id; related things nest (/customers/7/orders) or filter (/orders?customer=7).

Verbs in URLs are a smell#

/getOrders, /createOrder and /orders/41/delete duplicate what the method already says, and they break the safety rules browsers and caches rely on. A GET must never change anything: crawlers, prefetchers and caches send them freely.

When REST bends#

Some actions aren’t create, read, update or delete: “cancel”, “refund”, “resend”. It’s fine to model them as creating something (POST /orders/41/cancellation) when the record must stay in history. REST is a set of conventions, not a law; consistency matters more than purity.

Check yourself

Which endpoint best changes only the delivery address of order 41?