Skip to main content
The Cart API persists each user’s cart server-side inside their MongoDB user document. All three endpoints require authentication — pass the JWT token in the token request header. The cart is stored as a flat map on the user document under the key cartData, where each key is a food item’s _id and the value is its integer quantity:
You do not need to send userId in any request body. The auth middleware automatically decodes it from your JWT token and injects it into the request before the controller runs.

POST /api/cart/add

Adds one unit of a food item to the authenticated user’s cart. If the item is not yet in the cart, it is initialized with a quantity of 1. If it already exists, the quantity is incremented by 1.

Request Headers

string
required
A valid JWT token issued at login. The middleware decodes this to extract userId.

Request Body

string
required
The MongoDB _id of the food item to add (e.g., "6641a3f2e4b0c123456789ab").

Response

boolean
true when the cart was updated successfully.
string
"Added to cart" on success, "Error" on failure.

Example


POST /api/cart/remove

Removes one unit of a food item from the authenticated user’s cart. The quantity is decremented by 1 only if the current quantity is greater than 0. This prevents the quantity from going negative.

Request Headers

string
required
A valid JWT token issued at login.

Request Body

string
required
The MongoDB _id of the food item to decrement.

Response

boolean
true when the cart was updated successfully.
string
"Removed from cart" on success, "Error" on failure.

Example


POST /api/cart/get

Retrieves the full cart map for the authenticated user. The frontend uses this response to re-hydrate the cart state on page load or after a refresh.

Request Headers

string
required
A valid JWT token issued at login.

Request Body

Send an empty JSON object — userId is provided by the auth middleware.

Response

boolean
true when the cart data was fetched successfully.
object
A key-value map of food item IDs to their quantities. Each key is a MongoDB _id string and each value is a non-negative integer representing how many units are in the cart. Items with a quantity of 0 may still appear in the map after being decremented.

Example