A production-ready serverless visit counter badge for GitHub README files. This project uses Vercel serverless functions to track and display visit counts in a beautiful SVG badge.
- π¨ Beautiful neon purple SVG badge (similar to shields.io style)
- β‘ Serverless function on Vercel (fast and scalable)
- πΎ Dual storage: Vercel KV (production) or JSON file (local dev)
- π‘οΈ Built-in rate limiting (prevents abuse)
- π Auto-detection of storage backend
- π Real-time counter updates
# Install Vercel CLI (if not already installed) npm i -g vercel # Deploy vercel --prodOr connect your GitHub repository to Vercel for automatic deployments.
For production, you'll need to set up Vercel KV:
- Go to your Vercel project dashboard
- Navigate to Storage β Create Database β KV
- Create a new KV database
- Go to Settings β Environment Variables and add the following (Vercel will automatically inject these when you link the KV database):
Option A: Using Vercel KV SDK (Recommended) When you link a KV database to your project, Vercel automatically sets:
KV_REST_API_URLKV_REST_API_TOKENKV_REST_API_READ_ONLY_TOKEN
Option B: Manual Configuration If you need to set them manually:
KV_REST_API_URL- Your KV REST API URL (from KV database settings)KV_REST_API_TOKEN- Your KV REST API token (from KV database settings)
Note: The project automatically detects which storage method to use. If KV environment variables are present, it uses KV. Otherwise, it falls back to JSON file storage (for local development).
Add this line to your GitHub README.md:
Replace your-project-name with your actual Vercel project name.
- Node.js 18+ installed
- Vercel CLI installed (
npm i -g vercel)
# Install dependencies (if any) npm install # Start local development server npm run devThe counter will use counter.json for storage when running locally (no KV required).
For local development, you can create a .env file (see .env.example). However, local development will automatically fall back to JSON file storage if KV variables are not set.
. βββ api/ β βββ counter.js # Serverless function handler βββ lib/ β βββ storage.js # Storage abstraction layer βββ counter.json # Initial seed file (local dev) βββ vercel.json # Vercel configuration βββ package.json # Project dependencies βββ .env.example # Example environment variables βββ README.md # This file -
Request Flow:
- User loads the badge URL in their README
- Vercel serverless function is triggered
- Function checks rate limiting (1 request per minute per IP)
- Counter is incremented (if not rate limited)
- SVG badge is generated and returned
-
Storage:
- Production: Uses Vercel KV for persistent, distributed storage
- Local Dev: Uses
counter.jsonfile for simplicity
-
Rate Limiting:
- Prevents abuse by limiting increments to once per minute per IP
- Still returns the current count even if rate limited
Edit api/counter.js and modify the color constants:
const labelColor = '#9D7CFF'; // Neon purple const valueColor = '#0d1117'; // Dark background const textColor = '#ffffff'; // White textModify the label variable in generateSVG() function:
const label = 'visits'; // Change to 'views', 'hits', etc.- Check that your Vercel deployment is successful
- Verify environment variables are set correctly
- Check Vercel function logs for errors
- Ensure Vercel KV is properly configured
- Check that
VERCEL_REST_API_TOKENhas correct permissions - Verify
VERCEL_KV_NAMESPACEmatches your KV database
- Make sure
counter.jsonexists and is writable - Check that Node.js version is 18 or higher
- Verify Vercel CLI is installed and up to date
MIT
Contributions are welcome! Feel free to open issues or submit pull requests.





