A Python FastAPI implementation of the LeetCode API with GraphQL support using Strawberry.
- Python 3.9+
- Poetry (for dependency management)
- Docker (optional, for containerized deployment)
-
Clone the repository
-
Install dependencies:
make install
-
Copy environment configuration:
cp .env.example .env
-
Run the development server:
make dev
The API will be available at:
- REST API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- GraphQL Playground: http://localhost:8000/graphql
- Health Check: http://localhost:8000/health
- Migration Guide - Step-by-step migration from TypeScript API
- API Compatibility Matrix - Detailed compatibility analysis
- Technical Differences - Architecture and implementation comparison
- Complete API Reference - Full API documentation with examples
- Swagger UI:
/docs- Interactive API testing and exploration - ReDoc:
/redoc- Clean, readable API documentation - GraphQL Playground:
/graphql- Interactive GraphQL query interface
- 100% Backward Compatible: All existing TypeScript API endpoints work unchanged
- FastAPI: Modern, fast web framework with automatic validation
- GraphQL: Strawberry GraphQL integration alongside REST endpoints
- Type Safety: Full type hints and Pydantic models
- Async Support: Native asynchronous request handling
- Auto-Generated Docs: Interactive OpenAPI/Swagger documentation
- Advanced Error Handling: Structured error codes and detailed messages
- Response Caching: Built-in caching with configurable TTL
- Rate Limiting: IP-based rate limiting with customizable rules
- Health Monitoring: Comprehensive health check endpoints
- Property-Based Testing: Comprehensive test suite with Hypothesis
- Docker Support: Containerized deployment ready
- Hot Reload: Development server with automatic reloading
- Configuration Management: Environment-based configuration with validation
app/
βββ main.py # FastAPI application entry point
βββ api/routes/ # REST API endpoints
βββ core/ # Configuration & dependencies
βββ services/ # Business logic layer
βββ models/ # Pydantic data models
βββ clients/ # External API clients
βββ middleware/ # Custom middleware
βββ formatters/ # Data transformation
βββ graphql/ # GraphQL schema & resolvers
βββ queries/ # GraphQL query definitions
βββ security/ # Security & monitoring
tests/ # Test suites
docs/ # API documentation
scripts/ # Utility scripts
The Python FastAPI implementation maintains 100% backward compatibility:
- β Same Endpoints: All URLs and parameters identical
- β Same Response Format: Identical JSON structure
- β Same Behavior: Identical business logic and validation
- β No Code Changes: Existing client code works unchanged
- Update Base URL: Point to new Python API endpoint
- Test Compatibility: Verify existing functionality works
- Explore New Features: Optionally use GraphQL and enhanced features
See the Migration Guide for detailed instructions.
# Get user profile
curl https://your-api-domain.com/johndoe
# Get daily problem
curl https://your-api-domain.com/dailyProblem
# Get problems with filtering
curl "https://your-api-domain.com/problems?difficulty=Medium&limit=10"
# Get contests
curl https://your-api-domain.com/contests# Get user with specific fields
query {
user(username: "johndoe") {
username
profile {
ranking
reputation
}
badges {
name
displayName
}
}
}
# Get daily problem
query {
dailyProblem {
date
question {
title
difficulty
topicTags {
name
}
}
}
}# Build and run
make docker-build
make docker-run
# Development with hot reload
make docker-dev# Build image
docker build -t leetcode-api-python .
# Run container
docker run -p 8000:8000 leetcode-api-python
# Run with environment file
docker run -p 8000:8000 --env-file .env leetcode-api-python# Format code
make format
# Run linting
make lint
# Type checking
make type-check# Run all tests
make test
# Run specific test types
make test-unit # Unit tests only
make test-integration # Integration tests only
make test-property # Property-based tests only
# Run with coverage
make test-cov# Install pre-commit hooks
make pre-commit-install
# Run pre-commit manually
make pre-commit-runConfiguration is managed through environment variables and the app/core/settings.py module.
# API Configuration
DEBUG=false
API_TITLE="LeetCode API"
API_VERSION="2.0.0"
# Feature Toggles
ENABLE_CACHING=true
ENABLE_RATE_LIMITING=true
ENABLE_GRAPHQL=true
# Performance Settings
CACHE_TTL=300 # Cache time-to-live (seconds)
RATE_LIMIT_REQUESTS=120 # Requests per hour
RATE_LIMIT_WINDOW=3600 # Rate limit window (seconds)
# LeetCode API
LEETCODE_GRAPHQL_URL="https://leetcode.com/graphql"
LEETCODE_REFERER="https://leetcode.com"- Health Check:
/healthendpoint for service monitoring - Metrics: Built-in performance and usage metrics
- Logging: Structured logging with configurable levels
- Rate Limiting: Automatic rate limit monitoring and enforcement
- Async Processing: Native async/await for better concurrency
- Response Caching: Intelligent caching reduces external API calls
- Connection Pooling: Efficient HTTP client with connection reuse
- Request Validation: Automatic input validation with Pydantic
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
- Follow PEP 8 style guidelines
- Add type hints to all functions
- Write tests for new functionality
- Update documentation for API changes
- Use conventional commit messages
This project is a modern migration from TypeScript to Python FastAPI, inspired by and forked from the excellent work of:
Alfaarghya's alfa-leetcode-api
- Original TypeScript/Node.js implementation
- Foundation and API design
This Python FastAPI version maintains API compatibility while providing a Pythonic implementation with enhanced features like GraphQL support, improved type safety, and better async handling.
This project is licensed under the MIT License - see the LICENSE file for details.
- Original TypeScript API: README.md
- Interactive API Docs:
/docsendpoint - GraphQL Playground:
/graphqlendpoint - Health Status:
/healthendpoint - Complete Documentation: docs/ directory
- Documentation: Check the docs/ directory for comprehensive guides
- Interactive Testing: Use
/docsand/graphqlendpoints for API exploration - Health Monitoring: Use
/healthendpoint to verify service status - Issues: Report bugs and request features on GitHub
Migration Note: This Python implementation maintains 100% compatibility with the original TypeScript API while providing significant enhancements. See the Migration Guide for seamless transition instructions.