← Back to documentation

developer-guide-v2

BuyWhere API Developer Guide

A comprehensive technical guide for AI agent developers and platform integrators building with the BuyWhere product catalog API.

Table of Contents

  1. Overview
  2. Authentication
  3. Core Endpoints
  4. MCP Integration
  5. Rate Limiting
  6. SDK Usage
  7. Affiliate Integration
  8. Use Case Walkthroughs

Overview

BuyWhere provides an agent-native product catalog API that unifies product data from Lazada, Shopee, Amazon SG, Carousell, Qoo10, and other Singapore e-commerce platforms into a single, queryable interface.

Base URL:

https://api.buywhere.ai

Current Version: v2

Key capabilities:

  • Full-text product search across 500K+ products
  • Cross-platform price comparison
  • Real-time deal discovery
  • Category browsing with hierarchical taxonomy
  • Direct purchase links with affiliate tracking

Authentication

Getting an API Key

Contact the BuyWhere team to provision an API key, or use the self-service developer signup flow where enabled. You'll receive:

  • A key ID (UUID) — used for JWT token management
  • A raw key (bw_xxxxx) — used directly as a Bearer token

Store the raw key securely — it will not be shown again.

Using Your API Key

Include your API key in the Authorization header as a Bearer token:

curl https://api.buywhere.ai/v2/search?q=laptop \
  -H "Authorization: Bearer bw_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Alternative header for compatibility:

curl https://api.buywhere.ai/v2/search?q=laptop \
  -H "X-API-Key: bw_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

API Key Format

  • Raw keys are issued as opaque bw_live_... tokens for production
  • Test keys use the bw_test_... prefix
  • Keys are hashed before storage — never store the raw key in plaintext

Authentication Errors

401 Unauthorized — Invalid or missing API key:

{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Invalid or missing API key"
  }
}

Solutions:

  • Verify your API key is correct
  • Ensure Authorization: Bearer <key> header format is correct
  • If using the alternate auth mode, ensure X-API-Key: <key> is set exactly
  • Check that your key hasn't been revoked

Core Endpoints

Search Products

Endpoint: GET /v2/search

Full-text product search with natural language queries and filters.

Request:

curl "https://api.buywhere.ai/v2/search?q=sony+wh-1000xm5&limit=10&max_price=500" \
  -H "Authorization: Bearer bw_live_xxxxxxxx"

Parameters:

ParameterTypeDescription
qstringSearch query (required)
limitintegerNumber of results (default: 20, max: 100)
offsetintegerPagination offset
categorystringFilter by category name
min_pricenumberMinimum price filter
max_pricenumberMaximum price filter
in_stockbooleanFilter by availability
sourcestringFilter by merchant (e.g., shopee_sg, lazada_sg)
regionstringRegional filter: sg, us, sea
currencystringResponse currency (SGD, USD, MYR, THB, PHP, VND)

Response:

{
  "total": 847,
  "limit": 10,
  "offset": 0,
  "has_more": true,
  "items": [
    {
      "id": "prod_12345",
      "sku": "SONY-WH1000XM5-B",
      "name": "Sony WH-1000XM5 Wireless Noise Cancelling Headphones",
      "description": "Industry-leading noise cancellation with Auto NC Optimizer...",
      "price": 449.00,
      "currency": "SGD",
      "source": "shopee_sg",
      "buy_url": "https://buywhere.ai/redirect/prod_12345",
      "affiliate_url": "https://buywhere.ai/redirect/prod_12345?aff=abc123",
      "image_url": "https://cdn.buywhere.ai/img/prod_12345.jpg",
      "category": "Electronics > Audio > Headphones",
      "category_path": ["Electronics", "Audio", "Headphones"],
      "brand": "Sony",
      "is_available": true,
      "availability": "in_stock",
      "rating": 4.7,
      "review_count": 2847,
      "metadata": {
        "original_url": "https://shopee.sg/sony-wh-1000xm5"
      },
      "confidence_score": 0.95,
      "updated_at": "2026-04-15T10:30:00Z"
    }
  ]
}

Price Comparison

Endpoint: GET /v2/agents/price-comparison

Cross-platform price comparison to find the best deal across all merchants.

curl "https://api.buywhere.ai/v2/agents/price-comparison?product_id=prod_12345" \
  -H "Authorization: Bearer bw_live_xxxxxxxx"

Response:

{
  "product_id": "prod_12345",
  "product_name": "Sony WH-1000XM5 Wireless Noise Cancelling Headphones",
  "total_matches": 8,
  "matches": [
    {
      "source": "shopee_sg",
      "price": 449.00,
      "currency": "SGD",
      "buy_url": "https://shopee.sg/sony-wh-1000xm5",
      "affiliate_url": "https://buywhere.ai/redirect/prod_12345?source=shopee_sg&aff=abc123",
      "is_available": true,
      "rating": 4.7,
      "review_count": 2847,
      "savings_pct": 0,
      "last_updated": "2026-04-15T10:30:00Z"
    },
    {
      "source": "lazada_sg",
      "price": 469.00,
      "currency": "SGD",
      "buy_url": "https://lazada.sg/sony-wh-1000xm5",
      "affiliate_url": "https://buywhere.ai/redirect/prod_12345?source=lazada_sg&aff=abc123",
      "is_available": true,
      "rating": 4.6,
      "review_count": 1523,
      "savings_pct": 4.3,
      "last_updated": "2026-04-15T09:15:00Z"
    }
  ],
  "highlights": {
    "cheapest": {
      "source": "shopee_sg",
      "price": 449.00,
      "currency": "SGD"
    },
    "highest_rated": {
      "source": "shopee_sg",
      "rating": 4.7
    }
  }
}

Product Detail

Endpoint: GET /v2/products/{id}

Get detailed information for a single product by BuyWhere ID.

curl "https://api.buywhere.ai/v2/products/prod_12345" \
  -H "Authorization: Bearer bw_live_xxxxxxxx"

Response:

{
  "id": "prod_12345",
  "sku": "SONY-WH1000XM5-B",
  "name": "Sony WH-1000XM5 Wireless Noise Cancelling Headphones",
  "description": "Industry-leading noise cancellation with Auto NC Optimizer. Crystal clear hands-free calling with 4 beamforming microphones...",
  "price": 449.00,
  "currency": "SGD",
  "source": "shopee_sg",
  "buy_url": "https://shopee.sg/sony-wh-1000xm5",
  "affiliate_url": "https://buywhere.ai/redirect/prod_12345?aff=abc123",
  "image_url": "https://cdn.buywhere.ai/img/prod_12345.jpg",
  "images": [
    "https://cdn.buywhere.ai/img/prod_12345.jpg",
    "https://cdn.buywhere.ai/img/prod_12345_2.jpg"
  ],
  "category": "Electronics > Audio > Headphones",
  "category_path": ["Electronics", "Audio", "Headphones"],
  "brand": "Sony",
  "is_available": true,
  "availability": "in_stock",
  "stock_level": null,
  "rating": 4.7,
  "review_count": 2847,
  "metadata": {
    "original_url": "https://shopee.sg/sony-wh-1000xm5",
    "seller": "Sony Official Store",
    "condition": "new"
  },
  "confidence_score": 0.95,
  "updated_at": "2026-04-15T10:30:00Z"
}

Batch Product Lookup

Endpoint: POST /v2/agents/batch-lookup

Retrieve multiple products by their BuyWhere IDs in a single request.

curl -X POST "https://api.buywhere.ai/v2/agents/batch-lookup" \
  -H "Authorization: Bearer bw_live_xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{"product_ids": ["prod_12345", "prod_67890", "prod_11111"]}'

Response:

{
  "products": [
    {
      "id": "prod_12345",
      "name": "Sony WH-1000XM5",
      "price": 449.00,
      "source": "shopee_sg"
    },
    {
      "id": "prod_67890",
      "name": "Apple AirPods Pro 2",
      "price": 329.00,
      "source": "lazada_sg"
    }
  ],
  "not_found": ["prod_11111"]
}

Best Price

Endpoint: GET /v2/agents/best-price

Find the cheapest listing for a product across all merchants.

curl "https://api.buywhere.ai/v2/agents/best-price?product_name=Sony%20WH-1000XM5" \
  -H "Authorization: Bearer bw_live_xxxxxxxx"

Response:

{
  "total": 8,
  "items": [
    {
      "id": "prod_12345",
      "source": "shopee_sg",
      "price": 449.00,
      "currency": "SGD",
      "buy_url": "https://shopee.sg/sony-wh-1000xm5",
      "is_available": true,
      "rating": 4.7,
      "review_count": 2847,
      "last_checked": "2026-04-15T10:30:00Z"
    }
  ]
}

Bulk Compare

Endpoint: POST /v2/agents/bulk-compare

Compare prices across multiple products in a single request.

curl -X POST "https://api.buywhere.ai/v2/agents/bulk-compare" \
  -H "Authorization: Bearer bw_live_xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "items": [
      {"product_name": "Sony WH-1000XM5", "limit": 3},
      {"product_name": "Apple AirPods Pro 2", "limit": 3}
    ]
  }'

Response:

{
  "results": [
    {
      "query": "Sony WH-1000XM5",
      "matches": [
        {"source": "shopee_sg", "price": 449.00, "currency": "SGD"},
        {"source": "lazada_sg", "price": 469.00, "currency": "SGD"},
        {"source": "amazon_sg", "price": 479.00, "currency": "SGD"}
      ]
    },
    {
      "query": "Apple AirPods Pro 2",
      "matches": [
        {"source": "lazada_sg", "price": 329.00, "currency": "SGD"},
        {"source": "shopee_sg", "price": 339.00, "currency": "SGD"}
      ]
    }
  ]
}

Compare Matrix

Endpoint: POST /v2/agents/compare-matrix

Full comparison matrix for multiple products showing all merchants side by side.

curl -X POST "https://api.buywhere.ai/v2/agents/compare-matrix" \
  -H "Authorization: Bearer bw_live_xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "products": ["prod_12345", "prod_67890", "prod_11111"],
    "show_metadata": true
  }'

Categories

Endpoint: GET /v2/categories

List all available product categories with hierarchical structure.

curl "https://api.buywhere.ai/v2/categories" \
  -H "Authorization: Bearer bw_live_xxxxxxxx"

Response:

{
  "total": 128,
  "categories": [
    {
      "id": "cat_electronics",
      "name": "Electronics",
      "path": "Electronics",
      "count": 45230,
      "children": [
        {
          "id": "cat_audio",
          "name": "Audio",
          "path": "Electronics > Audio",
          "count": 8420,
          "children": [
            {
              "id": "cat_headphones",
              "name": "Headphones",
              "path": "Electronics > Audio > Headphones",
              "count": 2150
            }
          ]
        }
      ]
    }
  ]
}

MCP Integration

The Model Context Protocol (MCP) provides a standardized way for AI agents and AI models (Claude, GPT-4, Gemini, etc.) to interact with BuyWhere's product catalog tools. BuyWhere exposes 7 MCP tools for direct AI agent integration — no REST HTTP code required.

Available MCP Tools

ToolDescription
searchNatural language product search across all merchants
price_comparisonCross-retailer price comparison for a specific product
batch_lookupMulti-product lookup by BuyWhere product ID
bulk_compareBulk price comparison across multiple products
exploreBrowse products by category with filtering
best_priceFind the cheapest listing for a product across all merchants
compare_matrixFull comparison matrix for multiple products

Connecting to Claude (Desktop)

  1. Download Claude for Desktop
  2. Install the BuyWhere MCP server:
# Install the BuyWhere MCP server
npm install -g @buywhere/mcp-server

# Or with Python
pip install buywhere-mcp
  1. Configure Claude's MCP settings in ~/Library/Application Support/Claude/claude_desktop_config.json:
{
  "mcpServers": {
    "buywhere": {
      "command": "npx",
      "args": ["-y", "@buywhere/mcp-server"],
      "env": {
        "BUYWHERE_API_KEY": "bw_live_xxxxxxxxxxxxxxxx"
      }
    }
  }
}
  1. Restart Claude Desktop. BuyWhere tools will appear automatically.

Connecting to Cursor

Add to your Cursor MCP settings in .cursor/mcp.json:

{
  "mcpServers": {
    "buywhere": {
      "command": "npx",
      "args": ["-y", "@buywhere/mcp-server"],
      "env": {
        "BUYWHERE_API_KEY": "bw_live_xxxxxxxxxxxxxxxx"
      }
    }
  }
}

Connecting to GPT-4 (OpenAI API)

Use the OpenAI Functions / Tools API with BuyWhere as a function:

from openai import OpenAI

client = OpenAI()

# Define BuyWhere as an available function
tools = [
    {
        "type": "function",
        "function": {
            "name": "search_products",
            "description": "Search for products across BuyWhere's catalog of Southeast Asian e-commerce",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string", "description": "Search query"},
                    "max_price": {"type": "number", "description": "Maximum price filter"},
                    "limit": {"type": "integer", "description": "Number of results (default 20)"}
                },
                "required": ["query"]
            }
        }
    }
]

# Use in a conversation
messages = [
    {"role": "user", "content": "Find me a good laptop under $1000 in Singapore"}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,
    tool_choice="auto"
)

# Handle function calls automatically
# GPT-4 will call the buywhere search tool and return results

MCP Server Self-Hosting

For production deployments, run the BuyWhere MCP server yourself:

# Docker
docker run -d \
  --name buywhere-mcp \
  -e BUYWHERE_API_KEY=bw_live_xxxxxxxx \
  -e BUYWHERE_API_BASE=https://api.buywhere.ai \
  -p 8080:8080 \
  buywhere/mcp-server:latest

# The MCP server runs at http://localhost:8080/mcp
# Configure your AI agent to connect to this endpoint

MCP vs REST

AspectREST APIMCP
InvocationHTTP requests from your codeDirect tool calls by the AI model
ContextYou manage state and contextModel manages context automatically
DiscoveryRead docs, write codeModel discovers available tools automatically
AuthAPI keys on every requestConfigured once per server connection
Latency~100-300ms per request~50-150ms per tool call

Rate Limiting

Rate limits protect the API from abuse and ensure fair access for all integrators. All requests are counted against your API key.

Rate Limit Tiers

TierRequests/MinuteDaily LimitDescription
Free1001,000Default for new API keys
Starter / Basic5001,000For individual developers
Pro / Growth / Standard / Premium1,00050,000For production applications
Enterprise10,000UnlimitedFor high-volume integrations

Rate Limit Headers

Every response includes rate limit information in headers:

X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 847
X-RateLimit-Reset: 1713202500
  • X-RateLimit-Limit: Total requests allowed per minute
  • X-RateLimit-Remaining: Requests remaining in the current 60-second window
  • X-RateLimit-Reset: Unix timestamp when the window resets

429 Error Response

When you exceed your rate limit, the API returns 429 Too Many Requests:

{
  "error": {
    "code": "RATE_LIMIT_EXCEEDED",
    "message": "Rate limit exceeded. Retry after 43 seconds.",
    "retry_after": 43
  }
}

Retry Strategy

Use exponential backoff when you encounter 429 errors:

import time
import httpx

def fetch_with_retry(url, headers, max_retries=5):
    """Fetch with exponential backoff on rate limit errors."""
    for attempt in range(max_retries):
        response = httpx.get(url, headers=headers)
        if response.status_code == 429:
            retry_after = int(response.headers.get("Retry-After", 60))
            wait_time = retry_after * (2 ** attempt)  # Exponential backoff
            print(f"Rate limited. Retrying in {wait_time}s (attempt {attempt + 1}/{max_retries})...")
            time.sleep(wait_time)
        elif response.status_code >= 500:
            # Server error — retry
            wait_time = 2 ** attempt
            print(f"Server error {response.status_code}. Retrying in {wait_time}s...")
            time.sleep(wait_time)
        else:
            return response
    raise Exception("Max retries exceeded")

The Python SDK handles rate limit retries automatically with exponential backoff:

from buywhere_sdk import BuyWhere

client = BuyWhere(api_key="bw_live_xxxxx")
# The SDK automatically retries on 429 with exponential backoff
results = client.search("laptop")

SDK Usage

Python SDK

Install the SDK:

pip install buywhere-sdk

Or with framework integrations:

pip install "buywhere-sdk[langchain]"  # LangChain integration
pip install "buywhere-sdk[crewai]"    # CrewAI integration

Basic Usage:

from buywhere_sdk import BuyWhere

client = BuyWhere(api_key="bw_live_xxxxxxxxxxxxxxxx")

# Search products
results = client.search("dyson vacuum cleaner", limit=10)
for p in results.items:
    print(f"{p.name} — {p.currency} {p.price} @ {p.source}")

# Find the cheapest listing
cheapest = client.best_price("Nintendo Switch OLED")
print(f"Best price: {cheapest.price} at {cheapest.source}")

# List all categories
cats = client.list_categories()
print(f"{cats.total} categories available")

Async Client:

import asyncio
from buywhere_sdk import AsyncBuyWhere

async def main():
    async with AsyncBuyWhere(api_key="bw_live_xxxxx") as client:
        results = await client.search("laptop", limit=5)
        print(f"Found {results.total} products")

asyncio.run(main())

JavaScript/TypeScript SDK

npm install @buywhere/sdk
import { BuyWhereClient } from "@buywhere/sdk";

const client = new BuyWhereClient("bw_live_your_api_key");

// Search products
const result = await client.search({
  q: "wireless headphones",
  limit: 10
});

console.log(`Found ${result.total} products`);
result.items.forEach(product => {
  console.log(`${product.name} - ${product.currency} ${product.price}`);
});

Error Handling

from buywhere_sdk import (
    BuyWhere,
    AuthenticationError,
    RateLimitError,
    NotFoundError,
    BuyWhereError
)

client = BuyWhere(api_key="bw_live_xxxxx")

try:
    product = client.get_product(999999)
except NotFoundError:
    print("Product not found")
except RateLimitError:
    print("Rate limited — wait and retry")
except AuthenticationError:
    print("Invalid API key")
except BuyWhereError as e:
    print(f"API error: {e.message}")

Affiliate Integration

BuyWhere provides affiliate-tracked purchase links that enable commission-based commerce for qualifying integrations.

How Affiliate Links Work

When you include an affiliate parameter in requests or use SDK methods that return purchase URLs, BuyWhere appends tracking parameters to merchant URLs. When users complete purchases, affiliate commissions are tracked.

Using Affiliate URLs

Direct API Usage:

# The affiliate_url field is automatically included in search responses
curl "https://api.buywhere.ai/v2/search?q=iphone" \
  -H "Authorization: Bearer bw_live_xxxxxxxx"

Response includes:

{
  "items": [
    {
      "buy_url": "https://buywhere.ai/redirect/prod_12345",
      "affiliate_url": "https://buywhere.ai/redirect/prod_12345?aff=YOUR_AFFILIATE_ID"
    }
  ]
}

SDK Usage:

# The SDK returns affiliate URLs automatically
results = client.search("iphone")
for product in results.items:
    # Use affiliate_url for commission tracking
    send_to_user(product.name, product.affiliate_url)

Affiliate Requirements

  • Affiliate integration requires separate approval
  • Commission rates vary by merchant and product category
  • Affiliate payouts are processed monthly
  • Contact the BuyWhere team to enable affiliate features

Use Case Walkthroughs

Walkthrough 1: "Find Cheapest iPhone in Singapore"

Complete end-to-end example using the Python SDK.

from buywhere_sdk import BuyWhere, NotFoundError

client = BuyWhere(api_key="bw_live_xxxxxxxxxxxxxxxx")

# Step 1: Search for iPhone models
print("Searching for iPhone 15 Pro...")
search_results = client.search("iPhone 15 Pro", limit=10)

if search_results.total == 0:
    print("No results found")
    exit()

# Step 2: Display top results with prices
print(f"\nFound {search_results.total} options:")
for i, product in enumerate(search_results.items[:5], 1):
    print(f"  {i}. {product.name}")
    print(f"     Price: {product.currency} {product.price}")
    print(f"     Source: {product.source}")
    print(f"     URL: {product.buy_url}\n")

# Step 3: Get price comparison for the top result
top_product = search_results.items[0]
print(f"Getting price comparison for: {top_product.name}...")

try:
    comparison = client.price_comparison(product_id=top_product.id)
    print(f"\nFound {comparison.total_matches} listings across merchants:")

    # Sort by price
    sorted_matches = sorted(comparison.matches, key=lambda x: x.price)

    for match in sorted_matches:
        print(f"  - {match.source}: {match.currency} {match.price}")

    # Highlight the cheapest
    cheapest = comparison.highlights.get("cheapest")
    if cheapest:
        print(f"\n*** BEST PRICE: {cheapest['currency']} {cheapest['price']} at {cheapest['source']} ***")

except NotFoundError:
    print("Price comparison not available for this product")

Output:

Searching for iPhone 15 Pro...
Found 47 options:

Found 47 options:
  1. Apple iPhone 15 Pro 256GB Natural Titanium
     Price: SGD 1649
     Source: shopee_sg
     URL: https://buywhere.ai/redirect/prod_xxxxx

  2. Apple iPhone 15 Pro 128GB Black Titanium
     Price: SGD 1599
     Source: lazada_sg
     URL: https://buywhere.ai/redirect/prod_xxxxx

Getting price comparison for: Apple iPhone 15 Pro 256GB Natural Titanium...

Found 6 listings across merchants:
  - shopee_sg: SGD 1649
  - lazada_sg: SGD 1669
  - amazon_sg: SGD 1699
  - qoo10_sg: SGD 1679

*** BEST PRICE: SGD 1649 at shopee_sg ***

Walkthrough 2: Building a Deal Alert Bot

Find products with active discounts and send notifications.

import time
from buywhere_sdk import BuyWhere

client = BuyWhere(api_key="bw_live_xxxxxxxxxxxxxxxx")

def check_deals(category=None, min_discount=30):
    """Check for deals with at least min_discount % off."""
    print(f"Checking {category or 'all'} deals with {min_discount}%+ discount...")

    deals = client.get_deals(
        category=category,
        min_discount_pct=min_discount,
        limit=20
    )

    print(f"Found {deals.total} deals")
    return deals.items

def send_notification(product):
    """Send a notification for a deal (implementation depends on your setup)."""
    message = (
        f"🔥 DEAL ALERT!\n"
        f"{product.name}\n"
        f"Was: {product.original_price} | Now: {product.price}\n"
        f"Discount: {product.discount_pct}% off\n"
        f"Shop: {product.affiliate_url}"
    )
    # Implement your notification logic here
    print(message)

# Main loop
print("Starting deal alert bot...")
categories = ["Electronics", "Fashion", "Home & Living"]

while True:
    for category in categories:
        try:
            deals = check_deals(category=category, min_discount=30)
            for deal in deals[:5]:  # Top 5 deals per category
                send_notification(deal)
        except Exception as e:
            print(f"Error checking {category}: {e}")

    print("\n--- Checking again in 30 minutes ---\n")
    time.sleep(1800)  # 30 minutes

Walkthrough 3: Building a Shopping Agent with LangChain

Integrate BuyWhere as a tool in a LangChain agent.

import os
from langchain.tools import tool
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain import hub
from buywhere_sdk import BuyWhere, NotFoundError

# Initialize BuyWhere client
client = BuyWhere(api_key=os.environ["BUYWHERE_API_KEY"])

# Define BuyWhere as LangChain tools
@tool
def search_products(query: str, max_price: float = None, category: str = None) -> str:
    """Search for products by query with optional filters.

    Args:
        query: The product search query (e.g., 'Sony headphones')
        max_price: Optional maximum price filter
        category: Optional category filter
    """
    try:
        results = client.search(
            query=query,
            max_price=max_price,
            category=category,
            limit=5
        )
        if not results.items:
            return "No products found"

        response = f"Found {results.total} products:\n"
        for p in results.items:
            response += f"- {p.name} | {p.currency} {p.price} | {p.source}\n"
        return response
    except Exception as e:
        return f"Search error: {str(e)}"

@tool
def compare_price(product_id: str) -> str:
    """Compare a product price across all merchants.

    Args:
        product_id: The BuyWhere product ID
    """
    try:
        comparison = client.price_comparison(product_id=product_id)
        response = f"Price comparison for {comparison.product_name}:\n"

        sorted_matches = sorted(comparison.matches, key=lambda x: x.price)
        for m in sorted_matches:
            response += f"- {m.source}: {m.currency} {m.price}\n"

        if comparison.highlights.get("cheapest"):
            cheapest = comparison.highlights["cheapest"]
            response += f"\nBest price: {cheapest['currency']} {cheapest['price']} at {cheapest['source']}"
        return response
    except NotFoundError:
        return "Product not found"
    except Exception as e:
        return f"Comparison error: {str(e)}"

@tool
def get_deals(category: str = None, min_discount: float = 20) -> str:
    """Find products with active discounts.

    Args:
        category: Optional category filter
        min_discount: Minimum discount percentage (default 20)
    """
    try:
        deals = client.get_deals(
            category=category,
            min_discount_pct=min_discount,
            limit=10
        )
        if not deals.items:
            return "No deals found"

        response = f"Found {deals.total} deals:\n"
        for p in deals.items:
            response += f"- {p.name} | {p.currency} {p.price} ({p.discount_pct}% off)\n"
        return response
    except Exception as e:
        return f"Deal search error: {str(e)}"

# Initialize LLM and agent
llm = ChatOpenAI(model="gpt-4o")

tools = [search_products, compare_price, get_deals]

prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)

agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,
    max_iterations=5
)

# Run the agent
if __name__ == "__main__":
    result = agent_executor.invoke({
        "input": "Find me a good gaming laptop under $1500. Compare prices across stores and tell me which is the cheapest option."
    })
    print(result["output"])

Additional Resources

Support