Authentication
This endpoint requires authentication. Include a valid JWT token in the Authorization header.
Authorization: Bearer <token>
Request Body
The ID of the product to add to the cart
The quantity of the product to add. Must be at least 1.
{
"productId": 42,
"quantity": 2
}
Response
Returns the created or updated cart item.
Unique identifier for the cart item
ID of the cart this item belongs to
ID of the product that was added
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"
}