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 🙏

© 2024 – Pkg Stats / Ryan Hefner

chess-web-api

v1.2.0

Published

chess.com api wrapper

Downloads

144

Readme

Overview

chess-web-api is a lightweight, unofficial wrapper for the Chess.com public data API.

It includes helper functions for all available endpoints, such as retrieving player data, current daily chess games, monthly archives, club data, tournaments and more.

Please read Chess.com's notes on data currency, language and rate limits on parallel requests before you implement this into your work.

I'll try to keep this library updated if they're API changes, feel free to submit any issues.

All endpoint descriptions were copied from here. It's here for ease of access only. Please go to the site to ensure the return values have not been changed.

Documentation

Installation

Install via node:

$ npm i chess-web-api

Import the module and instantiate the wrapper.

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI();

You are then free to call any of the methods!

chessAPI.getPlayer('andyruwruw')
    .then(function(response) {
        console.log('Player Profile', response.body);
    }, function(err) {
        console.error(err);
    });

getPlayer(username, options, callback)

Get additional details about a player in a game.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerStats(username, options, callback)

Get ratings, win/loss, and other stats about a player's game play, tactics, lessons and Puzzle Rush score.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerOnline(username, options, callback)

This endpoint was removed by Chess.com 8/25/2021. For more information see the forum post.

This endpoint requires a large number of internal resources to maintain, and in the past week it has been used by only one clear developer script with a user-agent that helps us contact the developer. All other access to that endpoint appears to be tests, runaway scripts, or unidentified programs that could work better.

For those looking for a simular endpoint, Player Profile includes a last_online field.

getPlayerCurrentDailyChess(username, options, callback)

Array of Daily Chess games that a player is currently playing.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerToMoveDailyChess(username, options, callback)

Array of Daily Chess games where it is the player's turn to act.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerMonthlyArchives(username, options, callback)

Array of monthly archives available for this player.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerCompleteMonthlyArchives(username, year, month, options, callback)

Array of Live and Daily Chess games that a player has finished.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | year | string / number | Year of matches. | | month | string / number | Month of matches. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerMultiGamePGN(username, year, month, options, callback)

Standard multi-game format PGN containing all games for a month.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | year | string / number | Year of matches. | | month | string / number | Month of matches. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerClubs(username, options, callback)

List of clubs the player is a member of, with joined date and last activity date.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerMatches(username, options, callback)

List of Team matches the player has attended, is partecipating or is currently registered.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getPlayerTournaments(username, options, callback)

List of tournaments the player is registered, is attending or has attended in the past.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | username | string | Username of desired profile. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getGameByID(id, options, callback)

Game data lookup by ID. Includes PGN, metadata, and player info.

For more information on what fields this generates or how it was implemented, see here.

Read Before Using: Fair Usage

chess-web-api's getGameById method is not an official endpoint of Chess.com's Published Data API. It uses a callback from Chess.com's website to get its data.

Therefore it is highly unstable and could be changed without warning or stop functioning. Hammering this endpoint with requests could result in an IP ban from Chess.com. This would not only impact your script, but your access to Chess.com itself.

The use of this endpoint violates Chess.com's Terms of Service, however Chess.com has informed me that as long as the "script is polite", they have no issue and it will go unnoticed. Corrective action will be taken if there is abuse.

There is work to add a simular endpoint to Chess.com, and this library will be updated when that takes place. There is no current timeline there and it could take up to a year.

Please submit an issue if you notice any strange behavior to improve this documentation or make fixes. I'll periodically check to make sure the endpoint is working as intended and make fixes when needed.

Parameters

| Name | Type | Description | |----------|--------------|------------------------------------------------| | id | string | The game ID. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getClub(urlID, options, callback)

Get additional details about a club.

All club-based URLs use the club's "URL ID" to specify which club you want data for.

The url-ID is the same as found in the URL for the club's web page on www.chess.com. For example, the url-ID of the Chess.com Developer's Club is chess-com-developer-community.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | urlID | string | Club's unique urlID | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getClubMembers(urlID, options, callback)

List of club members (usernames and joined date timestamp), grouped by club-activity frequency. The club-activity is one of this actions.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | urlID | string | Club's unique urlID | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getClubMatches(urlID, options, callback)

List of daily and club matches, grouped by status (registered, in progress, finished).

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | urlID | string | Club's unique urlID | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTournament(urlID, options, callback)

Get details about a daily, live and arena tournament.

All tournaments-based URLs use the tournament's "URL ID" to specify which tournament you want data for.

The url-ID is the same as found in the URL for the tournament's web page on www.chess.com. For example, the url-ID of the Chess.com Developer's Club is -33rd-chesscom-quick-knockouts-1401-1600

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | urlID | string | Tournaments's unique urlID | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTournamentRound(urlID, round, options, callback)

Get details about a tournament's round.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | urlID | string | Tournaments's unique urlID | | round | string / number | Round number | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTournamentRoundGroup(urlID, round, group, options, callback)

Get details about a tournament's round group.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | urlID | string | Tournaments's unique urlID | | round | string / number | Round number | | group | string / number | Group number | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTeamMatch(id, options, callback)

Get details about a team match and players playing that match. After the match is finished there will be a link to each player's stats endpoint, in order to get up-to-date information about the player.

All team matches-based URLs use the match "ID" to specify which match you want data for.

The ID is the same as found in the URL for the team match web page on www.chess.com. For example, the ID WORLD LEAGUE Round 5: Romania vs USA Southwest is 12803.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | id | string / number | Id of desired team match. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTeamMatchBoard(id, board, options, callback)

Get details about a team match board. Only in-progress or finished games will be included, so there may be one or two games in this list.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | id | string / number | Id of desired team match. | | board | string / number | Board identifier | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTeamLiveMatch(id, options, callback)

Get details about a team match and players playing that match. After the match is finished there will be a link to each player's stats endpoint, in order to get up-to-date information about the player.

All live team matches-based URLs use the match "ID" to specify which match you want data for.

The ID is the same as found in the URL for the team match web page on www.chess.com. For example, the ID Friendly 5+2 is 5833.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | id | string / number | Id of desired live team match. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTeamLiveMatchBoard(id, board, options, callback)

Get details about a team match board. Only in-progress or finished games will be included, so there may be one or two games in this list.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | id | string / number | Id of desired live team match. | | board | string / number | Board identifier | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getCountry(iso, options, callback)

Get additional details about a country.

All country-based URLs use the country's 2-character ISO 3166 code (capitalized) to specify which country you want data for.

Find their code's here.

Additional countries not listed on that official list have been posted by Chess.com.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | iso | string | Country's ISO identifier | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getCountryPlayers(iso, options, callback)

List of usernames for players who identify themselves as being in this country.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | iso | string | Country's ISO identifier | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getCountryClubs(iso, options, callback)

List of URLs for clubs identified as being in or associated with this country.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | iso | string | Country's ISO identifier | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getDailyPuzzle(options, callback)

Information about the daily puzzle found in www.chess.com.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getDailyPuzzleRandom(options, callback)

Information about a randomly picked daily puzzle.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getStreamers(options, callback)

Information about Chess.com streamers.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getLeaderboards(options, callback)

It displays information about top 50 player for daily and live games, tactics and lessons.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

getTitledPlayers(titleAbbrev, options, callback)

Retrieves an array of usernames of players with a given title.

Valid title abbreviations are: GM, WGM, IM, WIM, FM, WFM, NM, WNM, CM, WCM.

Parameters

| Name | Type | Description | |----------|------------|------------------------------------------------| | titleAbbrev | string | GM, WGM, IM, WIM, FM, WFM, NM, WNM, CM, WCM.| | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

Priority Queue

The Chess.com public data API does not allow you to make two requests simultaneously. If you have more than two active requests at a time, you'll recieve a 429 Too Many Requests error.

chess-web-api can be initialized with a priority queue. All requests made through the queue will be made as soon as the previous returns.

To use chess-web-api with a queue, instantiate the wrapper with the following option set to true:

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

Using the queue requires the passing of a callback function as a parameter.

To add something to the queue, use the method dispatch.

Adds an item to the priority queue.

The provided callback method will be provided two parameters (response, error). Response is the full request result.

Additional parameters can be passed to the callback in callbackParameters.

The dispatch function determines the difference between parameters to be passed into the chess-web-api method vs the callback function by order.

The first array passed into dispatch will always be passed into the chess-web-api method.

The second array will always be sent to the callback function, with the response to the request as the first parameter. See example.

Parameters

| Name | Type | Description | |------------|------------|---------------------------------------------------| | method | function | chess-web-api function for request. | | callback | function | Function to be called with result | | parameters | array | Array of parameters to be passed into the method. | | options | object | Added options to the end of the URL (optional) | | callbackParameters | array | Array of parameters to be passed on to the callback method along with the response. (optional)| | priority | number | Priority in queue (1 is highest priority) (optional)|

Example:

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

let printResults = function(response, error, sampleParameter1, sampleParameter2) {
    console.log(response.body);
    console.log(sampleParameter1);
    console.log(sampleParameter2);
}

chessAPI.dispatch(chessAPI.getPlayer, printResults, ["andyruwruw"], {}, 1, ["callbackParameter", "anotherCallbackParameter"]);
chessAPI.dispatch(chessAPI.getTitledPlayers, printResults, ["GM"]);
chessAPI.dispatch(chessAPI.getPlayerCurrentDailyChess, printResults, ["andyruwruw"], ["callbackParameter"]);
chessAPI.dispatch(chessAPI.getPlayerCompleteMonthlyArchives, printResults, ["andyruwruw", 2019, 10], {}, ["callback parameter"]);

If you initialize your ChessWebAPI with the queue enabled, you can still call any of the regular functions without using the queue.

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

chessAPI.getTitledPlayers('GM')
    .then(function(response) {
        console.log('Grand Masters', response.body.players);
    }, function(err) {
        console.error(err);
    });

Should you wish to clear the queue you can run the following method.

Example:

var ChessWebAPI = require('chess-web-api');

var chessAPI = new ChessWebAPI({
    queue: true,
});

let printResults = function(response, error) {
    console.log(response.body);
}

chessAPI.dispatch(chessAPI.getPlayer, printResults, ["andyruwruw"], {}, 1, ["callbackParameter", "anotherCallbackParameter"]);

chessAPI.clearQueue();

The Chess.com public data API also allows clients to provide an etag from a previous response to check if data since the last request has changed.

This has been implemented into chess-web-api's ifChanged method.

Allows you to make any of the helper functions with the added parameter of the etag provided in the header of the last simular request.

Parameters

| Name | Type | Description | |------------|------------|---------------------------------------------------| | etag | string | ID of last request made. Found in the header. | | method | function | chess-web-api function for request. | | parameters | array | Array of parameters to be passed into the method. | | options | object | Added options to the end of the URL (optional) | | callback | function | Function to be called with result (optional) |

Returns

// If data has changed, response is attatched.
{
    changed: true,
    response: // response from chess.com
}

// If data has not been updated.
{
    changed: false,
}

Example:

var games, etag;

// Gets the games and stores an etag.
var trackGames = async function() {
    let response = await chessAPI.getPlayerCurrentDailyChess('andyruwruw');
    games = response.body.games;
    etag = response.headers.etag;

    // runs checkGames every 10 seconds.
    setInterval(checkGames, 10000);
}

var checkGames = async function() {
    // Makes the request but with ifChanged
    let gamesChanged = await chessAPI.ifChanged(etag, chessAPI.getPlayerCurrentDailyChess, ['andyruwruw']);

    // Updates variables if new data is available.
    if (gamesChanged.changed) {
        games = gamesChanged.response.body.games;
        etag = gamesChanged.response.headers.etag;
    }
}

trackGames();

ENJOY THE WRAPPER

alt text

- Andrew Young