Yes, restricting your API key to your website is the right approach! Here's why:
✅ Security: Even though the API key is visible in your client-side code, it will only work when requests come from your allowed domains
✅ Cost Protection: Prevents others from using your API key on their websites
✅ Rate Limit Protection: Stops unauthorized usage that could hit your rate limits
Important Note: The API key will still be visible in the browser's source code, but Google will reject any requests that don't come from your whitelisted domains.
- Visit: https://console.cloud.google.com/apis/credentials
- Sign in with your Google account
- Select your project (or create one if you haven't)
- Look for your API key (
VITE_GEMINI_API_KEY) in the credentials list - Click on the key name to edit it
-
Under "Application restrictions", select "HTTP referrers (web sites)"
-
Click "Add an item" and add these referrers one by one:
For Production (Your Live Site):
https://daniyal-rashid-00.github.io/* https://daniyal-rashid-00.github.io/Email-Generator/*For Local Development (Optional but Recommended):
http://localhost:* http://127.0.0.1:* http://localhost:5173/* -
Important: Make sure to include the
/*at the end - this allows all paths on your domain
- Under "API restrictions", select "Restrict key"
- Check the box for "Generative Language API" (or "Vertex AI API")
- Click "Save"
- Go to: https://console.cloud.google.com/apis/library
- Search for "Generative Language API"
- Click on it and click "Enable"
- Wait a few minutes for it to activate
- Under API restrictions, select "Restrict key"
- Select "Generative Language API" (or "Vertex AI API" if that's what you're using)
- Click Save
- Go to: https://console.cloud.google.com/apis/library
- Search for "Generative Language API" or "Gemini API"
- Click on it and click "Enable"
- Make sure the API key value matches what you have in GitHub Secrets
- The key should start with
AIza...
Yes, you absolutely still need GitHub Secrets! Here's why:
- Build Process: During the GitHub Actions build, Vite needs the API key to embed it into your JavaScript bundle
- Security: Keeps the API key out of your source code repository
- Environment Variables: The key is injected as
VITE_GEMINI_API_KEYduring the build
GitHub Secret (VITE_GEMINI_API_KEY)
↓
GitHub Actions Build Process
↓
Injected into JavaScript bundle
↓
Deployed to GitHub Pages
↓
Visible in browser, but ONLY works from your whitelisted domains
- Go to your GitHub repository
- Click Settings → Secrets and variables → Actions
- Click "New repository secret"
- Name:
VITE_GEMINI_API_KEY - Value: Your API key (the same one you restricted in Google Cloud Console)
- Click "Add secret"
Important: The API key in GitHub Secrets should be the same key you configured in Google Cloud Console with domain restrictions.
Here's the complete security flow:
- ✅ API Key in GitHub Secrets → Not visible in your code repository
- ✅ Domain Restrictions in Google Cloud → Key only works from your domain
- ✅ Visible in Browser → But Google rejects requests from other domains
- ✅ Result: Others can see the key, but cannot use it on their websites
After configuring everything:
- Wait 2-3 minutes for Google Cloud changes to propagate
- Rebuild your site (push a new commit or manually trigger GitHub Actions)
- Test on your live site: https://daniyal-rashid-00.github.io/Email-Generator/
- Test locally: Run
npm run devand test on localhost
- ✅ Verify the referrer URLs in Google Cloud Console match exactly (including
/*) - ✅ Check that "Generative Language API" is enabled
- ✅ Wait a few more minutes for changes to propagate
- ✅ Check browser console for detailed error messages
- ✅ Verify the API key in GitHub Secrets matches the one in Google Cloud Console
If your current key is blocked or you want a fresh start:
- Go to: https://console.cloud.google.com/apis/credentials
- Click "Create Credentials" → "API Key"
- Configure restrictions as described above
- Update the
VITE_GEMINI_API_KEYsecret in GitHub with the new key
- ✅ Monitor Usage: Check Google Cloud Console regularly for API usage
- ✅ Set Billing Alerts: Avoid unexpected charges
- ✅ Rate Limiting: Consider implementing client-side rate limiting
- ✅ Keep Secrets Updated: If you rotate your API key, update both Google Cloud and GitHub Secrets