Appearance
Python SDK
Installation
pip install x402-client httpx eth-account
Basic Usage
from x402.client import X402Client
from eth_account import Account
wallet = Account.from_key("0x_your_key")
client = X402Client(wallet=wallet)
# Single price ($0.01)
eth = client.get("https://api.foursec.xyz/price/ETH").json()
print(f"ETH: ${eth['price_usd']}")
# Batch multiple tokens
for symbol in ['BTC', 'ETH', 'SOL']:
data = client.get(f"https://api.foursec.xyz/price/{symbol}").json()
print(f"{symbol}: ${data['price_usd']}")
Funding Rate Endpoints
# Current funding rate ($0.03)
funding = client.get("https://api.foursec.xyz/funding-rate/ETH").json()
print(f"Funding rate: {funding['funding_rate']}%")
# Historical funding rates ($0.04)
history = client.get(
"https://api.foursec.xyz/funding-rate/history/ETH?limit=100"
).json()
print(f"Historical rates: {len(history['rates'])}")
# Extreme funding rates ($0.05)
extreme = client.get("https://api.foursec.xyz/funding-rate/extreme").json()
for s in extreme['symbols']:
print(f"{s['symbol']}: {s['funding_rate']}%")
Liquidation Endpoints
# Liquidation heatmap ($0.04)
heatmap = client.get(
"https://api.foursec.xyz/liquidation/heatmap/ETH"
).json()
print(f"Liquidation levels: {len(heatmap['levels'])}")
# Recent liquidations ($0.03)
recent = client.get("https://api.foursec.xyz/liquidation/recent").json()
print(f"Recent events: {len(recent['events'])}")
# Liquidation alert ($0.06)
alert = client.get("https://api.foursec.xyz/liquidation/alert").json()
print(f"Alert status: {alert['status']}")
CEX Market Data
# Aggregated CEX prices ($0.02)
prices = client.get(
"https://api.foursec.xyz/api/v1/prices?symbols=btc,eth,sol"
).json()
for p in prices['prices']:
print(f"{p['symbol']}: ${p['current_price']}")
# CEX market details ($0.03)
market = client.get(
"https://api.foursec.xyz/api/v1/market/bitcoin"
).json()
print(f"Market: {market['name']} - ${market['current_price']}")
# CEX market history ($0.04)
market_hist = client.get(
"https://api.foursec.xyz/api/v1/market/bitcoin/history?days=7"
).json()
print(f"Historical data points: {len(market_hist['prices'])}")
Error Handling
import httpx
try:
response = client.get("https://api.foursec.xyz/price/XYZ")
except httpx.HTTPStatusError as e:
if e.response.status_code == 404:
print("Symbol not found")
elif e.response.status_code == 429:
print("Rate limited, retrying...")
Async Usage
import asyncio
import httpx
async def fetch_prices():
async with httpx.AsyncClient() as http:
tasks = [
client.aget(f"https://api.foursec.xyz/price/{s}")
for s in ['BTC', 'ETH', 'SOL']
]
results = await asyncio.gather(*tasks)
return [r.json() for r in results]
prices = asyncio.run(fetch_prices())
Caching Wrapper
import time
class CachedClient:
def __init__(self, api_client, ttl=30):
self.api = api_client
self.ttl = ttl
self.cache = {}
def get(self, url):
now = time.time()
if url in self.cache and (now - self.cache[url]['time']) < self.ttl:
return self.cache[url]['data']
data = self.api.get(url).json()
self.cache[url] = {'data': data, 'time': now}
return data
Redis Integration
import redis
import json
cache = redis.Redis()
def get_price(symbol):
key = f"price:{symbol}"
cached = cache.get(key)
if cached:
return json.loads(cached)
data = client.get(f"https://api.foursec.xyz/price/{symbol}").json()
cache.setex(key, 30, json.dumps(data))
return data