{"id":39230179,"url":"https://github.com/myselfshravan/git-stats-api","last_synced_at":"2026-01-17T23:40:20.569Z","repository":{"id":328689582,"uuid":"1033093985","full_name":"myselfshravan/git-stats-api","owner":"myselfshravan","description":"GitHub Stats API for portfolio website - Open Source","archived":false,"fork":false,"pushed_at":"2025-12-14T18:44:16.000Z","size":41,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-17T05:50:11.001Z","etag":null,"topics":["api","github","github-config","open-source"],"latest_commit_sha":null,"homepage":"https://gitstatsapi.vercel.app/api/commits","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/myselfshravan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-06T09:37:14.000Z","updated_at":"2025-12-14T18:44:20.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/myselfshravan/git-stats-api","commit_stats":null,"previous_names":["myselfshravan/git-stats-api"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/myselfshravan/git-stats-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/myselfshravan%2Fgit-stats-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/myselfshravan%2Fgit-stats-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/myselfshravan%2Fgit-stats-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/myselfshravan%2Fgit-stats-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/myselfshravan","download_url":"https://codeload.github.com/myselfshravan/git-stats-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/myselfshravan%2Fgit-stats-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28522311,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T22:11:28.393Z","status":"ssl_error","status_checked_at":"2026-01-17T22:11:27.841Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","github","github-config","open-source"],"created_at":"2026-01-17T23:40:20.513Z","updated_at":"2026-01-17T23:40:20.563Z","avatar_url":"https://github.com/myselfshravan.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GitHub Stats API\n\nA Next.js 14 App Router API that provides comprehensive GitHub statistics including lifetime commit counts and detailed repository breakdowns for GitHub users.\n\n## Features\n\n- **Single Endpoint**: `GET /api/commits/{username}` returns comprehensive GitHub statistics\n- **Detailed Repository Breakdown**: Shows owned repos, original repos (non-forks), and private repos\n- **GitHub GraphQL Integration**: Fetches commits year-by-year and detailed repository statistics\n- **Smart Caching**: 24-hour in-memory cache per user to avoid rate limits\n- **CORS Support**: Ready for cross-origin requests (GitHub Pages, etc.)\n- **Error Handling**: Comprehensive error responses with appropriate HTTP status codes\n- **Vercel Ready**: Optimized for serverless deployment\n\n## API Response\n\n### Success Response\n```json\n{\n  \"commits\": 1308,\n  \"total_repos\": 42,\n  \"repos_count\": {\n    \"owned\": 28,\n    \"original\": 35,\n    \"private\": 12\n  }\n}\n```\n\n**Response Fields:**\n- `commits`: Total lifetime commit count across all years\n- `total_repos`: Total repository count (public + private)\n- `repos_count.owned`: Repositories where user is the owner (excludes collaborations)\n- `repos_count.original`: Non-forked repositories (original work)  \n- `repos_count.private`: Private repositories\n\n### Error Response\n```json\n{\n  \"error\": \"User not found\"\n}\n```\n\n## Setup\n\n### 1. Clone and Install\n\n```bash\ngit clone \u003crepository-url\u003e\ncd git-stats-api\nnpm install\n```\n\n### 2. Environment Variables\n\nCreate a `.env.local` file in the root directory:\n\n```bash\ncp .env.local.example .env.local\n```\n\nAdd your GitHub Personal Access Token:\n\n```env\nGITHUB_TOKEN=your_github_personal_access_token_here\n```\n\n#### Creating a GitHub Token\n\n1. Go to GitHub Settings → Developer settings → Personal access tokens\n2. Click \"Generate new token (classic)\"\n3. Give it a descriptive name like \"Git Stats API\"\n4. Select scopes: `public_repo` (or `repo` if you need private repo access)\n5. Click \"Generate token\"\n6. Copy the token and add it to your `.env.local` file\n\n### 3. Run Locally\n\n```bash\nnpm run dev\n```\n\nThe API will be available at `http://localhost:3000`\n\nTest it: `http://localhost:3000/api/commits/octocat`\n\n## Deployment\n\n### Deploy to Vercel\n\n1. **Via Vercel CLI** (recommended):\n   ```bash\n   npm i -g vercel\n   vercel\n   ```\n\n2. **Via GitHub Integration**:\n   - Push your code to GitHub\n   - Connect your repository to Vercel\n   - Add `GITHUB_TOKEN` to your Vercel environment variables\n\n3. **Environment Variables**:\n   - In your Vercel dashboard, go to Settings → Environment Variables\n   - Add: `GITHUB_TOKEN` with your GitHub token value\n\n### Manual Deploy\n```bash\nnpm run build\nnpm start\n```\n\n## Usage Examples\n\n### JavaScript/Fetch\n```javascript\nconst response = await fetch('https://your-api.vercel.app/api/commits/octocat');\nconst data = await response.json();\nconsole.log(`Total commits: ${data.commits}`);\nconsole.log(`Total repos: ${data.total_repos}`);\nconsole.log(`Owned: ${data.repos_count.owned}, Original: ${data.repos_count.original}, Private: ${data.repos_count.private}`);\n```\n\n### cURL\n```bash\ncurl https://your-api.vercel.app/api/commits/octocat\n```\n\n### React Component\n```tsx\nimport { useEffect, useState } from 'react';\n\nfunction GitHubStats({ username }) {\n  const [stats, setStats] = useState(null);\n  const [loading, setLoading] = useState(true);\n\n  useEffect(() =\u003e {\n    fetch(`https://your-api.vercel.app/api/commits/${username}`)\n      .then(res =\u003e res.json())\n      .then(data =\u003e {\n        setStats(data);\n        setLoading(false);\n      });\n  }, [username]);\n\n  if (loading) return \u003cdiv\u003eLoading...\u003c/div\u003e;\n  return (\n    \u003cdiv\u003e\n      \u003cdiv\u003e{stats.commits} commits • {stats.total_repos} repositories\u003c/div\u003e\n      \u003cdiv\u003e\n        Owned: {stats.repos_count.owned} | \n        Original: {stats.repos_count.original} | \n        Private: {stats.repos_count.private}\n      \u003c/div\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n## Architecture\n\n```\nlib/\n├── github.ts       # GitHub GraphQL client\n├── cache.ts        # In-memory caching system  \n└── utils.ts        # Utility functions (CORS, number formatting)\n\napp/api/commits/[username]/\n└── route.ts        # Main API endpoint\n\ntypes/\n└── index.ts        # TypeScript type definitions\n```\n\n## How It Works\n\n1. **User Request**: Client requests `/api/commits/{username}`\n2. **Cache Check**: System checks if data is cached and fresh (\u003c 24 hours)\n3. **GitHub Query**: If not cached, queries GitHub GraphQL API:\n   - Fetches user's account creation date\n   - Loops through each year from creation to present\n   - Sums `contributionCalendar.totalContributions` for each year\n   - Fetches detailed repository statistics:\n     - Total repositories (public + private)\n     - Owned repositories (user is owner)\n     - Original repositories (non-forks)\n     - Private repositories\n4. **Cache \u0026 Return**: Stores comprehensive stats in memory cache and returns detailed data\n\n## Rate Limits\n\n- **GitHub API**: 5000 requests/hour with personal access token\n- **Caching**: 24-hour cache per user significantly reduces API calls\n- **Optimization**: Year-by-year querying minimizes data transfer\n\n## Error Handling\n\nThe API handles various error scenarios:\n\n- **Missing Username**: 400 Bad Request\n- **User Not Found**: 500 with descriptive error\n- **Invalid Token**: 500 with GitHub API error\n- **Network Issues**: 500 with connection error\n- **Rate Limiting**: 500 with GitHub rate limit error\n\n## CORS Configuration\n\nThe API includes CORS headers for cross-origin requests:\n\n```javascript\n'Access-Control-Allow-Origin': '*'\n'Access-Control-Allow-Methods': 'GET, OPTIONS'\n'Access-Control-Allow-Headers': 'Content-Type'\n```\n\nPerfect for integration with GitHub Pages, portfolios, and other static sites.\n\n## Utilities\n\n### Number Formatting\n```javascript\nimport { formatNumber } from './lib/utils';\n\nformatNumber(1308);     // \"1.3k\"\nformatNumber(2500000);  // \"2.5M\"\nformatNumber(42);       // \"42\"\n\n// Example usage with API response\nconst { commits, total_repos, repos_count } = await fetch('/api/commits/username').then(r =\u003e r.json());\nconsole.log(`${formatNumber(commits)} commits • ${formatNumber(total_repos)} total repos`);\nconsole.log(`${formatNumber(repos_count.owned)} owned • ${formatNumber(repos_count.original)} original • ${formatNumber(repos_count.private)} private`);\n```\n\n## Development\n\n### Project Structure\n- Next.js 14 with App Router\n- TypeScript for type safety\n- No external dependencies beyond Next.js\n- Serverless-ready architecture\n\n### Local Development\n```bash\nnpm run dev      # Start development server\nnpm run build    # Build for production\nnpm run start    # Start production server\nnpm run lint     # Run ESLint\n```\n\n## License\n\nMIT License - feel free to use in your projects!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmyselfshravan%2Fgit-stats-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmyselfshravan%2Fgit-stats-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmyselfshravan%2Fgit-stats-api/lists"}