A lightweight REST API for bot-vs-bot chess games, built for Discord bots Jarvis and Gromozeka. Supports both local development (JSON files) and Vercel serverless deployment (Upstash Redis).
- Create new chess games between two players
- Make moves with full chess rule validation
- Get current game state and board representation
- ASCII board display for Discord posting
- Game state persistence via JSON files (local) or Redis (Vercel)
- Support for checkmate, stalemate, and draw detection
This API is designed for serverless deployment on Vercel with Upstash Redis for storage.
- Vercel account - Sign up at vercel.com
- Upstash Redis database - Create a free Redis database at upstash.com
- Go to Upstash Console
- Create a new Redis database (free tier available)
- Copy the REST URL and REST TOKEN from the database details
- These will be your environment variables
- Fork/clone this repository
- Connect your repository to Vercel
- Set environment variables in Vercel:
UPSTASH_REDIS_REST_URL= Your Redis REST URLUPSTASH_REDIS_REST_TOKEN= Your Redis REST token
- Deploy! Vercel will automatically detect the
vercel.jsonconfiguration
The API will be available at your Vercel domain (e.g., https://your-app.vercel.app)
UPSTASH_REDIS_REST_URL=https://your-redis-url.upstash.io
UPSTASH_REDIS_REST_TOKEN=your-redis-token
For local development and testing, the original JSON file storage is still supported.
- Clone or download this directory
- Create a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
source venv/bin/activate # On Windows: venv\Scripts\activate
uvicorn main:app --host 0.0.0.0 --port 8888The API will be available at http://localhost:8888
Once the server is running, visit:
- Interactive API docs:
http://localhost:8888/docs(local) orhttps://your-app.vercel.app/docs(Vercel) - ReDoc documentation:
http://localhost:8888/redoc(local) orhttps://your-app.vercel.app/redoc(Vercel)
POST /game/new
Content-Type: application/json
{
"white": "jarvis",
"black": "gromozeka"
}Returns game ID and initial state.
GET /game/{game_id}Returns current game state including FEN, moves, status, and whose turn it is.
POST /game/{game_id}/move
Content-Type: application/json
{
"player": "jarvis",
"move": "e2e4"
}Move format is UCI notation (e.g., "e2e4", "g1f3", "e7e8q" for promotion).
GET /game/{game_id}/boardReturns ASCII art representation of the current board position.
GET /gamesReturns summary of all games (active and completed).
in_progress- Game is ongoingcheckmate- Game ended by checkmatestalemate- Game ended by stalematedraw- Game ended by other draw conditions (insufficient material, repetition, etc.)
Games are stored as JSON files in ~/.chess-api/games/ directory. Each game file is named {game_id}.json.
Games are stored in Upstash Redis with the following structure:
- Game data:
game:{game_id}→ JSON string - Game list:
games:all→ Redis set containing all game IDs
Both storage methods maintain the same game data format:
{
"id": "uuid",
"white": "player_name",
"black": "player_name",
"fen": "current_board_position",
"moves": ["list", "of", "uci", "moves"],
"status": "in_progress",
"created_at": "iso_timestamp",
"updated_at": "iso_timestamp"
}source venv/bin/activate
python -m pytest test_chess_api.py -vsource venv/bin/activate
python -m pytest test_chess_api_redis.py -vCORS is enabled for all origins to allow Discord bots from different hosts to access the API.
- Python 3.12+ - Runtime
- FastAPI - Web framework
- python-chess - Chess logic and validation
- upstash-redis - Redis client for Vercel (serverless-friendly)
- uvicorn - ASGI server (local development)
- pytest - Testing framework
The project is structured for both local development and serverless deployment:
main.py- Local development server with JSON file storageapi/index.py- Vercel serverless function with Redis storagechess_logic.py- Shared game logic and modelsvercel.json- Vercel deployment configuration
The project follows Test-Driven Development (TDD). All tests should pass before any changes are committed.
To make changes:
- Write or update tests first
- Implement the feature to make tests pass
- Verify all tests still pass (both local and Redis versions)
- Commit changes
The API returns appropriate HTTP status codes:
200- Success400- Bad request (invalid move, wrong turn, etc.)404- Game not found500- Server error
Error responses include descriptive messages in the detail field.
- Local development: Free
- Vercel deployment: Free tier supports hobby projects
- Upstash Redis: Free tier includes 10K commands/day, perfect for testing
Both platforms offer generous free tiers suitable for Discord bot usage.