Skip to main content

A Python wrapper for the official MLB Stats API (https://statsapi.mlb.com)

Project description

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.

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

baseball_api_wrapper-0.1.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

baseball_api_wrapper-0.1.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file baseball_api_wrapper-0.1.0.tar.gz.

File metadata

  • Download URL: baseball_api_wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for baseball_api_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6c4324dc284c4a9e136cda531d21d14fb1aeef278c42c2b7c5c8f2f9fda0d994
MD5 04bcb2fe5523e08090d9fd601c1ee008
BLAKE2b-256 19672a7e92bac7e639b27f49143baaa7ff95b822b8efcf6bf3dfb5ecde3018b8

See more details on using hashes here.

File details

Details for the file baseball_api_wrapper-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for baseball_api_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a73f87bb32b313bb120e8ad1cbba7f7f5e0b15f1f3510fb7e87aacc39e396574
MD5 f62c586082f7c581f819802ed6d6ad4a
BLAKE2b-256 2cbd77bdfe94d7160243f0faa005a5139f00caf95b91f7cf7184030173844d70

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page