npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

apibaseball

v0.2.0

Published

TypeScript SDK for the API Baseball

Readme

apibaseball

TypeScript SDK for the API Baseball REST API. Zero runtime dependencies — uses native fetch (Node 18+ and browsers).

Install

npm install apibaseball

Requires Node.js 18+ (or any environment with a global fetch).

Quick start

import { BaseballClient } from "apibaseball";

const client = new BaseballClient({
  apiKey: process.env.APIBASEBALL_KEY!,
});

const leagues = await client.getLeagues({ page: 1 });
const games = await client.getGames({
  from: "2026-07-01",
  to: "2026-07-07",
});

Authentication

Pass your API key when constructing the client. By default it is sent as the X-Api-Key header.

const client = new BaseballClient({
  apiKey: "your-api-key",
});

To send the key as a query parameter (api_key) instead:

const client = new BaseballClient({
  apiKey: "your-api-key",
  authIn: "query",
});

Configuration

| Option | Type | Default | Description | | --- | --- | --- | --- | | apiKey | string | — | Required. Your API key. | | baseUrl | string | https://api.api-baseball.com/api | API base URL. | | authIn | "header" | "query" | "header" | Where to send the API key (X-Api-Key or api_key). | | fetch | typeof fetch | global fetch | Custom fetch implementation (useful for tests or polyfills). | | timeoutMs | number | disabled | Request timeout in milliseconds. 0 or omit to disable. |

API methods

All methods return the unwrapped result (or a Paginated<T> for paginated endpoints).

| Method | Endpoint | | --- | --- | | getCountries() | GET /baseball/countries | | getLeagues({ itemsPerPage?, page? }) | GET /baseball/leagues | | getLeaguesByCountry(countryId) | GET /baseball/countries/{countryId}/leagues | | getTeamsByLeague(leagueId) | GET /baseball/leagues/{leagueId}/teams | | getPlayers({ teamId?, leagueId? }) | GET /baseball/players | | getGames({ from, to, leagueId?, teamId?, itemsPerPage?, page? }) | GET /baseball/games | | getLiveGames() | GET /baseball/live | | getTeamInjuries(teamId) | GET /baseball/teams/{team_id}/injuries | | getTeamStats(teamId) | GET /baseball/teams/{team_id}/stats | | getOdds({ from, to, gameId? }) | GET /baseball/odds | | getStandings(leagueId) | GET /baseball/leagues/{league_id}/standings |

Examples

// Countries & leagues
const countries = await client.getCountries();
const { result: leagues, pagination } = await client.getLeagues({
  page: 1,
  itemsPerPage: 50,
});
const usLeagues = await client.getLeaguesByCountry(countries[0].id);

// Teams & players
const teams = await client.getTeamsByLeague(usLeagues[0].id);
const players = await client.getPlayers({ teamId: teams[0].id });

// Games (from/to required; max 6 months range)
const { result: games } = await client.getGames({
  from: "2026-07-01",
  to: "2026-07-07",
  leagueId: usLeagues[0].id,
});

// Live games
const live = await client.getLiveGames();

// Team injuries & stats
const injuries = await client.getTeamInjuries(teams[0].id);
const stats = await client.getTeamStats(teams[0].id);

// Odds (from/to required; max 5 days range)
const odds = await client.getOdds({
  from: "2026-07-01",
  to: "2026-07-05",
});

// Standings
const standings = await client.getStandings(usLeagues[0].id);

Pagination

Paginated methods (getLeagues, getGames) return:

type Paginated<T> = {
  result: T;
  pagination: {
    current_page: number;
    per_page: number;
    total: number;
    last_page: number;
    from: number | null;
    to: number | null;
  };
};

For getLeagues, the API currently ignores itemsPerPage (including "*") and always returns 15 leagues per page. To fetch every league, iterate page up to pagination.last_page:

const leagues = [];
for (let page = 1; ; page++) {
  const { result, pagination } = await client.getLeagues({ page });
  leagues.push(...result);
  if (page >= pagination.last_page) break;
}

getOdds accepts a date range of at most 5 days; longer ranges are rejected by the API with a BaseballApiError.

Error handling

Failed requests throw BaseballApiError when:

  • HTTP status is >= 400
  • The response envelope has success !== 1
  • The request times out (if timeoutMs is set)
  • The body is not valid JSON
import { BaseballClient, BaseballApiError } from "apibaseball";

try {
  await client.getGames({ from: "2026-07-01", to: "2026-07-07" });
} catch (err) {
  if (err instanceof BaseballApiError) {
    console.error(err.message, err.status, err.code);
  }
  throw err;
}

| Property | Description | | --- | --- | | status | HTTP status (0 if the request never completed, e.g. timeout). | | code | Optional API error code from the response body. | | body | Raw response body when available. |

Example

examples/dashboard is a runnable Express + browser dashboard that exercises every SDK method — see its README for setup.

Development

npm install
npm run build      # ESM + CJS + .d.ts via tsup
npm run typecheck  # tsc --noEmit
npm test           # vitest

License

MIT