đź’ˇ Best Practices & Rate-Limits

Evita errores y optimiza tus llamadas

⚡ Guía: Buenas prácticas con APIs musicales

1. Respeta los rate-limits

Spotify: ~20 req/10s • Songkick: ~10 req/10s • Radio Browser: sin límite, pero sé amable.

const delay = ms => new Promise(res => setTimeout(res, ms));
await delay(600); // espera 0.6s entre llamadas

2. Cachea respuestas frecuentes

const cache = new Map();
const fetchWithCache = async (url) => {
  if (cache.has(url)) return cache.get(url);
  const res = await fetch(url);
  const data = await res.json();
  cache.set(url, data);
  setTimeout(() => cache.delete(url), 60000); // 1 min
  return data;
};

3. Refresca tokens automáticamente

let token = null;
const getToken = async () => {
  if (!token || Date.now() > token.expires_at) {
    token = await fetchNewToken();
    token.expires_at = Date.now() + token.expires_in * 1000 - 5000;
  }
  return token.access_token;
};

4. Maneja errores 429 con retry exponencial

const fetchRetry = async (url, options, retries = 3) => {
  for (let i = 0; i < retries; i++) {
    const res = await fetch(url, options);
    if (res.status === 429) {
      await delay(2 ** i * 1000);
      continue;
    }
    return res;
  }
};