Rate Limiting
Pag-unawa at pamamahala ng rate limits, daily quotas, at burst rates ng TCG Price Lookup API.
Mga Rate Limit Ayon sa Plan
| Plan | Daily Requests | Burst Rate |
|---|---|---|
| Libre | 200 | 1 request/3 segundo |
| Trader | 10,000 | 1 request/segundo |
| Business | 100,000 | 3 requests/segundo |
Ang daily limits ay nire-reset araw-araw sa UTC midnight.
Mga Rate Limit Header
Lahat ng API response ay naglalaman ng kasalukuyang impormasyon ng rate limit:
X-RateLimit-Limit: 200
X-RateLimit-Remaining: 150
X-RateLimit-Reset: 1704067200
| Header | Paliwanag |
|---|---|
X-RateLimit-Limit | Maximum na requests bawat period |
X-RateLimit-Remaining | Natitirang requests |
X-RateLimit-Reset | UNIX timestamp kung kailan iri-reset ang limit |
Paghawak ng 429 Error
Kapag nalampasan ang rate limit, ibabalik ang 429 Too Many Requests:
{
"error": {
"code": "rate_limit_exceeded",
"message": "Daily request limit reached. Resets at midnight UTC."
}
}
Gamitin ang Retry-After header para malaman kung kailan hihintayin bago ang susunod na request:
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;
}
}
Mga Pinakamahusay na Gawi
Gumamit ng Batch Requests (Trader plan o mas mataas):
// Hindi mahusay: 20 hiwalay na requests
for (const id of cardIds) {
await tcg.cards.get(id); // ✗
}
// Mahusay: 1 batch request
const cards = await tcg.cards.batch(cardIds); // ✓
I-cache ang mga Response:
Ang mga presyo ay ina-update bawat ilang oras. Ang panandaliang caching (5–15 minuto) ay angkop para sa karamihan ng use cases:
const cache = new Map();
const CACHE_TTL = 5 * 60 * 1000; // 5 minuto
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;
}