Price comparison tools help shoppers and resellers find the best deal across multiple retailers. Building one requires querying multiple platforms, normalizing price formats, and ranking results by value. The Scavio API covers both Amazon and Walmart with a unified endpoint, making it straightforward to query both platforms and compare results side by side. This tutorial builds a comparison function that accepts a product name, queries both platforms in parallel, and returns a sorted list of offers.
Prerequisites
- Python 3.10 or higher
- requests and concurrent.futures (stdlib) available
- A Scavio API key
- Basic understanding of async or concurrent programming
Walkthrough
Step 1: Query both platforms concurrently
Use concurrent.futures.ThreadPoolExecutor to query Amazon and Walmart at the same time, reducing total latency.
from concurrent.futures import ThreadPoolExecutor, as_completed
def search_platform(platform: str, query: str) -> list[dict]:
r = requests.post(
"https://api.scavio.dev/api/v1/search",
headers={"x-api-key": API_KEY},
json={"platform": platform, "query": query, "marketplace": "US"}
)
r.raise_for_status()
return r.json().get("products", [])
def fetch_both(query: str) -> dict:
with ThreadPoolExecutor(max_workers=2) as ex:
futures = {ex.submit(search_platform, p, query): p for p in ["amazon", "walmart"]}
return {futures[f]: f.result() for f in as_completed(futures)}Step 2: Normalize prices across platforms
Both platforms return price as a string like "$29.99". Parse them into floats for comparison.
def normalize(product: dict, source: str) -> dict:
price_str = product.get("price", "")
price = float(price_str.replace("$", "").replace(",", "")) if price_str else None
return {"source": source, "title": product.get("title"), "price": price, "raw": price_str}Step 3: Merge and sort results
Combine results from both platforms into one list and sort by price ascending.
def compare(query: str) -> list[dict]:
results = fetch_both(query)
items = []
for source, products in results.items():
items.extend(normalize(p, source) for p in products[:5])
return sorted(items, key=lambda x: x["price"] or float("inf"))Step 4: Display the comparison table
Print a formatted table showing the best price for the product across both platforms.
offers = compare("Sony WH-1000XM5")
print(f"{'Source':<10} {'Price':<10} {'Title'}")
for o in offers[:6]:
print(f"{o['source']:<10} {o['raw']:<10} {o['title'][:50]}")Python Example
import os
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
API_KEY = os.environ.get("SCAVIO_API_KEY", "your_scavio_api_key")
ENDPOINT = "https://api.scavio.dev/api/v1/search"
def search(platform: str, query: str) -> list[dict]:
r = requests.post(ENDPOINT, headers={"x-api-key": API_KEY},
json={"platform": platform, "query": query, "marketplace": "US"})
r.raise_for_status()
return r.json().get("products", [])
def compare(query: str) -> list[dict]:
with ThreadPoolExecutor(max_workers=2) as ex:
futs = {ex.submit(search, p, query): p for p in ["amazon", "walmart"]}
all_items = []
for f in as_completed(futs):
src = futs[f]
for p in f.result()[:5]:
price_str = p.get("price", "")
price = float(price_str.replace("$", "").replace(",", "")) if price_str else None
all_items.append({"source": src, "price": price, "raw": price_str, "title": p.get("title", "")})
return sorted(all_items, key=lambda x: x["price"] or float("inf"))
if __name__ == "__main__":
for o in compare("Sony WH-1000XM5")[:6]:
print(f"{o['source']:<10} {o['raw']:<10} {o['title'][:50]}")JavaScript Example
const API_KEY = process.env.SCAVIO_API_KEY || "your_scavio_api_key";
const ENDPOINT = "https://api.scavio.dev/api/v1/search";
async function searchPlatform(platform, query) {
const res = await fetch(ENDPOINT, {
method: "POST",
headers: { "x-api-key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({ platform, query, marketplace: "US" })
});
const data = await res.json();
return (data.products || []).slice(0, 5).map(p => ({ source: platform, ...p }));
}
async function compare(query) {
const [amazon, walmart] = await Promise.all([
searchPlatform("amazon", query),
searchPlatform("walmart", query)
]);
const all = [...amazon, ...walmart];
return all.sort((a, b) => {
const pa = parseFloat((a.price || "").replace(/[$,]/g, "")) || Infinity;
const pb = parseFloat((b.price || "").replace(/[$,]/g, "")) || Infinity;
return pa - pb;
});
}
compare("Sony WH-1000XM5").then(results => {
results.slice(0, 6).forEach(r => console.log(`${r.source}: ${r.price} — ${r.title?.slice(0, 50)}`));
}).catch(console.error);Expected Output
amazon $279.00 Sony WH-1000XM5 Wireless Noise Canceling Headphones
amazon $289.99 Sony WH-1000XM5 Wireless Headphones (Midnight Black)
walmart $279.00 Sony WH1000XM5 Bluetooth Headphones
walmart $299.00 Sony WH-1000XM5 Premium Wireless Headphones