Build a MLB box score

A box score is one game plus the per-player stat lines for both teams. Find a game, fetch its player stats, and join them back to the players and teams.

  1. 1. Find a game

    List games for a season to get the game id you want a box score for. Each row carries home_team_id / away_team_id and the final score.

    curl -sS \
      -H 'Authorization: Bearer YOUR_API_KEY' \
      'https://api.stat-api.com/api/v1/mlb/games?season_id=2026&limit=20'
  2. 2. Pull per-player stats for that game

    game_player_batter_stats is keyed by game_id — one row per player who appeared. Each row carries team_id and player_id foreign keys. Swap GAME_ID for an id from step 1.

    curl -sS \
      -H 'Authorization: Bearer YOUR_API_KEY' \
      'https://api.stat-api.com/api/v1/mlb/game_player_batter_stats?game_id=GAME_ID'
  3. 3. Join players and teams

    Group the stat lines by team_id to render the two halves of the box score; resolve player_id and team_id to names with the lookups in the Data Dictionary.

    const headers = { Authorization: 'Bearer YOUR_API_KEY' }
    const base = 'https://api.stat-api.com/api/v1/mlb'
    
    const stats = await fetch(`${base}/game_player_batter_stats?game_id=GAME_ID`, { headers })
      .then(r => r.json())
    
    // Group the stat lines by team to render both halves of the box score.
    const byTeam = Map.groupBy(
      stats.game_player_batter_stats,
      (s: { team_id: number }) => s.team_id,
    )