When you build a React/Vite app, the API key gets bundled into the JavaScript files that are sent to the browser. Anyone can:
- View the page source
- Open browser DevTools → Network tab
- See the API key in the JavaScript bundle
This is unavoidable for front-end-only apps.
This is your real security layer:
- Go to Google Cloud Console
- Edit your API key
- Set Application restrictions → HTTP referrers
- Add only your domain:
https://daniyal-rashid-00.github.io/* https://your-custom-domain.com/* - Set API restrictions → Only enable "Generative Language API"
Result: Even if someone steals your API key, it won't work from other domains.
- ✅ Use GitHub Secrets (you're already doing this)
- ✅ Use environment variables
- ❌ Never hardcode the key in your code
- Set up billing alerts in Google Cloud Console
- Monitor API usage regularly
- Set up rate limiting if possible
GitHub Pages: Requires GitHub Pro ($4/month) for private repos
Better Free Alternatives:
- Vercel: Free tier supports private repos ✅
- Netlify: Free tier supports private repos ✅
- Cloudflare Pages: Free tier supports private repos ✅
- Push your code to GitHub (can be private)
- Go to vercel.com
- Import your private repository
- Add environment variable:
VITE_GEMINI_API_KEY - Deploy!
Benefits:
- ✅ Free
- ✅ Supports private repos
- ✅ Automatic deployments
- ✅ Custom domains
- ✅ Better performance (CDN)
- Cost: $4/month
- Allows private repos with GitHub Pages
- Other GitHub features included
This is actually fine because:
- ✅ Your API key is in GitHub Secrets (not in source code)
- ✅ API key restrictions protect you
- ✅ The key would be visible in the deployed bundle anyway (even with private repo)
- Keep using GitHub Secrets (you're already doing this ✅)
- Set up API key restrictions in Google Cloud Console (most important!)
- Monitor your API usage regularly
- Consider switching to Vercel if you want private repo + free hosting
Making the repo private won't hide your API key from the deployed site - it will still be visible in the browser. The real security comes from:
- API key restrictions (domain whitelist)
- API usage monitoring
- Not hardcoding keys in source code (use secrets/env vars)
Your current setup with GitHub Secrets is already good! Just make sure API key restrictions are configured properly.