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.
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 }
},
...
]
}
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();
}
429.
429 errors.
O campo fixture.status.short indica o status atual. Os valores mais comuns:
1H — Primeiro tempo / First halfHT — Intervalo / Half-time2H — Segundo tempo / Second halfET — Prorrogação / Extra timeP — Penalidades / PenaltiesFT — Encerrado / Full-timefunction 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}`
);
});
}
/fixtures?live=all para obter todos os jogos ao vivo / Call to get all live fixturessetInterval com 15-30s para polling / Use setInterval with 15-30s for pollingAbortController para cancelar chamadas pendentes / Use AbortController to cancel pending callsfixture.status.short para exibir o status correto / Check for correct status display