MLB Stats API

raw JSON →
1.9.0 verified Sat May 09 auth: no python

A Python wrapper for the MLB Stats API, providing easy access to MLB data including schedules, standings, player stats, and more. Version 1.9.0 is the latest stable release. Updates are frequent, with several releases per year.

pip install mlb-statsapi
error ModuleNotFoundError: No module named 'mlb_statsapi'
cause Incorrect import path; using underscore instead of the correct module name 'statsapi'.
fix
Change import to 'import statsapi'.
error KeyError: 'teams'
cause Schedule function expects certain keys in the API response; missing data for unplayed games or incomplete schedule.
fix
Check if game is complete before accessing team data, or upgrade to latest version.
error AttributeError: module 'statsapi' has no attribute 'game_uniforms'
cause Attempting to use game_uniforms endpoint that was added in v1.9.0.
fix
Upgrade to mlb-statsapi>=1.9.0.
gotcha The module to import is 'statsapi', not 'mlb_statsapi'. The package name on PyPI is 'mlb-statsapi', but after installation, you use 'import statsapi'.
fix Use 'import statsapi' in your code.
gotcha The schedule function may raise a KeyError if game data is missing (e.g., no team score or name). This is partially fixed in v1.6, but edge cases remain.
fix Upgrade to v1.6.1+ or wrap in try/except.
breaking The 'startDate' and 'endDate' parameters for stats endpoint were added in v1.8.1. Older versions do not support these parameters.
fix Upgrade to v1.8.1+ if you need date filtering for stats.
deprecated The 'get()' method's 'params' parameter now defaults to empty dict (v1.8+). Previously it required explicit passing. Code relying on old default may break.
fix Ensure you pass params as a dict or rely on new default.

Quick example of common operations: schedule, player lookup, and standings.

import statsapi

# Get today's schedule
schedule = statsapi.schedule()
print(schedule)

# Get player info
player = statsapi.lookup_player('Mike Trout')
print(player)

# Get standings for a specific division
standings = statsapi.standings_data(divisionId=200)
print(standings.keys())