Create Cart
Create a new empty cart. Returns a unique cartId which you’ll use in all subsequent cart operations.
HTTP Method & Endpoint
POST | /cart
Request
Request Body Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
customerId | String | Yes | - | Unique identifier of the customer. This is available in the customer object (specifically the id field) returned upon successful login. |
Example Request
{
"customerId": "customer_123"
}
Response Format
Example Response
{
"message": "Cart Created Successfully",
"data": {
"id": "cart_PhIidALwQ8JFCBOU",
"storeId": "store_Vc3PvYOQrna9GOF7",
"customerId": "customer_123",
"totalAmount": 0
},
"source": "db"
}
Success Response (200 OK)
| Field | Type | Description |
|---|---|---|
message | String | Status message indicating the result of the cart creation |
data | Cart | Contains all details about the cart |
source | String | Source of the order data (e.g., "db") |
Cart Object Properties
| Field | Type | Description |
|---|---|---|
id | String | Unique identifier for the cart |
storeId | String | Store ID for which the cart was created |
customerId | String | Customer's unique identifier |
totalAmount | Number | Total amount payable for the items in the cart |
Examples
Javascript (React)
import axios from "axios";
import { api } from "../constant";
const createCart = async () => {
try {
const customerId = localStorage.get("customerId");
const response = await api.post("/cart", {
customerId: customerId,
});
console.log("Cart Created Successfully:", response.data);
const data = response.data.data;
const cartId = data.cartId;
return cartId;
} catch (error) {
console.error("Error creating cart:", error.response?.data || error.message);
}
};
Error Responses
| Status Code | Description |
|---|---|
| 400 | Bad Request - Invalid customer ID format |
| 401 | Unauthorized - Authentication token is missing or invalid |
| 403 | Forbidden - Access to carts not allowed |
| 404 | Not Found - No carts found for the specified customer |
| 500 | Internal Server Error - Something went wrong on the server |