Skip to content

itsdevansh5/GithubStats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

23 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“Š GitHub Language Stats API

๐Ÿš€ FastAPI + MongoDB Atlas + GitHub API

A production-ready backend API that analyzes GitHub repository languages, computes total usage, stores historical snapshots, and serves publicly accessible endpoints. Built with FastAPI, MongoDB Atlas, and deployed on Render.


๐ŸŒŸ Overview

This backend project:

  • Fetches all repositories of a GitHub user
  • Aggregates language usage across all repos
  • Computes bytes + percentage usage
  • Caches results for 24 hours using MongoDB
  • Stores full historical snapshots over time
  • Exposes clean API endpoints with Pydantic models
  • Runs asynchronously using FastAPI + httpx
  • Fully deployed online and globally accessible

Perfect for:

  • Developer dashboards
  • Portfolio projects
  • GitHub analytics tools
  • Resume enhancement
  • Learning full backend engineering

๐Ÿง  Tech Stack

Layer Technology
Backend Framework FastAPI
HTTP Client httpx (async)
Database MongoDB Atlas
Driver Motor (async)
Deployment Render
Config python-dotenv
Data Models Pydantic

๐Ÿ—‚ Project Structure

github-stats/
โ”‚โ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ main.py
โ”‚   โ”œโ”€โ”€ github_api.py
โ”‚   โ”œโ”€โ”€ stats_service.py
โ”‚   โ”œโ”€โ”€ database.py
โ”‚   โ”œโ”€โ”€ models.py
โ”‚โ”€โ”€ requirements.txt
โ”‚โ”€โ”€ .env
โ”‚โ”€โ”€ .gitignore
โ”‚โ”€โ”€ README.md

๐ŸŒ Live Demo

Frontend Coming Soon .....

๐Ÿš€ Base URL

https://githubstats-gqcp.onrender.com/

๐Ÿ“˜ Swagger Docs

https://githubstats-gqcp.onrender.com/docs

Embed in Your README

<img src="https://githubstats-gqcp.onrender.com/card/stats?username=YOUR_GITHUB_USERNAME" />

Example:

<img src="https://githubstats-gqcp.onrender.com/card/stats?username=itsdevansh5" />

๐Ÿ“ก API Documentation

1๏ธโƒฃ GET /

Health check route

Returns API status.

Example:

{"message": "GitHub Stats API is running!"}

2๏ธโƒฃ GET /stats/{username}

Fetches latest GitHub language statistics.

โœจ Features:

  • Fetches repositories
  • Aggregates all language data
  • Computes percentages
  • Saves snapshot
  • Uses 24-hour caching

Example:

/stats/itsdevansh5

Response:

{
  "username": "itsdevansh5",
  "cached": false,
  "total_bytes": {
    "Python": 63697231,
    "C++": 224954,
    "HTML": 246905
  },
  "percentages": {
    "Python": 93.13,
    "C++": 0.33,
    "HTML": 0.36
  }
}

If cached:

{
  "cached": true,
  ...
}

3๏ธโƒฃ GET /history/{username}

Returns all historical snapshots of the user.

Example:

{
  "username": "itsdevansh5",
  "history": [
    {
      "fetched_at": "2025-12-10T12:34:11",
      "total_bytes": { ... },
      "percentages": { ... }
    },
    {
      "fetched_at": "2025-12-11T12:34:11",
      "total_bytes": { ... },
      "percentages": { ... }
    }
  ]
}

3๏ธโƒฃ GET /card

Generates a dynamic SVG GitHub stats card for a user.

Query Params:

  • username โ€” GitHub username (required)

Example:

<img src="https://githubstats-gqcp.onrender.com/card?username=itsdevansh5" />
Screenshot (23)

๐Ÿงฎ How Percentages Are Calculated

GitHub returns byte counts:

Python โ†’ 63697231
C++    โ†’ 224954
HTML   โ†’ 246905

Percentage formula:

percent = (bytes_of_lang / sum(all_bytes)) ร— 100

Rounded to 2 decimals.


๐Ÿ” Environment Variables

Your .env should contain:

MONGO_URL="mongodb+srv://<user>:<password>@cluster.mongodb.net/?retryWrites=true&w=majority"
GITHUB_TOKEN="ghp_your_github_token_here"

โš ๏ธ Never commit .env to GitHub.
Add this to .gitignore:

.env

๐Ÿš€ Deployment (Render)

1๏ธโƒฃ Push code to GitHub

2๏ธโƒฃ Go to Render โ†’ New Web Service

3๏ธโƒฃ Choose your repo

4๏ธโƒฃ Set settings:

Build Command

pip install -r requirements.txt

Start Command

uvicorn app.main:app --host=0.0.0.0 --port=$PORT

5๏ธโƒฃ Add Environment Variables:

MONGO_URL=your_url_here
GITHUB_TOKEN=your_token_here
PYTHON_VERSION=3.11

6๏ธโƒฃ Deploy ๐ŸŽ‰


๐Ÿ“ To-Do / Future Improvements

  • Add user authentication
  • Add rate-limiting per IP
  • Add frontend dashboard
  • Add export to CSV / JSON
  • Add charts (Pie, Bar, Trends)
  • Add caching with Redis
  • Make it a full SaaS product

๐ŸŽ‰ Author

Made with โค๏ธ by Devansh
A backend + cloud enthusiast building real-world projects.


โญ Enjoy using the GitHub Stats API!

About

My very own SaaS application for getting Github Stats through Github API,can be used with your profile readme.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages