Tutorial: Creating a Dynamic Shopping Cart
In this tutorial, we move beyond simple variables and use the Mini-DB. This is a persistent internal data store that allows your mocks to "remember" items in a list—perfect for shopping carts, to-do lists, or message feeds.
The Mini-DB turns your mock into a real backend prototype. Instead of hardcoding responses for 0, 1, or 5 items, you'll build logic that dynamically adds items to a collection and retrieves them exactly like a production database.
Step 1: Scenario Setup
- Navigate to the Workflows tab.
- Click Create Scenario and name it
my-store. - Note your base URL:
http://localhost:36666/api/workflow/exec/my-store/
Step 2: Add an Item (POST /cart)
We want to "capture" the body of a POST request and save it into a table called cart_items.
- Method:
POST - Path:
/cart - Effect:
db.push - Table:
cart_items - Value:
{{input.body}}
Response Body:
{ "items_count": "{{db.cart_items.length}}" }
Step 3: List the Cart (GET /cart)
Retrieve everything stored in the cart_items table. Because Mockzilla supports Type Preservation, returning {{db.cart_items}} will automatically become a JSON array.
- Method:
GET - Path:
/cart
Response Body:
{
"cart": {{json db.cart_items}},
"total": {{db.cart_items.length}}
}
Step 4: Logic-Based Discounts
Let's return a discount code only if the user has more than 2 items in their cart.
- Method:
GET - Path:
/cart/discount - Condition:
db.cart_items.lengthgt2 - Response:
{"eligible": true, "code": "LOYALTY_10"}
Step 5: Verify the Journey
1. Add some coffee beans
curl -X POST http://localhost:36666/api/workflow/exec/my-store/cart \
-d '{"id": "p1", "name": "Dark Roast Coffee", "price": 14.99}'
2. See your cart content
curl http://localhost:36666/api/workflow/exec/my-store/cart
