Skip to main content

baseball-api-wrapper

A Python wrapper for the official MLB Stats API.

Installation

Clone the repo and install in editable mode so changes to the source are reflected immediately:

git clone <your-repo-url>
cd baseball-api-wrapper
pip install -e ".[dev]"   # includes pytest and test dependencies

For use in another project without dev tools:

pip install -e /path/to/baseball-api-wrapper

Quick Start

from baseball_api_wrapper import get_teams, get_roster

# List all MLB teams for the 2024 season
teams = get_teams(2024)
for team in teams:
    print(team["id"], team["name"])

# Get the active roster for the New York Yankees (teamId=147)
roster = get_roster(147, season=2024)
for player in roster:
    print(player["person"]["fullName"], player["position"]["name"])

Using the Low-Level Client

For endpoints not yet covered by a module, use MLBStatsClient directly:

from baseball_api_wrapper import MLBStatsClient

with MLBStatsClient() as client:
    data = client.get("/api/v1/standings", params={"leagueId": 103, "season": 2024})
    print(data)

Project Structure

baseball_api_wrapper/          # Installable package
    __init__.py        # Public API surface
    client.py          # Low-level HTTP client (MLBStatsClient, MLBStatsAPIError)
    modules/
        teams.py       # get_teams()
        roster.py      # get_roster()
tests/                 # pytest test suite
    test_teams.py
    test_roster.py
pyproject.toml         # Build & dependency config

Running Tests

pytest                        # Run all tests
pytest --cov=baseball_api_wrapper     # Run with coverage report

Error Handling

Network or API errors raise MLBStatsAPIError:

from baseball_api_wrapper import MLBStatsClient, MLBStatsAPIError

try:
    with MLBStatsClient() as client:
        data = client.get("/api/v1/teams/99999")  # Non-existent team
except MLBStatsAPIError as e:
    print(f"API error {e.status_code}: {e.message}")

License

MIT — see LICENSE for details.

Metadata

Release files for baseball-api-wrapper 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for baseball-api-wrapper 0.1.0
File Size Uploaded
baseball_api_wrapper-0.1.0.tar.gz 17.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for baseball-api-wrapper 0.1.0
File Interpreter ABI Platform
baseball_api_wrapper-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.7 kB

Release files / baseball_api_wrapper-0.1.0.tar.gz

Download URL baseball_api_wrapper-0.1.0.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6c4324dc284c4a9e136cda531d21d14fb1aeef278c42c2b7c5c8f2f9fda0d994
BLAKE2b-256 checksum
How to use checksums
19672a7e92bac7e639b27f49143baaa7ff95b822b8efcf6bf3dfb5ecde3018b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / baseball_api_wrapper-0.1.0-py3-none-any.whl

Download URL baseball_api_wrapper-0.1.0-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a73f87bb32b313bb120e8ad1cbba7f7f5e0b15f1f3510fb7e87aacc39e396574
BLAKE2b-256 checksum
How to use checksums
2cbd77bdfe94d7160243f0faa005a5139f00caf95b91f7cf7184030173844d70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page