A Cloudflare Worker to generate dynamic SVG shields for your GitHub repository and user statistics. Display your project's popularity (stars ⭐, forks 🍴) and your overall GitHub contributions with neat, auto-updating badges!
- Dynamic SVG Shields: Generate shields for GitHub repository stars and forks.
- User Statistics Shields: Generate shields for a user's total stars and total forks across all their repositories.
- JSON Data: Access the raw statistics in JSON format.
- Cloudflare Workers Powered: Fast, reliable, and globally distributed.
- KV Caching: Efficiently caches responses to reduce GitHub API load and provide quick responses. Cache duration is 10 minutes for repository stats and 60 minutes for user stats.
Replace :owner
, :repo
, and :metric
with your desired values. The base URL for these endpoints will be your Cloudflare Worker's URL.
-
Get Repository Stats (JSON)
- Endpoint:
/:owner/:repo
- Description: Retrieves statistics for a specific repository.
- Example:
/your-username/your-repo-name
- Response:
{ "stars": 123, "forks": 45 // ... other stats from getRepoStats if any }
- Endpoint:
-
Get Repository Shield (SVG)
- Endpoint:
/:owner/:repo/shields/:metric
- Description: Generates an SVG shield for a specific repository metric.
- Supported
:metric
values:stars
,forks
. - Example:
/your-username/your-repo-name/shields/stars
- Example (Markdown):

- Endpoint:
-
Get User Total Stats (JSON)
- Endpoint:
/:owner/stars
(Note: This endpoint returns all user stats including total forks, despite "stars" in the path) - Description: Retrieves total stars and forks for a specific user across all their repositories.
- Example:
/your-username/stars
- Response:
{ "totalStars": 1234, "totalForks": 567 }
- Endpoint:
-
Get User Shield (SVG)
- Endpoint:
/:owner/shields/:metric
- Description: Generates an SVG shield for a user's total metric.
- Supported
:metric
values:stars
,forks
. - Example:
/your-username/shields/stars
- Example (Markdown):

- Endpoint:
- The service receives a request for a shield or JSON data.
- It first checks Cloudflare KV for a cached response.
- If a fresh cached response exists, it's returned immediately.
- Otherwise, it fetches the latest stats from the GitHub API.
- The stats are then used to generate an SVG shield or a JSON response.
- The new stats are stored in KV for future requests.
- Repository stats are cached for 10 minutes (
expirationTtl: 600
). - User stats are cached for 60 minutes (
expirationTtl: 3600
).
- Repository stats are cached for 10 minutes (
This service is designed to be deployed as a Cloudflare Worker. You'll need to:
- Set up a Cloudflare account and a Worker.
- Configure a KV namespace and bind it to your worker as
KV
. - Set up
GH_TOKEN
as a secret in your worker environment if you need to overcome GitHub API rate limits for unauthenticated requests. (The provided code doesn't explicitly show token usage ingetRepoStats
orgetUserStats
, but it's a common practice for robust GitHub API interaction).
Contributions are welcome! Please feel free to submit a Pull Request or open an Issue.
This project is open source. You can specify your license here (e.g., MIT License).