Skip to main content
POST
/
cart
/
add
{
  "productId": 42,
  "quantity": 2
}
{
  "id": 10,
  "cartId": 1,
  "productId": 42,
  "quantity": 2
}

Authentication

This endpoint requires authentication. Include a valid JWT token in the Authorization header.
Authorization: Bearer <token>

Request Body

productId
integer
required
The ID of the product to add to the cart
quantity
integer
required
The quantity of the product to add. Must be at least 1.
{
  "productId": 42,
  "quantity": 2
}

Response

Returns the created or updated cart item.
id
integer
required
Unique identifier for the cart item
cartId
integer
required
ID of the cart this item belongs to
productId
integer
required
ID of the product that was added
quantity
integer
required
Total quantity of the product in the cart
{
  "id": 10,
  "cartId": 1,
  "productId": 42,
  "quantity": 2
}

Behavior

  • If the product is already in the cart, the quantity will be incremented by the specified amount
  • If the cart doesn’t exist yet, it will be created automatically
  • The operation uses an atomic upsert to prevent race conditions

Error Responses

Product Not Found (404)

{
  "message": "Producto no encontrado"
}

Insufficient Stock (400)

{
  "message": "Stock insuficiente"
}

Validation Error (400)

{
  "errors": [
    {
      "field": "quantity",
      "message": "La cantidad debe ser al menos 1"
    }
  ]
}

Unauthorized (401)

{
  "message": "Usuario no autenticado"
}

Build docs developers (and LLMs) love