Shopping Cart Management

Bazaara stores cart sessions inside the database, enabling shoppers to access their carts across different devices.


1. How Guest Carts Work

  • If the shopper is not logged in, you must track their cart using a unique guest token.
  • When you call GET /cart or POST /cart/items without an Authorization header, the server automatically creates a new cart, generates a guest token, and returns it in the X-Guest-Token response header.
  • Your custom storefront application must inspect the response headers for X-Guest-Token on the first cart call, persist it in the browser's storage, and send it as the X-Guest-Token request header in all subsequent cart calls.

2. Get Cart Details

  • Method: GET
  • Route: /api/v1/stores/:storeId/cart
# Get cart using guest token
curl -X GET https://api.bazaara.store/api/v1/stores/YOUR_STORE_ID/cart \
  -H "X-Guest-Token: GUEST_TOKEN_UUID"
async function fetchCart(storeId: string, guestToken: string | null, accessToken: string | null) {
  const headers: HeadersInit = {};
  if (accessToken) {
    headers['Authorization'] = `Bearer ${accessToken}`;
  } else if (guestToken) {
    headers['X-Guest-Token'] = guestToken;
  }

  const response = await fetch(`https://api.bazaara.store/api/v1/stores/${storeId}/cart`, { headers });
  
  // Persist guest token if generated
  const newGuestToken = response.headers.get('X-Guest-Token');
  if (newGuestToken) {
    localStorage.setItem('guest_token', newGuestToken);
  }
  return response.json();
}
import requests

def get_cart(store_id, guest_token=None, access_token=None):
    url = f"https://api.bazaara.store/api/v1/stores/{store_id}/cart"
    headers = {}
    if access_token:
        headers["Authorization"] = f"Bearer {access_token}"
    elif guest_token:
        headers["X-Guest-Token"] = guest_token
        
    response = requests.get(url, headers=headers)
    return response.json()
async fn get_cart(store_id: &str, guest_token: Option<&str>) -> Result<serde_json::Value, reqwest::Error> {
    let client = reqwest::Client::new();
    let mut req = client.get(&format!("https://api.bazaara.store/api/v1/stores/{}/cart", store_id));
    if let Some(token) = guest_token {
        req = req.header("X-Guest-Token", token);
    }
    let res = req.send().await?.json::<serde_json::Value>().await?;
    Ok(res)
}

3. Add Item to Cart

Adds a product variant item to the cart. If the product with matching variant options already exists, Bazaara increments the quantity.

  • Method: POST
  • Route: /api/v1/stores/:storeId/cart/items
curl -X POST https://api.bazaara.store/api/v1/stores/YOUR_STORE_ID/cart/items \
  -H "Content-Type: application/json" \
  -H "X-Guest-Token: GUEST_TOKEN_UUID" \
  -d '{
    "productId": "PRODUCT_UUID",
    "quantity": 1,
    "variantInfo": { "color": "Matte Black" }
  }'
async function addToCart(storeId: string, guestToken: string, productId: string, qty = 1, variant = {}) {
  const response = await fetch(`https://api.bazaara.store/api/v1/stores/${storeId}/cart/items`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Guest-Token': guestToken
    },
    body: JSON.stringify({ productId, quantity: qty, variantInfo: variant })
  });
  return response.json();
}
import requests

def add_to_cart(store_id, guest_token, product_id, quantity=1, variant=None):
    url = f"https://api.bazaara.store/api/v1/stores/{store_id}/cart/items"
    headers = { "X-Guest-Token": guest_token }
    payload = {
        "productId": product_id,
        "quantity": quantity,
        "variantInfo": variant or {}
    }
    response = requests.post(url, json=payload, headers=headers)
    return response.json()
async fn add_item(store_id: &str, guest_token: &str, product_id: &str, qty: i32) -> Result<serde_json::Value, reqwest::Error> {
    let client = reqwest::Client::new();
    let res = client.post(&format!("https://api.bazaara.store/api/v1/stores/{}/cart/items", store_id))
        .header("X-Guest-Token", guest_token)
        .json(&serde_json::json!({
            "productId": product_id,
            "quantity": qty,
            "variantInfo": {}
        }))
        .send()
        .await?
        .json::<serde_json::Value>()
        .await?;
    Ok(res)
}

4. Update Item Quantity

Updates the quantity of a specific line item in the cart. Set the quantity parameter to 0 to delete the item.

  • Method: PUT
  • Route: /api/v1/stores/:storeId/cart/items/:itemId
curl -X PUT https://api.bazaara.store/api/v1/stores/YOUR_STORE_ID/cart/items/ITEM_LINE_ID \
  -H "Content-Type: application/json" \
  -H "X-Guest-Token: GUEST_TOKEN_UUID" \
  -d '{ "quantity": 3 }'
async function updateCartItem(storeId: string, guestToken: string, itemId: string, quantity: number) {
  const response = await fetch(`https://api.bazaara.store/api/v1/stores/${storeId}/cart/items/${itemId}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'X-Guest-Token': guestToken
    },
    body: JSON.stringify({ quantity })
  });
  return response.json();
}
import requests

def update_qty(store_id, guest_token, item_id, quantity):
    url = f"https://api.bazaara.store/api/v1/stores/{store_id}/cart/items/{item_id}"
    headers = { "X-Guest-Token": guest_token }
    response = requests.put(url, json={"quantity": quantity}, headers=headers)
    return response.json()
async fn update_qty(store_id: &str, guest_token: &str, item_id: &str, qty: i32) -> Result<serde_json::Value, reqwest::Error> {
    let client = reqwest::Client::new();
    let res = client.put(&format!("https://api.bazaara.store/api/v1/stores/{}/cart/items/{}", store_id, item_id))
        .header("X-Guest-Token", guest_token)
        .json(&serde_json::json!({ "quantity": qty }))
        .send()
        .await?
        .json::<serde_json::Value>()
        .await?;
    Ok(res)
}