A Python 3 wrapper for the MLB StatsAPI at statsapi.mlb.com
Project description
mlbapi
mlbapi is a Python library that provides Pythonic bindings for MLB Advanced Media's StatsAPI — the same data source that powers MLB.com's live game data, box scores, standings, and more.
Unlike raw API calls, mlbapi returns structured Python objects so you can work with MLB data naturally in your code.
Features
- Schedule lookups by team and date range
- Box score data including batting and pitching stats
- Live linescore data (inning, batter, pitcher, count)
- Structured Python objects from every endpoint — no manual JSON parsing
- Lightweight: only requires
requests
Installation
pip install mlbapi
Quick Start
Get a team's schedule for a date range
import mlbapi
# Houston Astros schedule for the 2024 season
schedule = mlbapi.schedule(start_date='04/01/2024', end_date='10/01/2024', team_id=117)
Get a team's schedule for a single date
import mlbapi
schedule = mlbapi.schedule(date='04/01/2024', team_id=117)
Get box score data for a game
import mlbapi
schedule = mlbapi.schedule(date='04/01/2024', team_id=117)
game_pk = schedule.dates[0].games[0].game_pk
boxscore = mlbapi.boxscore(game_pk)
# Print all game info (weather, attendance, venue, etc.)
for info in boxscore.info:
print(info.info)
# Print batting stats for both teams
print(boxscore.teams.away.team_stats.batting.__dict__)
print(boxscore.teams.home.team_stats.batting.__dict__)
Get live linescore data
import mlbapi
schedule = mlbapi.schedule(date='04/01/2024', team_id=117)
game_pk = schedule.dates[0].games[0].game_pk
line = mlbapi.linescore(game_pk)
output = '{} of the {}, {} facing {}. {} ball(s), {} strike(s), {} out(s)'
print(output.format(
line.inning_half,
line.current_inning_ordinal,
line.offense.batter.full_name,
line.defense.pitcher.full_name,
line.balls,
line.strikes,
line.outs
))
# Top of the 9th, Scott Van Slyke facing Framber Valdez. 0 ball(s), 3 strike(s), 3 out(s)
Team IDs
Common MLB team IDs for reference:
| Team | ID | Team | ID |
|---|---|---|---|
| Yankees | 147 | Dodgers | 119 |
| Red Sox | 111 | Giants | 137 |
| Astros | 117 | Cubs | 112 |
| Braves | 144 | Cardinals | 138 |
| Mets | 121 | Padres | 135 |
A full list is available via mlbapi.teams().
Documentation
Full documentation and endpoint reference coming soon. In the meantime, feel free to open an issue with questions.
Contributing
Contributions are welcome! Please read CONTRIBUTING.md before submitting a pull request. Bug reports and feature requests can be filed as GitHub Issues.
Support the Project
If mlbapi saves you time, consider supporting its development:
- ⭐ Star this repo — it helps others discover the project
- 💛 Sponsor on GitHub
- ☕ Donate via PayPal
License
This project is licensed under the terms found in LICENSE.
Disclaimer
This library is not affiliated with or endorsed by Major League Baseball or MLB Advanced Media. Use of the MLB StatsAPI is subject to MLB's terms of service.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlbapi-0.2.2.tar.gz.
File metadata
- Download URL: mlbapi-0.2.2.tar.gz
- Upload date:
- Size: 37.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e7feceb5682d650ed963ffd2fc79e8dddd1ddf4929e33a6c3f6760fff72da2a
|
|
| MD5 |
f224dddf3c3383645cee14a91d788613
|
|
| BLAKE2b-256 |
db0783737ea2f792810763f7d45aa052450a50b2734e5a155647696d3aa486f3
|
Provenance
The following attestation bundles were made for mlbapi-0.2.2.tar.gz:
Publisher:
publish-to-pypi.yml on trevor-viljoen/mlbapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlbapi-0.2.2.tar.gz -
Subject digest:
2e7feceb5682d650ed963ffd2fc79e8dddd1ddf4929e33a6c3f6760fff72da2a - Sigstore transparency entry: 1203482653
- Sigstore integration time:
-
Permalink:
trevor-viljoen/mlbapi@0117f178bd48e8040796ecb229b24c0970ea2a2f -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/trevor-viljoen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@0117f178bd48e8040796ecb229b24c0970ea2a2f -
Trigger Event:
release
-
Statement type:
File details
Details for the file mlbapi-0.2.2-py3-none-any.whl.
File metadata
- Download URL: mlbapi-0.2.2-py3-none-any.whl
- Upload date:
- Size: 39.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e7b53d1421c59cd22e7a3d3112fcb734c274d6c50dcd9cb898d439645dbc0c4
|
|
| MD5 |
60af23dd0d71fc912ef58af1c347657b
|
|
| BLAKE2b-256 |
b33cd6d872aae49e5c8c9787f17438f38b8c2f7be74dcb163e1303aaa106feb4
|
Provenance
The following attestation bundles were made for mlbapi-0.2.2-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on trevor-viljoen/mlbapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mlbapi-0.2.2-py3-none-any.whl -
Subject digest:
0e7b53d1421c59cd22e7a3d3112fcb734c274d6c50dcd9cb898d439645dbc0c4 - Sigstore transparency entry: 1203482656
- Sigstore integration time:
-
Permalink:
trevor-viljoen/mlbapi@0117f178bd48e8040796ecb229b24c0970ea2a2f -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/trevor-viljoen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@0117f178bd48e8040796ecb229b24c0970ea2a2f -
Trigger Event:
release
-
Statement type: