Anmelden API-Schlüssel holen
Tutorials

How to Build a Team vs Team Comparison Page with an API

Illustration of a side-by-side football team comparison page with stats

Learn how to build a side-by-side team comparison page — form, standings position, squad depth, and head-to-head history — using Live Football API.

Auf dieser Seite

"Team A vs Team B" comparison pages are a staple of sports media and betting-adjacent apps — they answer the question fans actually have before a big match: who's in better form, who has the stronger squad, and who's historically had the upper hand. This guide combines several endpoints into one comparison screen.

What Goes Into a Good Comparison Page

  • Current form — league position and recent results for both teams
  • Head-to-head record — how these two teams have historically matched up
  • Squad depth — who's actually available to play
  • Context — where they sit in the table relative to each other

Step 1: Resolving Team IDs

Start with /team_search to let users pick both teams by name:

import requests

def search_team(name):
    response = requests.get(
        'https://live-football-api.com/api/v1/team_search',
        params={'api_key': 'YOUR_KEY', 'q': name}
    ).json()
    return response['data']['teams'][0]['id']

team_a_id = search_team('arsenal')
team_b_id = search_team('chelsea')

Step 2: Fetching Both Teams' Standings in Parallel

Fire both requests together rather than sequentially — this halves the wait time on the page:

const [standingsA, standingsB] = await Promise.all([
  fetch(`.../team_standings?api_key=YOUR_KEY&team_id=${teamAId}`).then(r => r.json()),
  fetch(`.../team_standings?api_key=YOUR_KEY&team_id=${teamBId}`).then(r => r.json())
]);

const rowA = standingsA.data.standings[0].total[0];
const rowB = standingsB.data.standings[0].total[0];

console.log(`${rowA.team_name}: Rank ${rowA.columns[0]}, ${rowA.points} pts`);
console.log(`${rowB.team_name}: Rank ${rowB.columns[0]}, ${rowB.points} pts`);

Step 3: Getting the Head-to-Head Picture

Head-to-head data requires a specific upcoming or recent match between the two teams — fetch it via /h2h using a match_id that pairs them (from /matches if they're playing soon, or the most recent past meeting):

const h2h = await fetch(
  `.../h2h?api_key=YOUR_KEY&match_id=${upcomingMatchId}&lang=en`
).then(r => r.json());

const summary = h2h.data.summary;
console.log(`${summary.home_wins} - ${summary.draws} - ${summary.away_wins}`);

Step 4: Comparing Squad Depth

Pull both squads to show a simple headcount and average stats comparison — useful context for "who has more depth to rotate":

const [squadA, squadB] = await Promise.all([
  fetch(`.../team_squad?api_key=YOUR_KEY&team_id=${teamAId}`).then(r => r.json()),
  fetch(`.../team_squad?api_key=YOUR_KEY&team_id=${teamBId}`).then(r => r.json())
]);

console.log(`${squadA.data.name}: ${squadA.data.players.length} players`);
console.log(`${squadB.data.name}: ${squadB.data.players.length} players`);

Assembling the Comparison Component

function TeamComparison({ teamAId, teamBId }) {
  const [data, setData] = useState(null);

  useEffect(() => {
    async function load() {
      const [standingsA, standingsB, squadA, squadB] = await Promise.all([
        fetchTeamStandings(teamAId),
        fetchTeamStandings(teamBId),
        fetchTeamSquad(teamAId),
        fetchTeamSquad(teamBId)
      ]);
      setData({ standingsA, standingsB, squadA, squadB });
    }
    load();
  }, [teamAId, teamBId]);

  if (!data) return <Loading />;

  return (
    <div className="comparison-grid">
      <TeamColumn standings={data.standingsA} squad={data.squadA} />
      <VersusDivider />
      <TeamColumn standings={data.standingsB} squad={data.squadB} />
    </div>
  );
}

Handling Teams With No Prior Meetings

Newly promoted teams or clubs from different confederations may have no head-to-head history at all. Check for an empty result before rendering the h2h section:

const hasHistory = h2h.data.summary.home_wins + h2h.data.summary.draws + h2h.data.summary.away_wins > 0;

{hasHistory ? <H2HSummary data={h2h.data} /> : <p>No previous meetings on record</p>}

Reducing Credit Usage on Comparison Pages

A single comparison page can easily use 5+ credits (2× standings, 2× squad, 1× h2h). If this is a high-traffic page, cache each team's standings and squad response for a few minutes server-side — these don't change match-to-match, so serving the same cached data to many visitors avoids redundant calls.

Frequently Asked Questions

Can I compare teams from different leagues?

Yes, /team_standings and /team_squad work independently per team regardless of league, though head-to-head data will only exist if the two teams have actually played each other (e.g. in a cup competition or friendly).

How do I find the right match_id for h2h if the teams aren't playing soon?

Use /team_matches for one of the teams, filter for matches against the other team's team_id, and use the most recent one's id as the match_id for the /h2h call.

Does squad size alone indicate depth quality?

No, it only reflects registered squad numbers — for a more meaningful depth comparison, combine it with individual player stats from the same /team_squad response rather than headcount alone.

How many credits does a full comparison page cost?

Roughly 5 credits per full page load (2 standings + 2 squad + 1 h2h calls), before any caching — see our pricing guide for strategies to reduce this at scale.

Artikel teilen:
← Zurück zum Blog