How to Secure Groq and Gemini API Keys: Preventing Frontend Leaks in AI Apps
Quick answer: To secure Groq and Gemini API keys, you must move all AI inference logic to the server side using Next.js API Routes, Server Actions, or Vite proxy servers. Never use VITE_ or NEXT_PUBLIC_ prefixes for these keys, as they expose your credentials to the browser, leading to billing theft.
By Paula C · Kraftwire Software
· 9 min readTo secure Groq and Gemini API keys, you must move all AI inference logic to the server side using Next.js API Routes, Server Actions, or Vite proxy servers. Never use VITE_ or NEXT_PUBLIC_ prefixes for these keys, as they expose your credentials to the browser, leading to billing theft and rate-limit exhaustion. By keeping keys in server-side environment variables, you ensure they never reach the client-side bundle.
Building with "vibe-coding" tools like Lovable, Bolt.new, and Cursor has made it possible to ship AI-powered features in minutes. However, the speed of these tools often leads to a critical oversight: hardcoding API keys in the frontend. When you use a groq api key or a google gemini api key directly in a React component or a Vite environment file without proper prefixing, you are essentially handing your billing account to the public.
In SimplyScan's scans of 187 AI-built apps, 68 of those apps (36%) had at least one HIGH or CRITICAL severity issue, often involving exposed secrets. If your app is part of this group, an attacker can scrape your key and exhaust your rate limits or rack up charges on your behalf.
Why Is Your Groq API Key Leaking in the Browser?
The primary reason for leaks in modern AI apps is the misunderstanding of how environment variables work in frameworks like Next.js and Vite. In Vite, any variable prefixed with VITE_ is automatically bundled into the client-side code. Similarly, in Next.js, the NEXT_PUBLIC_ prefix makes the variable accessible to the browser.
This means anyone can open the DevTools "Network" tab, find the request, and copy your Authorization: Bearer header.
The Risk of Client-Side Inference
Even if you use a secure api key generator, the generator cannot protect a key that is intentionally sent to the client. Groq's custom LPU silicon allows for sub-second response times, which makes it tempting to call it directly from the UI for that "instant" feel. However, doing so exposes your credentials. According to SimplyScan data, security issues (high) appeared in 21 apps (11%) out of 187 scanned, frequently due to this exact pattern.
How Do You Secure Gemini API Keys in Next.js?
Securing gemini api keys requires moving the logic to the server side. In Next.js, this is done using API Routes or Server Actions. By keeping the GOOGLE_API_KEY in a .env.local file without the NEXT_PUBLIC_ prefix, the variable remains on the server and is never sent to the user's browser.
- Create a file at
app/api/chat/route.ts. - Import the Google Generative AI SDK.
- Access the key using
process.env.GOOGLE_API_KEY. - Return only the text response to the frontend.
This architecture ensures that the client only sees the final AI response, not the google api key used to generate it. This is a fundamental part of vibe coding architecture best practices. For production environments, it is recommended to use a dedicated secret management service like Google Cloud Secret Manager or HashiCorp Vault to further isolate these credentials from the application code.
What Are the Free Tier Limits for Groq API in 2026?
As of 2026, Groq remains one of the most generous providers for developers. The groq api free tier currently provides 30,000 tokens per minute and 14,400 requests per day.
For developers needing higher quality, the llama-3.3-70b-versatile model is available but often carries tighter constraints on the free tier, such as 1,000 requests per day. If you exceed these limits because your key was leaked and used by a bot, your application will stop functioning for legitimate users. You can check if your site is currently leaking these credentials by using a secret scanner.
How to Prevent Google Gemini API Key Leaks in Vite?
Vite-based tools like Bolt.new and Lovable often default to client-side execution to simplify the "vibe." To secure a google gemini api key in a Vite project, you must use a backend proxy. In February 2026, nearly 3,000 Google API keys were accidentally exposed, highlighting the scale of this issue in the developer community.
Using a Proxy Server
Instead of calling https://generativelanguage.googleapis.com/... from your React code, you should call your own backend (e.g., an Express server, a Supabase Edge Function, or a Vercel Function).
By moving the key to the backend, you mitigate the risk of code injection and credential theft. In SimplyScan's research, architecture issues (medium) appeared in 81 apps (43%), highlighting how common it is for developers to skip the proxy layer in favor of development speed.
Is It Safe to Use Groq API Keys in Cursor or Windsurf?
When using AI editors like Cursor or Windsurf, you often provide your own groq api key in the editor settings to power the coding assistant. This is generally safe because the key is stored locally on your machine and sent directly to Groq's servers from your IDE, not bundled into the app you are building.
However, you must be careful when these editors generate code for you. If you ask a "vibe-coding" agent to "add Groq support to my app," it might generate a file named groqService.ts that includes a hardcoded string or a VITE_ variable. Always review the generated code to ensure it follows a security audit checklist. You can also use the SimplyScan MCP server to scan your project for these leaks directly within Cursor or Windsurf.
How Much Does Groq API Cost if You Upgrade?
If you outgrow the free tier, Groq's pricing in 2026 is highly competitive.
Because these costs are usage-based, a leaked key can lead to significant financial liability. Unlike a chatgpt api key which might have usage caps you've manually set, some providers have auto-refill features that can be drained quickly by malicious actors. Implementing broken access control protections and keeping keys server-side is the only way to ensure your billing remains predictable.
How to Rotate a Leaked Google API Key?
If you suspect your google api key has been compromised · perhaps because SimplyScan flagged it or you noticed unusual usage in the Google AI Studio console · you must act immediately.
- Go to the Google Cloud Console or AI Studio.
- Navigate to the "APIs & Services" > "Credentials" section.
- Locate the compromised key and select "Edit."
- Click "Regenerate Key." This will provide a new string and invalidate the old one.
- Update your server-side environment variables immediately.
- Delete the old key once the new one is confirmed working.
New keys created through AI Studio will default to Gemini-only access, preventing unintended cross-service usage. While you are there, apply "API Restrictions" to the key so it can only be used for the Generative Language API, and "Application Restrictions" to limit usage to specific IP addresses if your backend has a static IP. This is a core step in any saas security guide.
Can SimplyScan Detect Exposed AI Keys?
Yes. SimplyScan is designed specifically for the needs of modern, vibe-coded applications. While traditional scanners might miss the specific patterns used by Vite or Next.js, SimplyScan detects exposed API keys, missing or weak Supabase RLS, and environment variable leaks in about 30 seconds.
In SimplyScan's scans of 187 AI-built apps, the average security score was 86 out of 100, but the 36% of apps with high-severity issues shows that even "high-scoring" apps often have one fatal flaw. You can run a free scan at simplyscan.io to check your groq api and gemini api keys for exposure. The scan also checks for speed issues, which appeared in 129 apps (69%) of the corpus, ensuring your AI features are both secure and performant.
Summary of Best Practices
- Never prefix AI keys with
VITE_orNEXT_PUBLIC_. - Always use a server-side proxy (API Route or Server Action) to call AI models.
- Use environment variables security best practices by keeping
.envfiles out of Git. - Set usage limits and alerts in your Groq and Google AI Studio dashboards.
- Regularly scan your production URL with SimplyScan to catch accidental leaks during rapid deployments.
By following these steps, you can enjoy the sub-second inference of Groq and the reasoning capabilities of Gemini without risking your account security or your users' data. Protecting your groq api key is not just about saving money · it is about maintaining the integrity of your entire AI application.
Advanced Security Triage for Teams
For teams building AI applications, securing keys is only the first step. You should implement a security triage workflow that includes regular automated scans and manual pull request reviews. When a leak is detected, the remediation SLA (Service Level Agreement) should be immediate rotation.
Furthermore, if your startup is pursuing SOC 2 compliance, providing evidence of secret management and automated vulnerability scanning is essential. Using tools like SimplyScan helps small teams gather this evidence without the overhead of enterprise-grade security suites. By integrating security into your "vibe-coding" workflow, you can maintain development velocity while ensuring your infrastructure remains robust against credential theft.
Why AI Engines Can't Find Your Lovable Site
If you are building with Lovable or v0, you might notice that AI search engines (AEO) struggle to index your site. This is often due to performance issues or missing meta tags. SimplyScan's scan includes an AEO dimension to help you rank in AI search results by identifying these visibility gaps. Speed issues, which appeared in 69% of scanned apps, are a major factor in how AI engines perceive your site's quality. Ensuring your site is both secure and fast is the key to succeeding in the 2026 AI-driven web landscape.
Frequently asked questions
What models are available on the Groq API in 2026?
In 2026, Groq supports Llama 3.3 70B, Llama 3.1 8B, and other open-source variants. These models run on Groq LPU silicon for sub-second response times, making them popular for real-time AI applications built with vibe-coding tools like Lovable and Bolt.new.
What are the Groq API free tier limits?
The Groq free tier in 2026 provides 30,000 tokens per minute and 14,400 requests per day. For higher-tier models like Llama 3.3 70B, users are typically limited to 1,000 requests per day. Exceeding these limits will result in 429 Too Many Requests errors.
How does a Google Gemini API key get leaked?
A Gemini API key is leaked when it is included in the frontend bundle. This happens if you use the key directly in client-side JavaScript or prefix it with NEXT_PUBLIC_ in Next.js. Attackers can find these keys by inspecting the Network tab in browser DevTools.
How do I hide my API key in a React app?
To hide your API key, create a backend endpoint (like a Next.js Server Action) that handles the communication. The frontend sends the user prompt to your server, the server attaches the secret API key and calls the AI provider, and then returns the response.
What should I do if my Google API key is compromised?
If your key is leaked, go to the Google AI Studio or Groq Cloud console immediately and regenerate the key. This invalidates the old string. Update your server environment variables with the new key and set usage quotas to prevent financial loss.
Can I scan my website for leaked AI API keys?
Yes, SimplyScan is specifically built to detect exposed API keys for Groq, Gemini, and OpenAI. It scans your public-facing site to see if these secrets are accessible via the frontend code. One free scan takes about 30 seconds and checks for security and speed.