Skip to lesson

Nested resources: /customers/{id}/orders

A customer's app needs their saved addresses and their order history. With no URL rule, one team writes /getAddresses?cid=1 and another writes /orders/byCustomer/1.

Child resources that belong to a parent

A nested resource is a collection that lives under one parent item: /customers/1/addresses means "the addresses of customer 1". The URL shows the relationship, like a foreign key does in the database.

List the addresses of customer 1, then add an address for customer 8.

Example 1 · List and add a customer's addresses

Text
GET /api/v1/customers/1/addresses HTTP/1.1Host: localhost:8080POST /api/v1/customers/8/addresses HTTP/1.1Host: localhost:8080Content-Type: application/json{  "label": "Home",  "line1": "4 Residency Road",  "city": "Bengaluru",  "state": "Karnataka",  "pincode": "560025",  "isDefault": true}
Responses
HTTP/1.1 200 OK
Content-Type: application/json

[
  { "id": 1, "customerId": 1, "label": "Home", "line1": "12 MG Road, Indiranagar", "city": "Bengaluru", "state": "Karnataka", "pincode": "560038", "isDefault": true },
  { "id": 2, "customerId": 1, "label": "Work", "line1": "Prestige Tech Park, Marathahalli", "city": "Bengaluru", "state": "Karnataka", "pincode": "560103", "isDefault": false }
]

HTTP/1.1 201 Created
Location: http://localhost:8080/api/v1/customers/8/addresses/10
Content-Type: application/json

{
  "id": 10,
  "customerId": 8,
  "label": "Home",
  "line1": "4 Residency Road",
  "city": "Bengaluru",
  "state": "Karnataka",
  "pincode": "560025",
  "isDefault": true
}

What this code does

  • Asha (customer 1) has two saved addresses, returned as a plain array: a customer has only a few, so there is no paging.
  • The POST body has no customerId: it comes from the URL. The response shows "customerId": 8 and a Location one level under the customer.

A child resource only makes sense together with its parent. A ShopLite address belongs to exactly one customer (the customer_id foreign key in addresses), so the parent id in the path does real work: the client does not send customerId in the body, and the server takes it from the URL. A client cannot add an address to the wrong customer by mistake.

An item of the child collection can have its own URL too: /customers/1/addresses/2. The server must check that address 2 really belongs to customer 1; if it belongs to someone else, the answer is 404.

/customers/{id}/orders versus /orders?customerId=

A query filter is a query parameter that narrows a whole collection with conditions: GET /orders?status=PLACED searches all orders.

Customer 2's order history, read through a nested URL.

Example 2 · One customer's orders, newest first

Text
GET /api/v1/customers/2/orders HTTP/1.1Host: localhost:8080
Response
HTTP/1.1 200 OK
Content-Type: application/json

{
  "content": [
    { "id": 14, "customerId": 2, "status": "SHIPPED", "orderedAt": "2026-09-21T21:05:00+05:30", "total": 19298.00 },
    { "id": 7, "customerId": 2, "status": "DELIVERED", "orderedAt": "2026-08-09T11:00:00+05:30", "total": 1698.00 },
    { "id": 2, "customerId": 2, "status": "DELIVERED", "orderedAt": "2026-07-05T19:45:00+05:30", "total": 1947.00 }
  ],
  "page": 0,
  "size": 20,
  "totalElements": 3,
  "totalPages": 1
}

What this code does

  • Ravi Kumar (customer 2) has three orders; the list shows a summary of each (no items) with the computed total, newest first.
  • To see the items of order 14, the client follows its own URL /orders/14, not a longer nested one.

A nested URL answers "the children of this one parent"; a query filter answers "search the whole collection with these conditions". GET /customers/2/orders is Ravi's order history. Use nesting when the parent is the natural way in, such as a screen for one customer's orders. Use a filter when the parent is one optional condition among several, such as status, date or city.

ShopLite has both, without overlap: /customers/{id}/orders for one customer's history and /orders?status= for the shop's admin list. An order itself still has one home, /orders/{id}, whichever list it was found in.

The content, page and totalElements wrapper around order lists is the page envelope, covered in the next lesson.

Keep nesting to one level

One level of nesting means a URL has at most one parent: /customers/2/orders is fine, /customers/2/orders/14/items/1 is not.

Five deep URLs, flattened to at most one level.

The ShopLite URL tree. A customer at /customers/{id} has two children, addresses and orders, one level below. An order has its own top-level home at /orders/{id}, and its items at /orders/{id}/items are again one level below. No URL needs more than one parent id.

Example 3 · Deep URLs flattened to one level

Text
GET  /customers/2/orders/14GET  /customers/2/orders/14/itemsPOST /customers/2/orders/14/cancelGET  /categories/1/categories/2/productsGET  /customers/1/addresses/2/orders
One level
GET  /orders/14
GET  /orders/14/items
POST /orders/14/cancel
GET  /products?categoryId=2
GET  /orders?addressId=2   (not in ShopLite v1)

What this code does

  • Order 14 has a unique id, so it gets the top-level home /orders/14, and everything about it hangs from there.
  • Products in a category are a search over all products, so a filter fits better than a chain of parents. Each fixed URL needs at most one id.

Deep URLs are long, and every client must know the whole chain of ids just to reach one thing.

Once a child has its own id that is unique on its own, give it a top-level item URL. Order 14 is /orders/14, not /customers/2/orders/14. Then /orders/14/items is again only one level deep.

404 for a missing parent, empty list for no children

A missing parent is a nested URL whose parent does not exist, such as customer 99 in /customers/99/orders. The answer is 404. A parent that exists but has no children gives 200 with an empty list.

A customer that does not exist, then one that exists but has no orders.

Example 4 · Missing parent versus no children

Text
GET /api/v1/customers/99/orders HTTP/1.1Host: localhost:8080GET /api/v1/customers/8/orders HTTP/1.1Host: localhost:8080
Responses
HTTP/1.1 404 Not Found
Content-Type: application/problem+json

{
  "type": "https://shoplite.example/problems/not-found",
  "title": "Resource not found",
  "status": 404,
  "detail": "Customer 99 was not found.",
  "instance": "/api/v1/customers/99/orders"
}

HTTP/1.1 200 OK
Content-Type: application/json

{
  "content": [],
  "page": 0,
  "size": 20,
  "totalElements": 0,
  "totalPages": 0
}

What this code does

  • There is no customer 99, so the nested URL names nothing: 404, and the detail says which parent is missing.
  • Imran Khan (customer 8) exists but has not ordered yet: 200 with an empty content list and totalElements 0.

Mixing the two up causes real bugs. If both returned an empty list, a typo in the customer id would look like "this customer never ordered", and nobody would notice the wrong id.

A filter is different: GET /orders?status=SHIPPED with no matches is a normal empty 200, because a filter only narrows the list.

Order items: /orders/{id}/items

An order item is one line of an order: a product, a quantity and the price paid. It has no life outside its order, so its only URL is under the order: GET /orders/{id}/items.

Read the lines of order 16.

Example 5 · The items of order 16

Text
GET /api/v1/orders/16/items HTTP/1.1Host: localhost:8080
Response
HTTP/1.1 200 OK
Content-Type: application/json

[
  { "productId": 13, "sku": "FSH-SHOE", "productName": "Running Shoes", "quantity": 1, "unitPrice": 2499.00, "lineTotal": 2499.00 },
  { "productId": 11, "sku": "BK-ATOMIC", "productName": "Atomic Habits", "quantity": 3, "unitPrice": 399.00, "lineTotal": 1197.00 }
]

What this code does

  • Each line carries unitPrice (the price on the order day) and the computed lineTotal: 3 × 399.00 = 1197.00.
  • There is no item id in the JSON: a line is identified by its order and product (the schema has a unique key on both), and lines are never edited one by one.

An item is created with the order and deleted with it (ON DELETE CASCADE in the schema). ShopLite keeps items read-only after the order is placed: there is no POST, PUT or DELETE on /orders/{id}/items.

Items also appear inside GET /orders/{id}; the separate URL is for clients that need only the lines. The list is not paged, because one order has at most 20 items.

Next step

Check your understanding

11 quick questions