The first time someone tells you the league’s own data feed is free and open, you assume there must be a catch: a key to register for, a login, a rate-limit paywall waiting two requests in. There isn’t. Every time a scoreboard on MLB.com flips a score, a phone app refreshes a box score, or a broadcast graphic shows a hitter’s season line, the numbers come from one place: the MLB Stats API, a public JSON feed at statsapi.mlb.com. It is the same plumbing the league uses for its own products, it is wide open, and it never asks you for a key, a login, or a single dollar.
This is a from-scratch tutorial that uses nothing but Python’s requests library: no pybaseball, no scraping, no credentials. By the end you will have hit the live endpoint, walked the JSON it hands back, and charted the 2025 home-run leaders, the exact figure below, in about twenty lines. Then we will point the same technique at standings and the daily schedule, because once you can read one endpoint you can read them all.
One library, no key
The only dependency you truly need is requests, and you almost certainly already have it; if not, pip install requests takes a second. We will add matplotlib for the chart at the end. Here are the imports and the two constants that set everything up:
import requests
import matplotlib.pyplot as plt
BASE = "https://statsapi.mlb.com/api/v1/"
UA = {"User-Agent": "mlbanalytic-tutorial/1.0 (python-requests)"}
Two things are worth a word. The BASE URL ends in /api/v1/: every endpoint we touch hangs off that root. And UA is a User-Agent header that says plainly what is calling: a script, not a browser. The API asks for no key and has no robots.txt (it answered 404 when we checked on 2 November 2026), but naming your client honestly is good manners. You pass it along on every call. That is the entire setup; the rest is one request and a little dictionary-walking.
Hit the stats endpoint
The endpoint we want is stats, and the whole art of this API is in the query parameters you attach to it. We ask for season-level hitting stats for 2025, restricted to qualified hitters in the major leagues, and let requests assemble the query string for us via its params argument:
resp = requests.get(
BASE + "stats",
params={"stats": "season", "group": "hitting", "season": 2025,
"sportId": 1, "playerPool": "qualified", "limit": 300},
headers=UA, timeout=60)
resp.raise_for_status()
data = resp.json()
Read those parameters like a sentence. stats=season asks for full-season totals rather than a game log or career line. group=hitting selects the hitting stat group (swap in pitching or fielding and the same call returns those instead). season=2025 is the year. sportId=1 is the magic number for the major leagues: the same API serves the minors, college, and international ball under other sport IDs, so you must say which one you mean. playerPool=qualified keeps only hitters who cleared the plate-appearance threshold, and limit=300 makes sure the response is large enough to hold them all.
The two lines after the request matter as much as the request itself. raise_for_status() turns any HTTP error (a 404, a 500, a rate-limit) into a clean Python exception instead of letting a broken response slide silently into your code. And resp.json() parses the response body, which arrives as JSON, straight into Python dictionaries and lists. From here on we are just navigating that nested structure.
Walk the JSON
This is the step that intimidates newcomers and shouldn’t. The response is a nested object, but the path to the actual rows is short and always the same shape. The leaderboard lives under ["stats"][0]["splits"]: a list in which each element is one player’s line. Inside each split, the player’s name sits at ["player"]["fullName"] and his counting stats sit under ["stat"], keyed by name. We pull out the three fields we care about into a tidy list of plain dictionaries:
splits = data["stats"][0]["splits"]
recs = [{"player": s["player"]["fullName"],
"hr": int(s["stat"].get("homeRuns", 0)),
"ops": float(s["stat"].get("ops", 0) or 0)} for s in splits]
A couple of defensive habits are baked in here. We reach for stats with .get("homeRuns", 0) rather than ["homeRuns"] so that a player missing a field returns zero instead of crashing the loop, and we coerce each value with int(...) or float(...) because the rate stats come back as strings. In the response we saved on 2 November 2026, avg, obp and ops are strings such as ".948", while counts such as homeRuns and plateAppearances are already integers, which int() leaves unchanged. The or 0 on the OPS line guards against an empty string sneaking through. The result, recs, is now an ordinary list you can sort, filter, or print however you like: the data has officially escaped the API and become yours.
Sort and find the leaders
Sorting is one line. We order the records by home runs, highest first, and keep the top ten:
recs.sort(key=lambda r: r["hr"], reverse=True)
top = recs[:10]
The numbers that come back are a good sanity check that the whole pipeline worked. The 2025 home-run crown went to Cal Raleigh with 60 (a catcher leading the majors in homers, which is its own small piece of history), followed by Kyle Schwarber at 56 and Shohei Ohtani at 55. Aaron Judge sat fourth with 53 despite carrying a monstrous 1.145 OPS, far and away the best mark on the board. Here is the full top ten exactly as the script assembled it:
| Player | HR | OPS |
|---|---|---|
| Cal Raleigh | 60 | 0.948 |
| Kyle Schwarber | 56 | 0.928 |
| Shohei Ohtani | 55 | 1.014 |
| Aaron Judge | 53 | 1.145 |
| Eugenio Suárez | 49 | 0.824 |
| Junior Caminero | 45 | 0.846 |
| Juan Soto | 43 | 0.921 |
| Pete Alonso | 38 | 0.871 |
| Jo Adell | 37 | 0.778 |
| Riley Greene | 36 | 0.806 |
That table was not typed by hand; it is the same top list, written straight to the page. Notice how the home-run order and the OPS order disagree: Eugenio Suárez ranks fifth in homers on a relatively modest .824 OPS, while Ohtani’s 1.014 trails only Judge. That tension between raw power and all-around value is exactly the kind of thing you can now explore yourself, because every one of these stats came down in the same response.
Chart the top ten
A list is useful; a picture is persuasive. We draw a horizontal bar chart, which is the natural shape for a ranked list of names. The names and values get reversed with [::-1] because matplotlib draws its first bar at the bottom; reversing puts the leader on top, where a reader expects him:
fig, ax = plt.subplots(figsize=(8.0, 5.2))
names = [r["player"] for r in top][::-1]
vals = [r["hr"] for r in top][::-1]
ax.barh(range(len(names)), vals)
ax.set_yticks(range(len(names)))
ax.set_yticklabels(names, fontsize=10)
ax.set_xlabel("Home runs")
ax.set_title("2025 home-run leaders, via the MLB Stats API", loc="left")
for i, v in enumerate(vals):
ax.text(v + 0.4, i, str(v), va="center", fontsize=9)
plt.show()
The barh call draws the bars; the two set_yticks / set_yticklabels lines pin each player’s name to his bar. The little for loop is the finishing touch: it writes each home-run total as text just past the end of its bar, so the reader gets both the visual length and the exact number. Swap plt.show() for fig.savefig("hr_leaders.png") if you would rather write the chart to a file. Run it and you get this:
The same trick, two more endpoints
The real reward is that every corner of this API answers to the same pattern: build a URL off BASE, attach parameters, call .json(), walk the result. Two endpoints earn a spot in any toolkit.
The first is standings, which returns the division standings for a given season. You tell it which leagues you want with leagueId (103 is the American League and 104 is the National League) and which kind of standings with standingsTypes:
resp = requests.get(
BASE + "standings",
params={"leagueId": "103,104", "season": 2025,
"standingsTypes": "regularSeason"},
headers=UA, timeout=60)
records = resp.json()["records"] # one entry per division
Each element of records is a division, and inside it teamRecords lists the clubs in order with wins, losses, games back, and run differential. That is a full standings page in two lines of fetching.
The second is schedule, the endpoint behind every “today’s games” scoreboard. Hand it a date and a sportId and it returns that day’s slate, matchups, start times, venues, and, for finished games, the line score:
resp = requests.get(
BASE + "schedule",
params={"sportId": 1, "date": "2025-07-04"},
headers=UA, timeout=60)
games = resp.json()["dates"][0]["games"] # every game on that date
This is the exact feed that powers public MLB scoreboards. It returns clean JSON, it needs no key, and (the part that delights historians) its coverage reaches back more than a century, into the early 1900s. Point the season or date parameters at a year from the dead-ball era and the API hands you that history in the same format as last night’s box score. When we asked for 4 July 1905, it returned 16 games.
Eight traps we hit running this code
On 2 November 2026 we ran the listings on this page against the live API, as printed apart from the User-Agent line, and saved every response. They ran clean: 145 qualified hitters, the same top ten as the table above, 6 divisions and 15 games on 4 July 2025. Then we pushed each endpoint a little further. Here is what tripped us, with the numbers counted from the saved responses.
1. An off day crashes the schedule listing
The schedule listing reads ["dates"][0]. On a day without games the API answers with totalGames 0 and an empty dates list, so there is no first element. We pointed the listing at 2 November 2026, an off day, and this is the last line it printed:
IndexError: list index out of range
Flatten the list instead, which gives an empty list on an off day:
games = [g for d in resp.json()["dates"] for g in d["games"]]
2. Leave out limit and you get 50 rows
The same stats call without limit returned 50 hitters, with no hint that more exist. With limit=300 it returned all 145. Set limit above the count you expect, then check len(splits).
3. “Qualified” is not one list
playerPool=qualified returned 145 hitters for 2025. Baseball Savant’s min=q expected-stats leaderboard for the same season has 251, and all 145 of the API’s hitters are among them. Check the threshold before you compare two leaderboards.
4. One game can appear twice in the schedule
The full 2026 regular-season schedule (gameType=R) has 2,459 entries but only 2,430 distinct gamePk values. A postponed game keeps its gamePk on its makeup date, so 28 games appear once as Postponed and again as Final. One suspended game (San Francisco Giants at Atlanta Braves) appears twice as Final, once with its 16 June start and once with its 17 June resumption. One game, on 27 September, was cancelled (reason: rain) and never played. All 2,459 entries, the postponed and cancelled ones included, report abstractGameState Final. Counting entries whose codedGameState is F gives 2,430, which happens to equal the 2,430 games on the schedule, yet only 2,429 distinct games were played. Drop Postponed and Cancelled entries, then count distinct gamePk values.
5. Without gameType, the schedule is everything
Leave gameType out of the 2026 season schedule and it grows from 2,459 entries to 2,990: 451 spring-training entries, 38 exhibitions, 41 postseason entries and 1 All-Star Game come along with the regular season. Pass gameType=R when you mean the regular season.
6. Standings without leagueId fail silently
standings?season=2025 with no leagueId returned HTTP 200 and "records": []. There was no error to catch, just nothing. With leagueId=103,104 it returned 6 divisions and 30 teams.
7. standings?date= includes that day’s games
We asked for the standings as of date=2026-07-01 and compared each club’s wins plus losses with the decided games in the schedule. All 30 matched the games played through that date. Only 2 matched the games played before it: the clubs that were idle that day. For “standings entering a date,” ask for the day before.
8. sportId=1 isn’t only the American and National Leagues
teams?sportId=1&season=1937 returned 32 clubs: the 16 of the American and National Leagues plus 9 from the Negro American League and 7 from the Negro National League. The API treats all of them as major-league clubs, so filter on league.id 103 and 104 when you mean the AL and NL.
How we ran it
One script ran the listings above unchanged except for the User-Agent line, and saved every response. This page used to show a browser-style User-Agent; it now shows the plain one we sent. A second script sent the other requests in this section. All of them except trap 3’s Savant export went to statsapi.mlb.com, which has no robots.txt (it answered 404). Requests were at least a second apart, all on 2 November 2026. Each response carries MLB Advanced Media’s notice, which permits only individual, non-commercial, non-bulk use of the data.
Where this fits, and where to go next
Raw API access is the foundation, not the ceiling. When you want season leaderboards and Statcast pitch-by-pitch wrapped in friendlier function calls, the companion piece on getting started with pybaseball builds on exactly this groundwork: pybaseball is, under the hood, calling endpoints like these for you. And if you want to know how this feed stacks up against Baseball Savant, FanGraphs, and Retrosheet for serious work, our roundup of free public baseball data sources, ranked places it in context.
The bottom line
The distance between “I wonder if I could get that number myself” and a live chart of the home-run leaders is, it turns out, one requests.get, a short walk through a JSON dictionary, and no API key at all. The MLB Stats API is the league’s own data, served plainly and for free, with a century of baseball behind it. Learn the one pattern (URL, params, .json(), walk) and stats, standings, and schedules all open up the same way.
Sources & Further Reading
- Leaderboard data: the MLB Stats API (
statsapi.mlb.com, hitting leaders for 2025). Numbers retrieved June 2026; computed for this article from that source. Re-run on 2 November 2026 with the same top ten. - The eight traps: our requests to
statsapi.mlb.comon 2 November 2026, responses saved, and Baseball Savant’s 2025 expected-statistics export for trap 3. Every Stats API response links MLB Advanced Media’s terms notice (gdx.mlb.com/components/copyright.txt). - Baseball Savant: MLB’s Statcast site, fed by the same league data infrastructure.
- Baseball-Reference: for cross-checking season totals against an independent record.