How to get NBA stat leaders from the API with Python
Create a free account, confirm your e-mail address and copy your API key from the dashboard. Send it in the X-API-Key header. You need Python 3 and the requests package (pip install requests). The leaders are at /api/basketball/leagues/nba/leaders. Choose the stat with stat: pts, reb, ast, stl, blk, tov, tpm, minutes, fg_pct, tp_pct or ft_pct. With one stat, data is a list:
With several stats separated by commas, as in the script below, data has one list per stat. This page gets all nine lists with one request.
leaders.py
import requests
API_KEY = "YOUR_API_KEY"
answer = requests.get(
"https://sportapi.ai/api/basketball/leagues/nba/leaders",
params={"stat": "pts,reb,ast", "limit": 10},
headers={"X-API-Key": API_KEY},
timeout=20,
).json()
for stat, rows in answer["data"].items():
print(stat)
for r in rows:
print(f'{r["rank"]:>3} {r["player"]["name"]:<24} {r["team"]["abbreviation"]:<5} {r["value"]}')
A player needs a minimum number of games to make a list: by default 40% of the most games played by any player. Change it with min_games. stage=playoffs gives the playoff leaders.
Keep your API key on the server, never in JavaScript that runs in the browser. Save each answer for a few minutes too, so that page views don't use up your daily limit. This site does both: a script saves the API answers to files, and the pages are built from those files.
The API documentation lists every basketball endpoint. The same key works for the NBA, WNBA, EuroLeague, NCAA and the other leagues, and for football too.