Free Shopping Cart API — Mock REST API for Testing & Development
Stateful shopping carts are complex to mock purely in local storage. The Shopping Cart API provides session-aware endpoints where frontend apps can create carts (POST /api/cart/{sessionId}), add line items, adjust quantities, and calculate subtotal/tax totals automatically with live JSON responses.
Interactive Live API Explorer
/api/cart/{sessionId}
Get cart contents for session. Creates a new cart if sessionId is new or cart expired. Replace {sessionId} with an ID (e.g., sess_123).
Replace placeholders (e.g., {userId}) with actual values in the path above. You can also add/edit query parameters (e.g., ?key=value).
/api/cart/{sessionId}/items
Add an item to the cart. Replace {sessionId} with a session ID.
Replace placeholders (e.g., {userId}) with actual values in the path above.
/api/cart/{sessionId}/items
List all items in the specified cart. Replace {sessionId} with a session ID.
Replace placeholders (e.g., {userId}) with actual values in the path above. You can also add/edit query parameters (e.g., ?key=value).
/api/cart/{sessionId}/items
Update quantity of an item in the cart. Expects itemId and new quantity. Replace {sessionId} with a session ID.
Replace placeholders (e.g., {userId}) with actual values in the path above.
/api/cart/{sessionId}/items
Remove an item from the cart. Expects itemId. Replace {sessionId} with a session ID.
Replace placeholders (e.g., {userId}) with actual values in the path above.
/api/cart/{sessionId}
Update cart metadata, e.g., apply a coupon code. Replace {sessionId} with a session ID.
Replace placeholders (e.g., {userId}) with actual values in the path above.
/api/cart/{sessionId}
Clear all items from the cart (cart session persists but is empty). Replace {sessionId} with a session ID.
Replace placeholders (e.g., {userId}) with actual values in the path above.
Code Integration Examples
// Free Shopping Cart API - JavaScript Fetch Example
async function fetchShoppingCartAPI() {
try {
const response = await fetch('https://gen-endpoint.com/api/cart/{sessionId}', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log('Success:', data);
return data;
} catch (error) {
console.error('Fetch error:', error);
}
}
fetchShoppingCartAPI();Sample JSON Response
{
"id": "sess_123",
"userId": null,
"items": [
{
"id": "item_abc",
"productId": "prod_123",
"name": "Wireless Headphones",
"priceAtTimeOfAddition": 99.99,
"quantity": 1,
"imageUrl": "https://placehold.co/100x100.png",
"options": {
"color": "black"
},
"subtotal": 99.99
}
],
"subtotal": 99.99,
"tax": 8,
"shipping": 0,
"discount": 0,
"total": 107.99,
"createdAt": "2024-08-20T10:00:00Z",
"updatedAt": "2024-08-20T10:05:00Z",
"expiresAt": "2024-08-21T10:00:00Z"
}Common Developer Use Cases
Testing add-to-cart animations, flyout cart drawers, and badge counters.
Validating quantity increment/decrement debouncing and total price updates.
Testing multi-step checkout transitions with existing cart sessions.
Testing empty cart states and promo code application workflows.
Related Free REST APIs
Product Catalog API
API for browsing and managing a product catalog.
Payment Processing API
Handle payment flows, refunds, and transaction history with multiple payment methods.
Inventory Management API
Track stock levels, handle reservations, manage warehouses, and process stock adjustments.
Authentication API
Simplified API for user authentication processes like login, logout, and status check.
Frequently Asked Questions
How is the cart session identified?
Pass any unique string in the path, e.g., /api/cart/user-session-123. The API will maintain that session's cart items.
Does the cart calculate order totals automatically?
Yes, the response calculates subtotal, tax, and grand total based on the line item prices and quantities.
Can I remove items or clear the entire cart?
Yes, call DELETE /api/cart/{sessionId}/items to remove a specific item or DELETE /api/cart/{sessionId} to clear the entire cart.