Trade Calculator Example
This example shows how to build a simple trade value calculator using the RBLXValue API and vanilla JavaScript.
How It Works
Fetch item values from the API and compare the total value of two sides of a trade.
JavaScript Example
const API_KEY = 'rbx_your_key_here';
async function getItemValue(itemName) {
const slug = itemName.toLowerCase().replace(/\s+/g, '-');
const res = await fetch(`https://rblxvalue.com/api/v1/items?slug=${slug}`, {
headers: { 'X-Api-Key': API_KEY }
});
const data = await res.json();
if (data.error || !data.item) return null;
const a = data.item.value_source_a || 0;
const b = data.item.value_source_b || 0;
return Math.round((a + b) / 2);
}
async function calculateTrade(yourItems, theirItems) {
const [yourValues, theirValues] = await Promise.all([
Promise.all(yourItems.map(getItemValue)),
Promise.all(theirItems.map(getItemValue)),
]);
const yourTotal = yourValues.reduce((s, v) => s + (v || 0), 0);
const theirTotal = theirValues.reduce((s, v) => s + (v || 0), 0);
const difference = theirTotal - yourTotal;
return {
yourTotal,
theirTotal,
difference,
fair: Math.abs(difference) / Math.max(yourTotal, 1) < 0.1,
};
}
// Example usage
const result = await calculateTrade(
['seer', 'ginger'], // your items
['chroma-scythe'] // their items
);
console.log(`Your side: ${result.yourTotal}`);
console.log(`Their side: ${result.theirTotal}`);
console.log(`Difference: ${result.difference}`);
console.log(`Fair trade: ${result.fair}`);
PHP Example
function getItemValue(string $slug, string $apiKey): int {
$ch = curl_init("https://rblxvalue.com/api/v1/items?slug=" . urlencode($slug));
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ["X-Api-Key: $apiKey"],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
if (empty($data["item"])) return 0;
$a = (int)($data["item"]["value_source_a"] ?? 0);
$b = (int)($data["item"]["value_source_b"] ?? 0);
return (int)round(($a + $b) / 2);
}
$yourItems = ["seer", "ginger"];
$theirItems = ["chroma-scythe"];
$yourTotal = array_sum(array_map(fn($i) => getItemValue($i, $apiKey), $yourItems));
$theirTotal = array_sum(array_map(fn($i) => getItemValue($i, $apiKey), $theirItems));
echo "Your side: $yourTotal\n";
echo "Their side: $theirTotal\n";
tip
You can use the /api/v1/items?search= parameter to let users search by partial name instead of requiring exact slugs.