Build a PGA golfer's scorecard

A scorecard is one round broken into 18 holes. List a golfer's rounds, then pull the holes for the round you want.

  1. 1. List a golfer's rounds

    player_rounds has one row per round — score, round_number, and strokes-gained splits. Swap PLAYER_ID for an id from the pga/players endpoint.

    curl -sS \
      -H 'Authorization: Bearer YOUR_API_KEY' \
      'https://api.stat-api.com/api/v1/pga/player_rounds?player_id=PLAYER_ID'
  2. 2. Pull the hole-by-hole cards

    player_holes breaks each round into its holes — hole_number, score, to_par, and birdie/bogey flags. Filter by the same player_id to get every hole the golfer played, then group by round_number for each scorecard.

    curl -sS \
      -H 'Authorization: Bearer YOUR_API_KEY' \
      'https://api.stat-api.com/api/v1/pga/player_holes?player_id=PLAYER_ID'
  3. 3. Total a round

    Group the holes by round_number, sort each group by hole_number, and accumulate to_par for a running score relative to par.

    const headers = { Authorization: 'Bearer YOUR_API_KEY' }
    const base = 'https://api.stat-api.com/api/v1/pga'
    
    const holes = await fetch(`${base}/player_holes?player_id=PLAYER_ID`, { headers })
      .then(r => r.json())
    
    // One scorecard per round.
    const byRound = Map.groupBy(
      holes.player_holes,
      (h: { round_number: number }) => h.round_number,
    )
    for (const [round, card] of byRound) {
      card.sort((a: { hole_number: number }, b: { hole_number: number }) => a.hole_number - b.hole_number)
      const toPar = card.reduce((sum: number, h: { to_par: number }) => sum + h.to_par, 0)
      console.log(`Round ${round}: ${toPar > 0 ? '+' : ''}${toPar}`)
    }