Client API Reference¶
The ChessComClient class is the main interface for interacting with the Chess.com API. This document provides detailed
information about all available methods and their usage.
ChessComClient¶
Constructor¶
def __init__(
self,
session: Optional[aiohttp.ClientSession] = None,
timeout: int = 30,
max_retries: int = 3,
rate_limit: int = 300
) -> None
Creates a new Chess.com API client instance.
Parameters¶
session(Optional[aiohttp.ClientSession]): Custom aiohttp session. If not provided, a new session will be created.timeout(int): Request timeout in seconds. Default: 30max_retries(int): Maximum number of retry attempts. Default: 3rate_limit(int): Maximum concurrent requests. Default: 300
Example¶
import aiohttp
from chess_com_api import ChessComClient
# Basic usage
client = ChessComClient()
# Custom configuration
session = aiohttp.ClientSession(
headers={"User-Agent": "MyApp/1.0"}
)
client = ChessComClient(
session=session,
timeout=60,
max_retries=5
)
Player Methods¶
get_player¶
Get profile information for a player.
Parameters¶
username(str): The username of the player
Returns¶
Player: Player profile information
Raises¶
NotFoundError: If the player doesn't existValidationError: If the username is invalid
Example¶
player = await client.get_player("hikaru")
print(f"Title: {player.title}")
print(f"Rating: {player.rating}")
get_player_stats¶
Get statistics for a player.
Parameters¶
username(str): The username of the player
Returns¶
PlayerStats: Player statistics including ratings for different game types
Example¶
get_player_current_games¶
Get a player's current games.
Parameters¶
username(str): The username of the player
Returns¶
List[Game]: List of current games
Example¶
games = await client.get_player_current_games("hikaru")
for game in games:
print(f"Game URL: {game.url}")
get_player_to_move_games¶
Get a player's games where it's their turn to move.
Parameters¶
username(str): The username of the player
Returns¶
List[DailyGame]: List of games where it's the player's turn
Tournament Methods¶
get_tournament¶
Get tournament details.
Parameters¶
url_id(str): The tournament's URL identifier
Returns¶
Tournament: Tournament information
Example¶
get_tournament_round¶
Get details for a specific tournament round.
Parameters¶
url_id(str): The tournament's URL identifierround_num(int): The round number
Returns¶
Round: Round information
Club Methods¶
get_club¶
Get club details.
Parameters¶
url_id(str): The club's URL identifier
Returns¶
Club: Club information
Example¶
club = await client.get_club("chess-com-developer-community")
print(f"Members: {club.members_count}")
get_club_members¶
Get club members.
Parameters¶
url_id(str): The club's URL identifier
Returns¶
Dict[str, List[str]]: Dictionary of member lists by category
Country Methods¶
get_country¶
Get country details.
Parameters¶
iso_code(str): The country's ISO code
Returns¶
Country: Country information
Example¶
get_country_players¶
Get players from a specific country.
Parameters¶
iso_code(str): The country's ISO code
Returns¶
List[str]: List of player usernames
get_country_clubs¶
Get clubs from a specific country.
Parameters¶
iso_code(str): The country's ISO code
Returns¶
CountryClubs: Country clubs information
Match Methods¶
get_match¶
Get team match details.
Parameters¶
match_id(int): The match identifier
Returns¶
Match: Match information
get_match_board¶
Get team match board details.
Parameters¶
match_id(int): The match identifierboard_num(int): The board number
Returns¶
Board: Board information
Puzzle Methods¶
get_daily_puzzle¶
Get the daily puzzle.
Returns¶
DailyPuzzle: Daily puzzle information
Example¶
puzzle = await client.get_daily_puzzle()
print(f"Title: {puzzle.title}")
print(f"FEN: {puzzle.fen}")
get_random_puzzle¶
Get a random puzzle.
Returns¶
DailyPuzzle: Random puzzle information
Streamer Methods¶
get_streamers¶
Get Chess.com streamers.
Returns¶
List[Streamer]: List of Chess.com streamers
Leaderboard Methods¶
get_leaderboards¶
Get Chess.com leaderboards.
Returns¶
Leaderboard: Leaderboard information for various game types
Helper Methods¶
close¶
Close the client session.
Example¶
Context Manager Support¶
The client supports the async context manager protocol:
Error Handling¶
All methods may raise the following exceptions:
NotFoundError: Resource not foundRateLimitError: Rate limit exceededValidationError: Invalid input parametersRedirectError: Resource movedGoneError: Resource no longer availableChessComAPIError: Base class for all API errors
Example error handling:
try:
player = await client.get_player("username")
except NotFoundError:
print("Player not found")
except RateLimitError:
print("Rate limit exceeded")
except ChessComAPIError as e:
print(f"API error: {e}")
Best Practices¶
-
Always use the context manager when possible:
-
Set appropriate timeouts for your use case:
-
Handle rate limits properly:
-
Set a descriptive User-Agent:
See Also¶
- Models Reference - Data model documentation
- Exceptions Reference - Error types documentation
- Rate Limiting Guide - Detailed rate limiting information