Rate Limiting

TCG Price Lookup API के rate limits, daily quotas और burst rates को समझने और manage करने का तरीका।


Plan-wise Rate Limits

PlanDaily RequestsBurst Rate
फ्री2001 req/3 सेकंड
Trader10,0001 req/सेकंड
Business100,0003 req/सेकंड

Daily limits हर दिन UTC midnight पर reset होते हैं।

Rate Limit Headers

हर API response में current rate limit information शामिल होती है:

X-RateLimit-Limit: 200
X-RateLimit-Remaining: 150
X-RateLimit-Reset: 1704067200
HeaderDescription
X-RateLimit-LimitPeriod में maximum requests
X-RateLimit-RemainingRemaining requests
X-RateLimit-ResetLimit reset होने का UNIX timestamp

429 Errors Handle करें

Rate limit exceed होने पर 429 Too Many Requests मिलता है:

{
  "error": {
    "code": "rate_limit_exceeded",
    "message": "Daily request limit reached. Resets at midnight UTC."
  }
}

Retry-After header से next request तक wait time check करें:

async function fetchWithRetry(url, options, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    const response = await fetch(url, options);
    
    if (response.status === 429) {
      const retryAfter = parseInt(response.headers.get('retry-after') || '60');
      await new Promise(r => setTimeout(r, retryAfter * 1000));
      continue;
    }
    
    return response;
  }
}

Best Practices

Batch Requests उपयोग करें (Trader plan या उससे ऊपर):

// Inefficient: 20 individual requests
for (const id of cardIds) {
  await tcg.cards.get(id); // ✗
}

// Efficient: 1 batch request
const cards = await tcg.cards.batch(cardIds); // ✓

Responses Cache करें:

Prices कुछ घंटों में update होती हैं। Short-term caching (5-15 मिनट) ज्यादातर use cases के लिए ठीक है:

const cache = new Map();
const CACHE_TTL = 5 * 60 * 1000; // 5 मिनट

async function getCachedCard(id) {
  const cached = cache.get(id);
  if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
    return cached.data;
  }
  const data = await tcg.cards.get(id);
  cache.set(id, { data, timestamp: Date.now() });
  return data;
}