# AI Studio "Build apps" export silently inlines OS-level GEMINI\_API\_KEY into the client bundle (loadEnv with empty prefix) — USD 4,010 in unauthorized charges

**URL:** <https://discuss.ai.google.dev/t/ai-studio-build-apps-export-silently-inlines-os-level-gemini-api-key-into-the-client-bundle-loadenv-with-empty-prefix-usd-4-010-in-unauthorized-charges/188776>\
**Category:** Google AI Studio\
**Tags:** api-key\
**Created:** [October 13, 2026, 9:31am UTC](https://discuss.ai.google.dev/t/ai-studio-build-apps-export-silently-inlines-os-level-gemini-api-key-into-the-client-bundle-loadenv-with-empty-prefix-usd-4-010-in-unauthorized-charges/188776 "2026-10-13T09:31:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chihchuang](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/chihchuang/32/157092_2.png) [@chihchuang](https://discuss.ai.google.dev/u/chihchuang)\
**Post date:** [October 13, 2026, 9:31am UTC](https://discuss.ai.google.dev/t/ai-studio-build-apps-export-silently-inlines-os-level-gemini-api-key-into-the-client-bundle-loadenv-with-empty-prefix-usd-4-010-in-unauthorized-charges/188776/1 "2026-10-13T09:31:17Z")

</div>

````markdown

I want to report what I believe is an unsafe default in the AI Studio "Build apps" export, with a

reproduction and a real-world outcome. Posting publicly because I suspect others are hitting it

without realizing.

**## Summary**

The exported project's \`vite.config.ts\` loads **\*\*all\*\*** operating-system environment variables and

inlines `GEMINI_API_KEY` into the browser bundle. If your machine has `GEMINI_API_KEY` set as a

user-level environment variable — which is what the Gemini CLI setup tells you to do — any

production build you ship contains a live, billable key in plain text, with no warning.

**## The mechanism**

The template's build config does, in substance:

```ts

const env = loadEnv(mode, '.', '') // empty prefix → loads EVERY OS env var

// ...

define: {

  'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY),

}

````

Vite’s default prefix is `VITE_`, which exists precisely so that non-prefixed variables are not

exposed to client code. The empty third argument disables that protection. `define` then performs

a literal text substitution at build time, so the key ends up as a string in the output JS.

Two things make this worse than it first looks:

1. **No `.env.local` is required.** The developer never put the key in the project. It was picked up

2. **No warning is emitted.** The build succeeds silently. There is nothing in the output to suggest

a secret was just published.

**## Reproduction**

Environment: Windows, Vite 6.4.1, verified 2026-09-15.

1. Set `GEMINI_API_KEY` as a user-level OS environment variable (as per Gemini CLI setup).

2. Create a project from an AI Studio “Build apps” export. Do not create `.env.local`.

3. Run the production build.

4. Grep the output bundle for the key prefix — the key is present in plain text.

5. Change `loadEnv(mode, '.', '')` to Vite’s default prefix and rebuild — the key is absent.

**## What happened to us**

- The exported app was an internal ERP tool. **It had no AI feature whatsoever.** The browser-side

- Three bundles built 2026-03-04 to 03-06 each contained the key and were publicly served until

- First unauthorized use: **2026-06-12**. The key was evidently resold into a third-party API relay —

- Traffic ran 24/7 including weekends, 37% HTTP 400, on models our application has never called.

- Volume grew from ~1,600 to ~95,600 successful requests per half-month.

- **Total unauthorized charges: approximately USD 4,010 over 94 days** , on a project whose normal

- We discovered it ourselves. We received no abnormal-usage notification.

We have since deleted the key, removed the bundles, restricted the remaining key by IP and API,

set 85 quota overrides, and added a post-build secret scan that fails the build on any API-key

pattern. That part is handled. I am posting about the template, not about our cleanup.

**## Why I think this is a product issue, not just user error**

Google’s own documentation states that API keys must not be embedded in client-side code. The

template’s default does exactly that, automatically, for an app that did not ask for AI

functionality, reading from an environment variable that Google’s own CLI tooling tells users to

set. A developer following Google’s instructions end to end can ship a live key without ever

making a decision to do so.

**## Suggested fixes**

1. Use a non-empty prefix in `loadEnv`, so OS-level variables are not swept in.

2. Do not include a browser-side Gemini client in exports with no AI feature.

3. Fail the build, or at minimum warn loudly, when an API-key-shaped value would be inlined into

client output.

4. Default the template to a server-side proxy pattern rather than direct browser calls.

Items 1 and 3 seem cheap and would have prevented this entirely.

**## Questions**

- Is this a known issue? Has the export template been changed since March 2026?

- For anyone else who built from this template between roughly late 2025 and mid-2026: it is worth

Happy to provide the full root-cause report, reproduction output, or the nginx access logs to

anyone from the product team.

```auto

```

---

<div class="post-metadata">

**Author:** ![Payal\_Sharma2](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/payal_sharma2/32/31911_2.png) [@Payal\_Sharma2](https://discuss.ai.google.dev/u/Payal_Sharma2)\
**Post date:** [October 20, 2026, 2:32pm UTC](https://discuss.ai.google.dev/t/ai-studio-build-apps-export-silently-inlines-os-level-gemini-api-key-into-the-client-bundle-loadenv-with-empty-prefix-usd-4-010-in-unauthorized-charges/188776/2 "2026-10-20T14:32:15Z")

</div>

Thank you for reporting this. We are actively working on it.

---

<div class="post-metadata">

**Author:** ![Keyvault\_Edge](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/keyvault_edge/32/161455_2.png) [@Keyvault\_Edge](https://discuss.ai.google.dev/u/Keyvault_Edge)\
**Post date:** [November 10, 2026, 1:29pm UTC](https://discuss.ai.google.dev/t/ai-studio-build-apps-export-silently-inlines-os-level-gemini-api-key-into-the-client-bundle-loadenv-with-empty-prefix-usd-4-010-in-unauthorized-charges/188776/3 "2026-11-10T13:29:02Z")

</div>

Thanks for the detailed repro. Until the template changes, a quick fix is to drop the `define` for `process.env.GEMINI_API_KEY` from `vite.config.ts`, call Gemini from a server route (Cloud Run, a Vercel/Netlify function), and grep `dist/` for `AIza` before every deploy. It’s also worth setting a monthly spend cap on the project (AI Studio, Spend tab) so a missed key can’t run for days.

If you’d rather not run that server piece, KeyVault Edge (I build this) gives the browser a host-locked token with a monthly cost cap and a revoke button, and adds the real key at the edge: keyvaultedge .com/?utm\_source=google-ai-forum&utm\_medium=reply&utm\_campaign=warm-prospects-2026-11
