SleeperAPI is a TypeScript client library for interacting with the Sleeper API. It provides a comprehensive set of methods to fetch and manage data related to users, leagues, rosters, matchups, drafts, players, and more. Whether you're building a fantasy sports application or automating league management tasks, SleeperAPI offers a robust and type-safe way to integrate with Sleeper's platform.
- SleeperAPI
- TypeScript Support: Fully typed interfaces and classes for type-safe development.
- Comprehensive Coverage: Access to users, leagues, rosters, matchups, drafts, players, and more.
- Customizable Axios Instance: Option to provide a custom Axios instance for advanced configurations.
- Error Handling: Graceful handling of API errors with meaningful messages.
-
Utility Functions: Helper functions like
getAvatarUrl
for common tasks.
You can install SleeperAPI using Bun, npm, or Yarn.
bun add sleeperapi axios
npm install sleeperapi axios
yarn add sleeperapi axios
Note: This package depends on axios
. Ensure it's installed in your project.
Import the SleeperAPI
class and create an instance. You can optionally provide a custom Axios instance if you need to customize request configurations.
import SleeperAPI from 'sleeperapi';
import axios from 'axios';
// Optional: Create a custom Axios instance
const customAxios = axios.create({
baseURL: 'https://api.sleeper.app/v1',
timeout: 15000, // 15 seconds timeout
});
// Initialize SleeperAPI with the custom Axios instance
const sleeper = new SleeperAPI(customAxios);
// Or initialize with default settings
const sleeperDefault = new SleeperAPI();
import SleeperAPI from 'sleeperapi';
const sleeper = new SleeperAPI();
async function fetchUser() {
try {
const user = await sleeper.getUserByUsername('john_doe');
console.log(user);
} catch (error) {
console.error(error.message);
}
}
fetchUser();
import SleeperAPI from 'sleeperapi';
const sleeper = new SleeperAPI();
async function fetchLeagues() {
try {
const leagues = await sleeper.getLeaguesForUser('user_id_123', 'nfl', '2023');
console.log(leagues);
} catch (error) {
console.error(error.message);
}
}
fetchLeagues();
import SleeperAPI from 'sleeperapi';
const sleeper = new SleeperAPI();
async function fetchPlayers() {
try {
const players = await sleeper.getAllPlayers('nfl');
console.log(players);
} catch (error) {
console.error(error.message);
}
}
fetchPlayers();
-
getUserByUsername(username: string): Promise<User>
- Fetch a user by their username.
-
getUserById(userId: string): Promise<User>
- Fetch a user by their user ID.
-
getLeaguesForUser(userId: string, sport: string, season: string): Promise<League[]>
- Retrieve all leagues for a specific user.
-
getLeague(leagueId: string): Promise<League>
- Get details of a specific league.
-
getRosters(leagueId: string): Promise<Roster[]>
- Fetch all rosters within a league.
-
getUsersInLeague(leagueId: string): Promise<User[]>
- Get all users participating in a league.
-
getMatchups(leagueId: string, week: number): Promise<Matchup[]>
- Retrieve all matchups for a given week in a league.
-
getWinnersBracket(leagueId: string): Promise<BracketMatchup[]>
- Get the winners bracket of a league.
-
getLosersBracket(leagueId: string): Promise<BracketMatchup[]>
- Get the losers bracket of a league.
-
getTransactions(leagueId: string, round: number): Promise<Transaction[]>
- Fetch all transactions for a specific round in a league.
-
getTradedPicks(leagueId: string): Promise<DraftPick[]>
- Retrieve all traded draft picks in a league.
(Similar methods related to rosters can be documented here if applicable.)
(Similar methods related to matchups can be documented here if applicable.)
-
getDraftsForUser(userId: string, sport: string, season: string): Promise<Draft[]>
- Fetch all drafts associated with a user.
-
getDraftsForLeague(leagueId: string): Promise<Draft[]>
- Retrieve all drafts within a league.
-
getDraft(draftId: string): Promise<Draft>
- Get details of a specific draft.
-
getPicksInDraft(draftId: string): Promise<Pick[]>
- Fetch all picks in a draft.
-
getTradedPicksInDraft(draftId: string): Promise<DraftPick[]>
- Retrieve all traded picks in a draft.
-
getAllPlayers(sport: string): Promise<{ [playerId: string]: Player }>
- Fetch all players for a specific sport.
-
getTrendingPlayers(sport: string, type: 'add' | 'drop', lookbackHours?: number, limit?: number): Promise<TrendingPlayer[]>
- Get trending players based on recent activity.
SleeperAPI handles errors gracefully by catching them and throwing meaningful error messages. Ensure to use try-catch
blocks when making asynchronous calls to handle potential errors.
try {
const user = await sleeper.getUserByUsername('invalid_username');
} catch (error) {
console.error(error.message); // Outputs a user-friendly error message
}
Common error messages include:
- Bad Request: Invalid request parameters.
- Not Found: Resource does not exist.
- Too Many Requests: Rate limiting in effect.
- Internal Server Error: Server-side issues.
- Service Unavailable: Service is temporarily offline.
- No Response: No response received from the server.
- Unexpected Error: Other unforeseen errors.
Contributions are welcome! If you'd like to contribute to SleeperAPI, please follow these steps:
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeature
-
Commit Your Changes
git commit -m "Add some feature"
-
Push to the Branch
git push origin feature/YourFeature
-
Open a Pull Request
Please ensure your code follows the project's coding standards and includes relevant tests.
This project is licensed under the MIT License.
Note: Replace https://img.shields.io/npm/l/sleeperapi
, https://img.shields.io/npm/v/sleeperapi
, and https://img.shields.io/npm/dm/sleeperapi
with actual badge URLs if you have set up badges for your package. Also, ensure to create a LICENSE
file in your repository if you haven't already.
If you encounter any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.