Error: "API keys with referer restrictions cannot be used with this API."
Root Cause: Your backend is using a referer-restricted Google Maps API key for server-side API calls. Google Places API does NOT allow referer-restricted keys for server-side requests.
You need TWO different API keys:
-
Browser Key (for frontend) - Can have referer restrictions
- Used in:
VITE_GOOGLE_MAPS_API_KEY - Restrictions: HTTP referrers (websites)
- Used in:
-
Server Key (for backend) - CANNOT have referer restrictions
- Used in:
GOOGLE_MAPS_API_KEY(backend.env) - Restrictions: Either no restrictions OR IP address restrictions
- Used in:
-
Click "+ CREATE CREDENTIALS" → "API key"
-
Name it:
STAIRS Backend Server Key(or similar) -
Click "Restrict key" tab:
Application restrictions:
- Select "IP addresses (web servers, cron jobs, etc.)"
- Add your server's IP address(es):
160.187.22.41(your server IP)- Or if using Cloudflare, add Cloudflare IP ranges (see below)
API restrictions:
- Select "Restrict key"
- Select these APIs:
- ✅ Places API
- ✅ Geocoding API
- ✅ Maps JavaScript API (if needed)
- ❌ Do NOT restrict to Maps JavaScript API only
-
Click "SAVE"
-
Copy the new API key
cd /root/stairs-new/backend
# Edit .env file
nano .env
# or
vi .env
# Find the line:
# GOOGLE_MAPS_API_KEY=old_key_here
# Replace with new server key:
GOOGLE_MAPS_API_KEY=YOUR_NEW_SERVER_KEY_HEREOr via command line:
# Backup .env first
cp /root/stairs-new/backend/.env /root/stairs-new/backend/.env.backup
# Replace the key (edit this command with your new key)
sed -i 's/^GOOGLE_MAPS_API_KEY=.*/GOOGLE_MAPS_API_KEY=YOUR_NEW_SERVER_KEY_HERE/' /root/stairs-new/backend/.env
# Verify
grep GOOGLE_MAPS_API_KEY /root/stairs-new/backend/.envpm2 restart stairs-backend
# Verify it's running
pm2 logs stairs-backend --lines 20# Test local endpoint
curl "http://localhost:5000/api/maps/places/autocomplete?input=test"
# Should return JSON with predictions, NOT errorIf your backend is behind Cloudflare, you might need to allow Cloudflare IPs:
- Get Cloudflare IP ranges: https://www.cloudflare.com/ips/
- Add them to IP restrictions in Google Cloud Console
However, for server-side calls from your backend (localhost:5000), you should use your server's actual IP address, not Cloudflare IPs.
Only add Cloudflare IPs if:
- Your backend makes API calls through Cloudflare proxy (unlikely)
- Or Google sees Cloudflare IPs in requests (check logs)
- Location:
frontend/.env→VITE_GOOGLE_MAPS_API_KEY - Usage: Browser-based Maps JavaScript API
- Restrictions: ✅ HTTP referrers (websites) - REQUIRED
- Example:
https://portal.stairs.org.in/*
- Location:
backend/.env→GOOGLE_MAPS_API_KEY - Usage: Server-side Places API, Geocoding API
- Restrictions: ❌ NO referer restrictions
- Options:
- No restrictions (easiest, less secure)
- IP address restrictions (recommended for production)
- Example IP:
160.187.22.41
After setting up the server key:
- New API key created in Google Cloud Console
- Server key has IP address restrictions (NOT referer restrictions)
- Server key has Places API enabled
-
GOOGLE_MAPS_API_KEYupdated inbackend/.env - Backend restarted (
pm2 restart stairs-backend) - Test endpoint works:
curl http://localhost:5000/api/maps/places/autocomplete?input=test - No more "REQUEST_DENIED" errors in logs
- Frontend venue autocomplete works
- Use IP restrictions for server key (not "No restrictions")
- Rotate keys if compromised
- Monitor usage in Google Cloud Console
- Set up billing alerts to prevent unexpected charges
- Use separate keys for development and production
# Verify key is set
grep GOOGLE_MAPS_API_KEY /root/stairs-new/backend/.env
# Check backend logs
pm2 logs stairs-backend --lines 50 | grep -i "maps\|places\|api"-
Key not restricted to Places API
- Fix: Enable Places API in API restrictions
-
IP address incorrect
- Fix: Check your server's actual IP:
curl ifconfig.me
- Fix: Check your server's actual IP:
-
Billing not enabled
- Fix: Enable billing in Google Cloud Console for Maps Platform
-
API not enabled
- Fix: Enable Places API in Google Cloud Console → APIs & Services → Library
Once you create the server key and update the backend, the 502 errors should be resolved!