Skip to content

Cart

Manage the authenticated customer's shopping cart, update item quantities, and calculate cart checkout totals.


Authentication

All Cart endpoints require Bearer JWT access token authentication for a registered CUSTOMER account:

Authorization: Bearer <your_access_token>

Cart Endpoints Overview

Method Endpoint Description Auth Required
POST /api/v1/carts Add product or variant to cart Bearer JWT
GET /api/v1/carts Retrieve cart items (Paginated) Bearer JWT
GET /api/v1/carts/summary Retrieve cart summary with estimated tax & subtotal Bearer JWT
PUT /api/v1/carts/update Update product quantity in cart (0 removes item) Bearer JWT
DELETE /api/v1/carts/remove Remove specific product from cart Bearer JWT
DELETE /api/v1/carts/all Clear entire shopping cart Bearer JWT

Endpoint Details

1. Add Product to Cart

POST /api/v1/carts Bearer Token Required

Adds an item line to the authenticated user's cart. If the product has variants (e.g. Size, Color), variantId must be supplied.

Request Body Parameters

Parameter Type Required Description
productId string Required UUID of the product to add
quantity integer Required Quantity to add (must be positive, e.g. >= 1)
variantId string Conditional UUID of the selected variant. Mandatory if the product has variants.

Request Examples

curl -X POST 'http://localhost:8080/api/v1/carts' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9...' \
  -d '{
    "productId": "5b24d429-c981-47c8-9318-f4d61dd2c1a4",
    "quantity": 1
  }'
val response = client.post("http://localhost:8080/api/v1/carts") {
    bearerAuth(accessToken)
    contentType(ContentType.Application.Json)
    setBody(CartRequest(productId = "5b24d429-c981-47c8-9318-f4d61dd2c1a4", quantity = 1))
}
const res = await fetch("http://localhost:8080/api/v1/carts", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${accessToken}`
  },
  body: JSON.stringify({
    productId: "5b24d429-c981-47c8-9318-f4d61dd2c1a4",
    quantity: 1
  })
});

Response

{
  "productId": "5b24d429-c981-47c8-9318-f4d61dd2c1a4",
  "quantity": 1
}
{
  "message": "variantId is required for products with variants"
}

2. Get Cart Items

GET /api/v1/carts Bearer Token Required

Retrieves all line items currently saved in the customer's shopping cart.

Query Parameters

Parameter Type Required Description
page / offset integer Optional Page number (default: 1) or offset (default: 0)
perPage / limit integer Optional Items per page (default: 10, max: 100)

Request Examples

curl -X GET 'http://localhost:8080/api/v1/carts?page=1&perPage=10' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9...'

Response

[
  {
    "productId": "71b26dd9-b4b5-4f87-a84d-c8daa506018a",
    "quantity": 3,
    "product": {
      "id": "71b26dd9-b4b5-4f87-a84d-c8daa506018a",
      "categoryId": "58f5c085-d04a-47de-beab-1d476b6ce432",
      "productName": "Smart Watch",
      "productCode": "SW-001",
      "productQuantity": 5,
      "productDetail": "Xiaomi Smart Watch",
      "price": 10.0,
      "discountPrice": 0.0,
      "status": "ACTIVE"
    }
  }
]

3. Update Cart Item Quantity

PUT /api/v1/carts/update Bearer Token Required

Sets the absolute quantity of an item in the cart. Setting quantity: 0 automatically removes the item line.

Request Body Parameters

Parameter Type Required Description
productId string Required UUID of the product
quantity integer Required New absolute quantity (>= 0)
variantId string Optional Variant identifier if updating a variant line

Request Examples

curl -X PUT 'http://localhost:8080/api/v1/carts/update' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9...' \
  -d '{
    "productId": "5b24d429-c981-47c8-9318-f4d61dd2c1a4",
    "quantity": 2
  }'
val response = client.put("http://localhost:8080/api/v1/carts/update") {
    bearerAuth(accessToken)
    contentType(ContentType.Application.Json)
    setBody(CartRequest(productId = "5b24d429-c981-47c8-9318-f4d61dd2c1a4", quantity = 2))
}

Response

{
  "productId": "5b24d429-c981-47c8-9318-f4d61dd2c1a4",
  "quantity": 2,
  "product": {
    "id": "5b24d429-c981-47c8-9318-f4d61dd2c1a4",
    "productName": "Polo T-Shirt",
    "price": 100.0,
    "discountPrice": 0.0
  }
}

4. Remove Product from Cart

DELETE /api/v1/carts/remove Bearer Token Required

Removes an item line completely from the customer's cart.

Query Parameters

Parameter Type Required Description
productId string Required UUID of the product to remove
variantId string Optional UUID of the variant line to remove

Request Examples

curl -X DELETE 'http://localhost:8080/api/v1/carts/remove?productId=71b26dd9-b4b5-4f87-a84d-c8daa506018a' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9...'

Response

{
  "id": "71b26dd9-b4b5-4f87-a84d-c8daa506018a",
  "productName": "Smart Watch"
}

5. Clear Cart

DELETE /api/v1/carts/all Bearer Token Required

Empties all lines from the customer's shopping cart.

Request Examples

curl -X DELETE 'http://localhost:8080/api/v1/carts/all' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9...'

Response

{
  "message": "Cart cleared successfully"
}

6. Get Cart Summary

GET /api/v1/carts/summary Bearer Token Required

Calculates variant-aware subtotals, item counts, available stock, and estimated taxation.

Request Examples

curl -X GET 'http://localhost:8080/api/v1/carts/summary' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9...'

Response

{
  "items": [
    {
      "productId": "71b26dd9-b4b5-4f87-a84d-c8daa506018a",
      "productName": "Smart Watch",
      "price": 10.0,
      "quantity": 3,
      "image": "https://example.com/image1.jpg",
      "stockQuantity": 50,
      "shopId": "shop-uuid-1",
      "shopName": "Electronics Shop"
    }
  ],
  "subtotal": 30.0,
  "estimatedTax": 3.0,
  "itemCount": 1
}

Error Handling

Standard Error Responses

For details on error schemas, validation failure formats, and HTTP status codes (400, 401, 404), see the Error Handling & Status Codes Guide.