Skip to content

Use Cases

1. AI Trading Bots

Build autonomous trading agents with real-time data.

python
class TradingBot:
    def __init__(self, wallet):
        self.api = X402Client(wallet=wallet)
    
    async def analyze_and_trade(self):
        # Get real-time price ($0.01)
        eth = await self.api.get("https://api.foursec.xyz/price/ETH")
        
        # Check arbitrage ($0.05)
        arb = await self.api.get("https://api.foursec.xyz/arbitrage-opportunity")
        
        # Assess risk ($0.03)
        vol = await self.api.get("https://api.foursec.xyz/volatility-index/ETH")
        
        # Check funding rate ($0.03)
        funding = await self.api.get("https://api.foursec.xyz/funding-rate/ETH")
        
        # Check liquidation levels ($0.04)
        liqs = await self.api.get("https://api.foursec.xyz/liquidation/heatmap/ETH")
        
        if arb['total_opportunities'] > 0:
            profit = arb['opportunities'][0]['estimated_profit_usd']
            if profit > 5.0:
                await self.execute_arbitrage(arb['opportunities'][0])
        
        # Total analysis cost: $0.16 per cycle

2. Portfolio Trackers

python
class PortfolioTracker:
    async def get_portfolio_value(self, holdings):
        total_value = 0
        for symbol, amount in holdings.items():
            price = await self.api.get(
                f"https://api.foursec.xyz/price/{symbol}"
            ).json()
            total_value += price['price_usd'] * amount
        return total_value

3. DeFi Dashboards

python
class MarketDashboard:
    async def generate_report(self):
        tokens = ['BTC', 'ETH', 'SOL', 'ARB', 'OP']
        report = []
        for symbol in tokens:
            price = await self.api.get(f"/price/{symbol}")
            vol = await self.api.get(f"/volatility-index/{symbol}")
            report.append({
                'symbol': symbol,
                'price': price['price_usd'],
                'change': price['change_24h'],
                'risk': vol['risk_level']
            })
        # Cost: $0.30 per report (5 tokens x 2 endpoints)

4. Price Alert Bots

python
alert = client.post("https://api.foursec.xyz/price-alert", json={
    "symbol": "ETH",
    "condition": "below",
    "target_price": 2400,
    "webhook_url": "https://your-bot.com/webhook",
    "duration_hours": 24
}).json()
# Cost: $0.01 per alert

5. Research and Analytics

python
history = client.get(
    "https://api.foursec.xyz/price/history/ETH?interval=1h&limit=720"
).json()
for candle in history['candles']:
    print(f"{candle['timestamp']}: O={candle['open']} H={candle['high']}")
# Cost: $0.02 per request

6. Funding Rate Arbitrage

Monitor extreme funding rates across perpetual markets to find short/long opportunities.

python
class FundingRateArb:
    async def scan_opportunities(self):
        # Get extreme funding rates ($0.05)
        extreme = await self.api.get(
            "https://api.foursec.xyz/funding-rate/extreme"
        ).json()
        
        opportunities = []
        for symbol_data in extreme['symbols']:
            # Get current rate for confirmation ($0.03)
            rate = await self.api.get(
                f"https://api.foursec.xyz/funding-rate/{symbol_data['symbol']}"
            ).json()
            
            # Get price for position sizing ($0.01)
            price = await self.api.get(
                f"https://api.foursec.xyz/price/{symbol_data['symbol']}"
            ).json()
            
            if abs(rate['funding_rate']) > 0.05:  # >5% funding
                opportunities.append({
                    'symbol': symbol_data['symbol'],
                    'rate': rate['funding_rate'],
                    'price': price['price_usd'],
                    'strategy': 'short' if rate['funding_rate'] > 0 else 'long'
                })
        
        return opportunities
        # Cost: $0.09 per symbol scanned

7. Liquidation Monitoring

Track liquidation cascades and set alerts for risk management.

python
class LiquidationMonitor:
    async def check_risk(self, symbol):
        # Get liquidation heatmap ($0.04)
        heatmap = await self.api.get(
            f"https://api.foursec.xyz/liquidation/heatmap/{symbol}"
        ).json()
        
        # Get recent liquidation events ($0.03)
        recent = await self.api.get(
            "https://api.foursec.xyz/liquidation/recent"
        ).json()
        
        # Get current price ($0.01)
        price = await self.api.get(
            f"https://api.foursec.xyz/price/{symbol}"
        ).json()
        
        # Check if current price is near dense liquidation levels
        current = price['price_usd']
        danger_zones = [
            level for level in heatmap['levels']
            if abs(level['price'] - current) / current < 0.05
        ]
        
        return {
            'symbol': symbol,
            'current_price': current,
            'nearby_liquidations': danger_zones,
            'recent_events': recent['events'][:5]
        }
        # Cost: $0.08 per symbol check

8. CEX Market Analysis

Compare DEX vs CEX prices for cross-market insights.

python
class CEXAnalyzer:
    async def compare_markets(self, coin_id):
        # Get CEX market details ($0.03)
        cex_market = await self.api.get(
            f"https://api.foursec.xyz/api/v1/market/{coin_id}"
        ).json()
        
        # Get DEX price ($0.01)
        symbol = cex_market['symbol']
        dex_price = await self.api.get(
            f"https://api.foursec.xyz/price/{symbol}"
        ).json()
        
        spread = abs(cex_market['current_price'] - dex_price['price_usd'])
        spread_pct = (spread / cex_market['current_price']) * 100
        
        return {
            'symbol': symbol,
            'cex_price': cex_market['current_price'],
            'dex_price': dex_price['price_usd'],
            'spread_pct': spread_pct,
            'volume_24h': cex_market['total_volume']
        }
        # Cost: $0.04 per comparison

Cost Calculator

Use CaseRequests/HourCost/HourCost/Day
Trading Bot (Full Analysis)60$0.96$23.04
Trading Bot (Price Only)60$0.60$14.40
Portfolio Tracker12$0.12$2.88
Market Dashboard12$0.36$8.64
Price Alerts10$0.10$2.40
Research5$0.10$2.40
Funding Rate Scanner6$0.54$12.96
Liquidation Monitor12$0.96$23.04
CEX Comparison10$0.40$9.60

TIP

Use the 30-second cache wisely! Identical requests within 30s are free. This dramatically reduces costs for high-frequency bots.

Built with x402 protocol on Base