i want add Adding the GOOGLE_API_KEY in the front and saved it to can deploy
I understand you want to add the GOOGLE_API_KEY to the frontend, but I need to clarify something important: exposing API keys directly in frontend code is a security risk as they could be extracted by anyone visiting your site.
The application is already designed with the best security practices in mind:
If you want to pre-populate the API key field in the UI for testing or development purposes, here's a safer approach:
To properly implement this approach, you need to:
NEXT_PUBLIC_GEMINI_API_KEY. Any environment variable starting with NEXT_PUBLIC_ will be exposed to the browser.GOOGLE_API_KEY you've already set up. The application will use this key automatically when users don't provide their own.The code I've provided will:
NEXT_PUBLIC_GEMINI_API_KEY if availableRemember that the best practice is to keep your API key secure on the server side, which you've already done by adding GOOGLE_API_KEY to your Vercel project.
Please make sure to add the following environment variable to your project:
An error occurred during deployment. Please fix:
The sensitive environment variable NEXT_PUBLIC_GEMINI_API_KEY is in the following files and exposed in the client:
- app/page.tsx
Please remove the sensitive environment variable from the client code – either completely or by using a server component/action to fetch the value.
You're right - exposing API keys in client-side code is a security risk, even with the NEXT_PUBLIC_ prefix. Let's fix this by removing the client-side environment variable reference and using a more secure approach:
I've fixed the security issue by:
/api/check-api-key) that safely checks if the API key is available without exposing itThis approach is much more secure because:
The deployment should now succeed since we're no longer exposing sensitive environment variables in client-side code.
The code returns the following error:
Erreur d'initialisation du modèle Gemini: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent: [429 ] You exceeded your current quota, please check your plan and billing details. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits. [{"@type":"type.googleapis.com/google.rpc.QuotaFailure","violations":[{"quotaMetric":"generativelanguage.googleapis.com/generate_content_free_tier_requests","quotaId":"GenerateRequestsPerDayPerProjectPerModel-FreeTier","quotaDimensions":{"location":"global","model":"gemini-1.5-pro"},"quotaValue":"50"}]},{"@type":"type.googleapis.com/google.rpc.Help","links":[{"description":"Learn more about Gemini API quotas","url":"https://ai.google.dev/gemini-api/docs/rate-limits"}]},{"@type":"type.googleapis.com/google.rpc.RetryInfo","retryDelay":"45s"}]
at Module.POST (/app/api/chat/route)
at async testApiConnection (/app/page)
Revise the code to address the error.
I was previewing: /