Skip to main content
POST
/
api
/
cart
/
sync
Add Item to Cart
curl --request POST \
  --url https://api.example.com/api/cart/sync \
  --header 'Content-Type: application/json' \
  --data '
{
  "userId": 123,
  "items": [
    {
      "id": "<string>",
      "quantity": 123
    }
  ]
}
'
{
  "success": true,
  "data": {
    "id": 123,
    "userId": 123,
    "items": [
      {
        "id": 123,
        "productoId": 123,
        "quantity": 123,
        "producto": {}
      }
    ]
  },
  "error": "<string>"
}

Overview

Adds a new product to the user’s shopping cart. This endpoint uses the cart synchronization mechanism to add items while preserving existing cart contents.

Authentication

This endpoint requires authentication. Ensure the user is authorized to modify the cart.

Request Body

userId
number
required
The unique identifier of the user whose cart you want to modify
items
array
required
Array of cart items to sync. To add a new item, include all existing items plus the new one.
id
string
required
The product ID to add to the cart (as a string)
quantity
number
required
The quantity of the product to add (must be greater than 0)

Response

success
boolean
required
Indicates whether the request was successful
data
object
The complete updated cart object
id
number
Unique identifier for the cart
userId
number
The ID of the user who owns this cart
items
array
Array of all cart items including the newly added item
id
number
Unique identifier for the cart item
productoId
number
The product ID for this cart item
quantity
number
Quantity of the product in the cart
producto
object
Complete product details
error
string
Error message if the request fails

Example Request

Adding First Item

curl -X POST "https://api.pcfix.com/api/cart/sync" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "userId": 123,
    "items": [
      {
        "id": "789",
        "quantity": 2
      }
    ]
  }'

Adding Item to Existing Cart

curl -X POST "https://api.pcfix.com/api/cart/sync" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "userId": 123,
    "items": [
      {
        "id": "789",
        "quantity": 2
      },
      {
        "id": "456",
        "quantity": 1
      }
    ]
  }'

Example Response

Success Response

{
  "success": true,
  "data": {
    "id": 1,
    "userId": 123,
    "abandonedEmailSent": false,
    "items": [
      {
        "id": 45,
        "cartId": 1,
        "productoId": 789,
        "quantity": 2,
        "producto": {
          "id": 789,
          "name": "Gaming Mouse",
          "price": 49.99,
          "description": "High-performance gaming mouse"
        }
      },
      {
        "id": 46,
        "cartId": 1,
        "productoId": 456,
        "quantity": 1,
        "producto": {
          "id": 456,
          "name": "Mechanical Keyboard",
          "price": 129.99,
          "description": "RGB mechanical keyboard"
        }
      }
    ]
  }
}

Error Response

{
  "success": false,
  "error": "User ID required"
}

Error Codes

Status CodeDescription
200Item added successfully
400Invalid user ID or request body
401Unauthorized access
500Server error - Failed to sync cart

Important Notes

The sync endpoint replaces ALL cart items. To add a new item, you must include all existing items in the request along with the new item.
  • Product IDs must be valid and reference non-deleted products
  • Invalid product IDs are automatically filtered out
  • The abandonedEmailSent flag is reset to false when items are added
  • If an empty items array is provided, the cart will be cleared
  • Product ID should be sent as a string in the request body

Build docs developers (and LLMs) love