@balldontlie/sdk

中文文档
1.2.2发布于 2024/12/21MITTypeScriptCommonJS近 30 天下载 2013

Official TypeScript/JavaScript SDK for the balldontlie API

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. TypeScript/ES Modules
  4. CommonJS
  5. Example Usage
  6. NBA
  7. NFL
  8. MLB
  9. API Reference
  10. Error Handling

BALLDONTLIE API

Official TypeScript/JavaScript SDK for the BALLDONTLIE API. Access NBA, NFL, and MLB statistics and data. Check out the official website here.

Installation

# npm
npm install @balldontlie/sdk

# yarn
yarn add @balldontlie/sdk

# pnpm
pnpm add @balldontlie/sdk

Usage

TypeScript/ES Modules

import { BalldontlieAPI } from "@balldontlie/sdk";

const api = new BalldontlieAPI({ apiKey: "your-api-key" });

// Using async/await
async function getTeams() {
  try {
    const teams = await api.nba.getTeams();
    console.log(teams.data);
  } catch (error) {
    console.error(error);
  }
}

// Using promises
api.nba
  .getPlayers({ search: "James" })
  .then((response) => console.log(response.data))
  .catch((error) => console.error(error));

CommonJS

const { BalldontlieAPI } = require("@balldontlie/sdk");

const api = new BalldontlieAPI({ apiKey: "your-api-key" });

Example Usage

NBA

// Get all NBA teams
const teams = await api.nba.getTeams();

// Get a specific team
const team = await api.nba.getTeam(1);

// Search for players
const players = await api.nba.getPlayers({
  search: "James",
});

// Get season averages
const stats = await api.nba.getSeasonAverages({
  season: 2023,
  player_id: 1,
});

NFL

// Get NFL teams
const teams = await api.nfl.getTeams({ conference: "AFC" });

// Get player stats
const stats = await api.nfl.getStats({
  player_ids: [1],
  seasons: [2023],
});

// Get advanced stats
const rushingStats = await api.nfl.getAdvancedRushingStats({
  season: 2023,
  player_id: 1,
});

MLB

// Get MLB teams
const teams = await api.mlb.getTeams({ league: "American" });

// Get season stats
const seasonStats = await api.mlb.getSeasonStats({
  season: 2023,
  player_ids: [1],
});

// Get team season stats
const teamStats = await api.mlb.getTeamSeasonStats({
  season: 2023,
  team_id: 1,
});

API Reference

View the source code to see the types and function names: https://github.com/balldontlie-api/ts-sdk/tree/master/src

Check out the full API documentation: NBA NFL MLB

Error Handling

try {
  const data = await api.nba.getTeam(999999);
} catch (error) {
  if (error.status === 404) {
    console.error("Team not found");
  } else {
    console.error("API Error:", error.message);
  }
}

版本与发布记录

@balldontlie/sdk 共发布 8 个版本,首次发布于 2024/11/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.2latest2024/12/211.9 年前
1.1.22024/12/011.9 年前
1.1.12024/12/011.9 年前
1.1.02024/12/011.9 年前
1.0.32024/11/271.9 年前
1.0.22024/11/271.9 年前

关于 @balldontlie/sdk 的常见问题

@balldontlie/sdk 是什么?

Official TypeScript/JavaScript SDK for the balldontlie API

如何安装 @balldontlie/sdk?

在项目目录运行 npm install @balldontlie/sdk,或使用 pnpm add / yarn add / bun add。

@balldontlie/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@balldontlie/sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@balldontlie/sdk 可以商用吗?

@balldontlie/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@balldontlie/sdk 的下载量有多少?

根据 npm 官方下载统计,@balldontlie/sdk 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,013 次,日均约 67 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@balldontlie/sdk 还在维护吗?

最新版本 1.2.2 发布于 2024/12/21(1.9 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/11/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。