Como buscar jogos ao vivo

How to get live fixtures with real-time updates

Neste tutorial veremos como buscar todos os jogos ao vivo e exibir placares em tempo real com indicadores visuais.

In this tutorial we'll see how to get all live fixtures and display real-time scores with visual indicators.

1. Buscar jogos ao vivo / Get live fixtures

O endpoint /fixtures com o parâmetro live=all retorna todos os jogos em andamento.

const API_KEY = 'YOUR_API_KEY';
const BASE = 'https://football.api.insyde.one';

async function getLiveFixtures() {
  const res = await fetch(`${BASE}/fixtures?live=all&key=${API_KEY}`);
  const data = await res.json();
  return data.response;
}

const liveMatches = await getLiveFixtures();
console.log(`Live matches: ${liveMatches.length}`);

Resposta da API / API response:

{
  "get": "fixtures",
  "parameters": { "live": "all" },
  "results": 12,
  "response": [
    {
      "fixture": {
        "id": 1234567,
        "status": { "long": "Second Half", "short": "2H", "elapsed": 67 }
      },
      "league": { "id": 71, "name": "Serie A", "round": "Regular Season - 25" },
      "teams": {
        "home": { "id": 127, "name": "Flamengo", "winner": true },
        "away": { "id": 131, "name": "Corinthians", "winner": false }
      },
      "goals": { "home": 2, "away": 1 }
    },
    ...
  ]
}

2. Polling em tempo real / Real-time polling

Para manter os dados atualizados, usamos setInterval com um intervalo de 15-30 segundos. Importante: use AbortController para cancelar chamadas pendentes antes de iniciar uma nova.

let controller = null;

async function pollLive() {
  // Cancel any pending request
  if (controller) controller.abort();
  controller = new AbortController();

  try {
    const res = await fetch(
      `${BASE}/fixtures?live=all&key=${API_KEY}`,
      { signal: controller.signal }
    );
    const data = await res.json();
    renderMatches(data.response);
  } catch (err) {
    if (err.name !== 'AbortError') console.error(err);
  }
}

// Poll every 20 seconds
const interval = setInterval(pollLive, 20000);
pollLive(); // initial call

// Cleanup
function stopPolling() {
  clearInterval(interval);
  if (controller) controller.abort();
}
Intervalo recomendado: 15-30 segundos para jogos ao vivo. Intervalos menores podem resultar em erro 429.
Recommended interval: 15-30 seconds for live fixtures. Shorter intervals may result in 429 errors.

3. Exibir status do jogo / Display match status

O campo fixture.status.short indica o status atual. Os valores mais comuns:

function renderMatches(matches) {
  matches.forEach(m => {
    const elapsed = m.fixture.status.elapsed;
    const status = m.fixture.status.short;
    const home = m.teams.home;
    const away = m.teams.away;

    console.log(
      `[${status} ${elapsed}'] ${home.name} ${m.goals.home}-${m.goals.away} ${away.name}`
    );
  });
}

4. Resumo / Summary

  1. Chamar /fixtures?live=all para obter todos os jogos ao vivo / Call to get all live fixtures
  2. Usar setInterval com 15-30s para polling / Use setInterval with 15-30s for polling
  3. Usar AbortController para cancelar chamadas pendentes / Use AbortController to cancel pending calls
  4. Verificar fixture.status.short para exibir o status correto / Check for correct status display

Back to Football API docs