Back to blog

Exposing OpenAI or Gemini Keys in Frontend: How to Fix It with Edge Functions

Exposing OpenAI or Gemini API keys in client-side code puts your budget at risk. Learn how to secure your app by routing requests through Edge Functions.

Oct 19, 2026
7
directions
notes from delivery practice
3
levels
strategy · process · tools
1
library
guides · checklists · cases
See cases

Why Leaking AI Credentials to the Client Puts Your Business at Risk

Integrating artificial intelligence into web applications has become standard practice for modern digital products. Whether it is an intelligent search feature, content generation, or a customer support bot, teams rush to deliver AI capabilities. However, rapid prototyping often introduces a severe security flaw: calling OpenAI or Google Gemini APIs directly from client-side JavaScript.

When your frontend communicates with an AI provider directly, your private API key is shipped inside the bundle downloaded by every user. Anyone can inspect network traffic, copy the credentials, and route their own heavy workloads through your account. For business owners, this vulnerability translates into unexpected invoices, degraded service performance, and sudden account suspensions due to quota exhaustion.

How Secret Keys End Up in the Frontend

  • Public environment variables: Framework prefixes such as NEXT_PUBLIC_ or VITE_ automatically inline values into the production build, exposing them publicly.
  • Enabling dangerouslyAllowBrowser: The official OpenAI Node/JS SDK explicitly blocks browser execution unless this flag is passed, which directly warns against credential exposure.
  • AI code generation shortcuts: AI coding assistants frequently generate self-contained components that call LLM APIs directly in React or Vue without setting up a backend layer.

How to Verify If Your Application Is Vulnerable

Auditing your web application for exposed AI keys takes only a few minutes:

  • Inspect DevTools Network tab: Trigger the AI functionality on your site. If outgoing HTTP requests hit api.openai.com or generativelanguage.googleapis.com directly, your secret key resides in the browser.
  • Scan the codebase: Search your repository for key formats (such as sk-proj- or AIza) and mentions of client-side bypass flags.
  • Review provider billing logs: Check your consumption graphs. Unfamiliar spikes in token usage usually indicate third-party exploitation.

The Solution: Move AI Workloads to Edge Functions

The only robust architecture is delegating all AI interactions to an intermediary server layer. Using lightweight Edge Functions (such as Supabase Edge Functions, Cloudflare Workers, or Vercel Serverless) provides low latency, strong security, and cost efficiency.

Implementation Roadmap

  • Store credentials securely: Store your API keys strictly within server-side secrets or environment managers. These values must never be exposed to frontend bundlers.
  • Deploy an authentication-gated function: Build an Edge Function that verifies whether the incoming caller is logged in, validates input constraints (such as payload size), and attaches the API key on the server.
  • Update the client interface: Replace direct AI calls with an invocation of your custom Edge endpoint. Remove the provider SDK completely from client dependencies.
  • Rotate leaked keys: After the server endpoint is deployed and functional, generate a fresh API key in your AI console, store it in your server secrets, and permanently delete the leaked key.

Best Practices for Production AI Architecture

Hiding your key behind an Edge Function prevents theft, but your proxy endpoint must also be protected against abuse. At ViAcore, we recommend combining secret management with operational safeguards:

  • Rate Limiting: Enforce strict request thresholds per user or IP address to prevent automated budget draining.
  • Budget alerts: Configure hard and soft spending limits within OpenAI or Google Cloud Console to receive instant notifications when thresholds are reached.
  • Strict input sanitization: Truncate long prompts and enforce schema validation before forwarding requests to the model, saving substantial token costs.

By decoupling frontend user interfaces from underlying AI credentials, your business retains full control over access, data privacy, and infrastructure spending.

Services on this topic

Need this for your project?

Development turnkey: analysis → implementation → launch → support. Tell us about your task — we will reply with an estimate.

About the direction

Relevant articles