cURL
curl --request GET \ --url https://api.example.com/api/v1/usage \ --header 'X-API-Key: <x-api-key>'
{ "balance_usd": 123, "total_snapshots": 123, "active_videos": 123, "uncharged_usage_usd": 123, "total_charged_usd": 123, "estimated_snapshots_remaining": 123, "cost_per_snapshot": 123 }
Get your account balance, usage statistics, and billing information.
GET https://contentstats.io/api/v1/usage
curl "https://contentstats.io/api/v1/usage" \ -H "X-API-Key: cs_live_YOUR_KEY"
{ "balance_usd": 12.45, "total_snapshots": 450, "active_videos": 3, "uncharged_usage_usd": 0.75, "total_charged_usd": 6.75, "estimated_snapshots_remaining": 830, "cost_per_snapshot": 0.015 }
async function canAffordTracking(durationDays) { const usage = await fetch('https://contentstats.io/api/v1/usage', { headers: { 'X-API-Key': apiKey } }).then(r => r.json()); const estimatedCost = durationDays * 24 * 0.015; const available = usage.balance_usd - usage.uncharged_usage_usd; if (available < estimatedCost) { throw new Error( `Insufficient balance. Need $${estimatedCost.toFixed(2)}, ` + `have $${available.toFixed(2)}` ); } return true; } // Check before tracking await canAffordTracking(7); // 7 days await trackVideo(url, 7);
async function getUsageDashboard() { const usage = await fetch('https://contentstats.io/api/v1/usage', { headers: { 'X-API-Key': apiKey } }).then(r => r.json()); return { balance: `$${usage.balance_usd.toFixed(2)}`, pending: `$${usage.uncharged_usage_usd.toFixed(2)}`, available: `$${(usage.balance_usd - usage.uncharged_usage_usd).toFixed(2)}`, activeVideos: usage.active_videos, totalSnapshots: usage.total_snapshots, avgCostPerSnapshot: `$${usage.cost_per_snapshot}`, estimatedDaysLeft: Math.floor( usage.estimated_snapshots_remaining / (usage.active_videos * 24) ) }; } const dashboard = await getUsageDashboard(); console.log(dashboard); // { // balance: "$12.45", // pending: "$0.75", // available: "$11.70", // activeVideos: 3, // totalSnapshots: 450, // avgCostPerSnapshot: "$0.015", // estimatedDaysLeft: 11 // }
async function checkLowBalance(threshold = 5.0) { const usage = await fetch('https://contentstats.io/api/v1/usage', { headers: { 'X-API-Key': apiKey } }).then(r => r.json()); if (usage.balance_usd < threshold) { await sendAlert({ type: 'low_balance', balance: usage.balance_usd, threshold: threshold, message: `Balance is $${usage.balance_usd}. Please add credits.` }); return true; } return false; } // Check every hour setInterval(() => checkLowBalance(5.0), 60 * 60 * 1000);
def calculate_burn_rate(usage): """Calculate daily spending rate""" active_videos = usage['active_videos'] cost_per_snapshot = usage['cost_per_snapshot'] # Snapshots per day = active videos × 24 hours snapshots_per_day = active_videos * 24 daily_cost = snapshots_per_day * cost_per_snapshot # Days until balance depletes days_remaining = usage['balance_usd'] / daily_cost if daily_cost > 0 else float('inf') return { 'daily_cost': round(daily_cost, 2), 'snapshots_per_day': snapshots_per_day, 'days_remaining': round(days_remaining, 1) } usage = get_usage() burn_rate = calculate_burn_rate(usage) print(f"Daily cost: ${burn_rate['daily_cost']}") print(f"Days until depleted: {burn_rate['days_remaining']}")
async function monitorBudget(monthlyBudget) { const usage = await fetch('https://contentstats.io/api/v1/usage', { headers: { 'X-API-Key': apiKey } }).then(r => r.json()); const currentMonth = new Date().getMonth(); const daysInMonth = new Date( new Date().getFullYear(), currentMonth + 1, 0 ).getDate(); const dayOfMonth = new Date().getDate(); const budgetUsed = usage.total_charged_usd; // Assume reset monthly const budgetPercentage = (budgetUsed / monthlyBudget) * 100; const projectedSpend = (budgetUsed / dayOfMonth) * daysInMonth; if (projectedSpend > monthlyBudget) { console.warn( `⚠️ Projected to exceed budget: ` + `$${projectedSpend.toFixed(2)} > $${monthlyBudget}` ); } return { budget: monthlyBudget, used: budgetUsed, percentage: budgetPercentage.toFixed(1), projected: projectedSpend.toFixed(2) }; } const budget = await monitorBudget(100); // $100/month console.log(`Budget used: ${budget.percentage}%`);
balance_usd vs uncharged_usage_usd
const available = usage.balance_usd - usage.uncharged_usage_usd; console.log(`Available: $${available.toFixed(2)}`);
estimated_snapshots_remaining
estimated_snapshots_remaining = floor(balance_usd / cost_per_snapshot)
$12.45 / $0.015 = 830 snapshots
830 / 72 = ~11.5 days of tracking
total_charged_usd
{ "error": "API key required" }
{ "event": "balance.low", "threshold": 5.0, "url": "https://yourapp.com/webhooks/balance" }
Cache Usage Data
let usageCache = null; let cacheTime = null; const CACHE_TTL = 5 * 60 * 1000; // 5 minutes async function getCachedUsage() { if (usageCache && Date.now() - cacheTime < CACHE_TTL) { return usageCache; } usageCache = await fetch('/api/v1/usage').then(r => r.json()); cacheTime = Date.now(); return usageCache; }
Set Up Auto Top-Up
Monitor Regularly
// Daily at 9 AM cron.schedule('0 9 * * *', async () => { const usage = await getUsage(); await logUsage({ date: new Date(), balance: usage.balance_usd, snapshots: usage.total_snapshots, activeVideos: usage.active_videos }); if (usage.balance_usd < 10) { await notifyLowBalance(usage); } });