π Live API: https://leetcode-api-140473619582.us-central1.run.app
π Documentation: https://leetcode-api-140473619582.us-central1.run.app/docs/
A Flask-based REST API that fetches and processes comprehensive LeetCode user data including user profiles, submission statistics, and progress tracking by year.
Try the API right now:
curl https://leetcode-api-140473619582.us-central1.run.app/api/user/{username}- π Comprehensive User Profiles: Fetch detailed user information including bio, company, education, and social links
- π Problem Statistics: Get solving statistics categorized by difficulty (Easy, Medium, Hard)
- π Progress Tracking: View submission progress with yearly breakdowns and daily activity
- π― Current Year Analysis: Special focus on the last 365 days of activity
- π Social Integration: Access to GitHub, Twitter, and LinkedIn profiles
- π Ranking Information: LeetCode ranking and skill tags
- β‘ Redis Caching: Fast response times with 1-hour cache for user data
- π CORS Enabled: Cross-origin requests supported for web applications
Endpoint: GET /api/user/{username}
Description: Retrieves comprehensive LeetCode user data for the specified username.
Parameters:
username(path parameter, required): LeetCode username
Example Request:
curl -X GET "https://leetcode-api-140473619582.us-central1.run.app/api/user/john_doe"Example Response:
{
"username": "john_doe",
"github": "https://github.com/johndoe",
"twitter": "https://twitter.com/johndoe",
"linkedin": "https://linkedin.com/in/johndoe",
"ranking": 12345,
"realname": "John Doe",
"aboutme": "Software Engineer passionate about algorithms",
"school": "MIT",
"website": ["https://johndoe.dev"],
"country_name": "United States",
"company": "Tech Corp",
"job_title": "Senior Software Engineer",
"skill": ["Python", "JavaScript", "Algorithms"],
"progress": {
"current": {
"daily": {
"2024-01-15": 3,
"2024-01-16": 5
},
"total": 180
},
"2024": {
"daily": {
"2024-01-01": 2,
"2024-01-02": 1
},
"total": 365
},
"2023": {
"daily": {
"2023-12-31": 4
},
"total": 200
}
},
"problem": {
"easy": {
"solved": 150,
"total": 800
},
"medium": {
"solved": 100,
"total": 1600
},
"hard": {
"solved": 25,
"total": 700
}
}
}-
Clone the repository:
git clone <repository-url> cd LC-API
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
Copy the example environment file and configure your Redis credentials:
cp .env.example .env
Edit
.envand add your Redis (Upstash) credentials:REDIS_HOST=your-redis-host.upstash.io REDIS_PORT=your-redis-port REDIS_PASSWORD=your-redis-password
-
Start the server:
python main.py
-
Access the API: The API is deployed and available at
https://leetcode-api-140473619582.us-central1.run.app. -
Make requests:
# Get user data for a specific LeetCode username curl https://leetcode-api-140473619582.us-central1.run.app/api/user/your_leetcode_username
The API returns a JSON object with the following structure:
| Field | Type | Description |
|---|---|---|
username |
string | LeetCode username |
github |
string/null | GitHub profile URL |
twitter |
string/null | Twitter profile URL |
linkedin |
string/null | LinkedIn profile URL |
ranking |
integer/null | LeetCode ranking |
realname |
string/null | User's real name |
aboutme |
string/null | User's bio/about section |
school |
string/null | Educational institution |
website |
array | List of personal websites |
country_name |
string/null | User's country |
company |
string/null | Current company |
job_title |
string/null | Current job title |
skill |
array | List of skill tags |
progress |
object | Submission progress data |
problem |
object | Problem solving statistics |
The progress object contains:
current: Last 365 days of activityYYYY: Yearly breakdowns (e.g., "2024", "2023")
Each period contains:
daily: Object with date strings as keys and submission counts as valuestotal: Total submissions for that period
The problem object contains statistics for each difficulty:
easy: { solved: number, total: number }medium: { solved: number, total: number }hard: { solved: number, total: number }
{
"error": "Error message describing what went wrong"
}Common error scenarios:
- Invalid username
- User profile is private or doesn't exist
- LeetCode API blocking requests
- Network connectivity issues
- Flask: Web framework for creating the REST API
- flask-cors: Enable CORS for cross-origin requests from web applications
- flask-swagger-ui: Interactive API documentation interface
- requests: HTTP library for making requests to LeetCode's GraphQL API
- redis: Redis client for caching user data and improving performance
- python-dotenv: Load environment variables from .env file for configuration
- datetime: For date/time manipulation and progress calculations
- json: For parsing LeetCode's submission calendar data
- collections.defaultdict: For efficient data structure management
- GraphQL Query: The API uses LeetCode's GraphQL endpoint to fetch comprehensive user data
- Redis Caching: User data is cached for 1 hour to improve performance and reduce API calls
- Calendar Processing: Submission calendar data is processed to create yearly and current (365-day) breakdowns
- Statistics Aggregation: Problem statistics are calculated by difficulty level
- Data Transformation: Raw LeetCode data is transformed into a clean, structured format
- Cache Duration: 1 hour (3600 seconds) for user data
- Cache Key Format:
leetcode_user:{username} - Cache Benefits: Faster response times, reduced LeetCode API calls, better rate limit compliance
- Cache Provider: Redis (Upstash) for reliable cloud-based caching
- The API includes a custom User-Agent header to reduce the likelihood of being blocked
- Redis caching significantly reduces calls to LeetCode's API
- Be mindful of LeetCode's rate limiting policies
The API includes comprehensive error handling for:
- Invalid usernames
- Network timeouts
- Malformed responses from LeetCode
- Missing or null data fields
LC-API/
βββ main.py # Main application file
βββ swagger.yaml # OpenAPI/Swagger specification
βββ README.md # This file
get_user_profile(username): Fetches raw user data from LeetCode's GraphQL APIprocess_progress_by_year(submission_calendar): Processes submission calendar into yearly breakdownsget_user_data(username): Main API endpoint handler
- Enable debug mode (already enabled in the code)
- The server will automatically reload on code changes
- Access detailed error messages in debug mode
For detailed API documentation with interactive testing capabilities, refer to the swagger.yaml file which provides:
- Complete endpoint specifications
- Request/response schemas
- Example payloads
- Error response formats
You can view the Swagger documentation using tools like:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
This API is for educational and personal use only. Please respect LeetCode's terms of service and rate limiting policies. The API fetches publicly available user data and does not access private information.
If you encounter any issues or have questions:
- Check the error response for detailed information
- Ensure the LeetCode username is correct and the profile is public
- Verify network connectivity
- Check if LeetCode's API is accessible from your location