Limite de rată
Înțelegerea și gestionarea limitelor de rată, a cotelor zilnice și a ratelor burst ale TCG Price Lookup API.
Limite de rată pe plan
| Plan | Cereri zilnice | Rată burst |
|---|---|---|
| Gratuit | 200 | 1 cerere/3 secunde |
| Trader | 10.000 | 1 cerere/secundă |
| Business | 100.000 | 3 cereri/secundă |
Limita zilnică se resetează în fiecare zi la miezul nopții UTC.
Headere de limite de rată
Toate răspunsurile API includ informații despre limita de rată curentă:
X-RateLimit-Limit: 200
X-RateLimit-Remaining: 150
X-RateLimit-Reset: 1704067200
| Header | Descriere |
|---|---|
X-RateLimit-Limit | Numărul maxim de cereri pe perioadă |
X-RateLimit-Remaining | Cereri rămase |
X-RateLimit-Reset | Timestamp UNIX când se resetează limita |
Gestionarea erorilor 429
Când depășiți limita de rată, se returnează 429 Too Many Requests:
{
"error": {
"code": "rate_limit_exceeded",
"message": "Daily request limit reached. Resets at midnight UTC."
}
}
Folosiți headerul Retry-After pentru a știi cât timp să așteptați:
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;
}
}
Bune practici
Folosiți cereri în lot (plan Trader sau superior):
// Ineficient: 20 de cereri individuale
for (const id of cardIds) {
await tcg.cards.get(id); // ✗
}
// Eficient: 1 cerere în lot
const cards = await tcg.cards.batch(cardIds); // ✓
Memorați în cache răspunsurile:
Prețurile sunt actualizate la câteva ore. Cache-ul pe termen scurt (5–15 minute) este adecvat pentru majoritatea cazurilor:
const cache = new Map();
const CACHE_TTL = 5 * 60 * 1000; // 5 minute
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;
}