STAT-API
MLB Cookbook

Filter MLB games by season

List endpoints accept whitelisted filter parameters. This example resolves the current season from the API (so it survives the annual rollover) and uses it to filter games.

1.Resolve the current season

List the seasons table and pick the one with the largest start_year — the example resolves this at run time, so it never goes stale.

const season = (await api.mlb.seasons.list({ limit: 200 })).seasons
  .reduce((cur, s) => (s.start_year > cur.start_year ? s : cur)).id

The complete program

Every step as one runnable file. It reads STAT_API_KEY from the environment, and is the same file that ships in the cookbook project for each SDK.

import { StatApi } from '@stat-api/client'

const api = new StatApi() // reads STAT_API_KEY from the environment

// Resolve the current season
const season = (await api.mlb.seasons.list({ limit: 200 })).seasons
  .reduce((cur, s) => (s.start_year > cur.start_year ? s : cur)).id

// Filter games to that season
const games = (await api.mlb.games.list({ season_id: season, limit: 5 })).games

// Print the filtered games
console.log(JSON.stringify(games, null, 2))