A secure and reliable gateway for your applications to interact with the Gemini AI.
By routing requests through this proxy, you enhance security and maintain control over API usage.
Method: POST
Body (JSON):
{
"prompt": "Your query for Gemini AI..."
}The Gemini model used is determined by the GENKIT_DEFAULT_MODEL environment variable, or a hardcoded fallback if that's not set.
Bearer YOUR_PROXY_API_KEY.NODE_ENV=development), any localhost origin is allowed. In other environments, only requests from the APP_DOMAIN are permitted.Create a .env file in the root of your project for local development. For deployment, set these variables in your hosting environment's settings.
A secret key your client applications will use to authenticate with this proxy service. Generate a strong, unique key.
PROXY_API_KEY=your_strong_unique_proxy_secret_keyYour API key for accessing Google's Gemini API. Obtain it from Google AI Studio or Google Cloud Console. Alternatively, if your server environment supports it (e.g., Google Cloud services), you can use Application Default Credentials (ADC), and Genkit will pick them up automatically.
GOOGLE_API_KEY=your_google_ai_gemini_api_keyImportant: This key grants access to Google AI services. Keep it secret and secure on the server. Never expose it in client-side code.
The full origin (e.g., https://your-client-app.com) of your client application that will call this proxy. This is crucial for the CORS policy to restrict access to your specified domain in production environments. During local development (NODE_ENV=development), any localhost origin is automatically allowed, and this variable is not strictly needed for CORS but is good practice to set for consistency.
APP_DOMAIN=https://myfrontendapp.comSpecifies the default Gemini model to be used by Genkit. If this variable is not set, the application falls back to a hardcoded model (currently googleai/gemini-2.0-flash). Set this to your preferred model, like googleai/gemini-pro.
GENKIT_DEFAULT_MODEL=googleai/gemini-proModel Selection (Order of Precedence):
GENKIT_DEFAULT_MODEL environment variable (if set).googleai/gemini-2.0-flash).Deploy this Next.js application to your preferred hosting provider (e.g., Firebase App Hosting, Vercel, Netlify, Google Cloud Run). Ensure all the environment variables mentioned above are correctly configured in your hosting provider's settings dashboard. The apphosting.yaml file is preconfigured for Firebase App Hosting.
In your client application (e.g., Flutter, web app, mobile app):
PROXY_API_KEY:Header('Authorization', 'Bearer YOUR_PROXY_API_KEY_HERE'){
"prompt": "Your query for Gemini AI..."
}{
"result": "Response from Gemini AI..."
}You can test the proxy endpoint using curl from your terminal. Make sure to replace YOUR_PROXY_API_KEY_HERE with your actual proxy API key. The URL in the example below uses the current page's origin; replace it if you are testing a different deployment.
curl -X POST "/api/gemini-proxy" \
-H "Authorization: Bearer YOUR_PROXY_API_KEY_HERE" \
-H "Content-Type: application/json" \
-d '{
"prompt": "Explain quantum computing in simple terms."
}'The proxy includes a basic built-in rate limiter to protect against simple abuse:
20 requests per IP address per 1 minute window.MAX_REQUESTS_PER_WINDOW and RATE_LIMIT_WINDOW_MS) are currently hardcoded in src/app/api/gemini-proxy/route.ts. For different limits, you would need to modify this file. For distributed environments, consider using a shared store like Redis for rate limiting.For more robust abuse detection, consider integrating services like Firebase App Check:
src/app/api/gemini-proxy/route.ts.