Skip to main content
POST
/
api
/
cart
Add to Cart
curl --request POST \
  --url https://api.example.com/api/cart \
  --header 'Authorization: <authorization>' \
  --header 'Content-Type: <content-type>' \
  --header 'x-api-key: <x-api-key>' \
  --data '
{
  "productId": 123,
  "quantity": 123,
  "variant": "<string>"
}
'
{
  "success": true,
  "message": "<string>"
}

Authentication

This endpoint requires JWT authentication.

Required Headers

x-api-key
string
required
Your API key for authentication
Authorization
string
required
Bearer token for user authentication. Format: Bearer {token}
Content-Type
string
required
Must be application/json

Request Body

productId
integer
required
The ID of the product to add to cart
quantity
integer
required
The quantity of the product to add. Must be a positive integer.
variant
string
The product variant (e.g., color, size). Required if the product has variants available.

Response

success
boolean
Indicates if the request was successful
message
string
Response message indicating whether the product was added or quantity was updated

Example Request

curl -X POST https://api.example.com/api/cart \
  -H "x-api-key: your_api_key" \
  -H "Authorization: Bearer your_jwt_token" \
  -H "Content-Type: application/json" \
  -d '{
    "productId": 123,
    "quantity": 2,
    "variant": "Black"
  }'

Example Response

{
  "success": true,
  "message": "Product added to cart successfully"
}

Behavior Notes

If the same product with the same variant already exists in the cart, the quantity will be updated to the new value (not incremented). The response will return a 200 status code with the message “Product quantity updated in cart successfully”.
For products that have variants, the variant field is required. The variant value must match one of the available variants for the product, otherwise a 400 error will be returned.

Error Codes

Status CodeDescription
200Product quantity updated successfully
201Product added to cart successfully
400Missing required fields or invalid variant
401Missing or invalid authentication credentials
404Product not found
500Internal server error

Build docs developers (and LLMs) love