← All posts software engineering

Don't Ship Your Gemini API Key in Your App: How I Moved to a Server Proxy

G
Gary Vincent
2026-11-095 min read
Don't Ship Your Gemini API Key in Your App: How I Moved to a Server Proxy

If your app calls an AI API directly, the API key is in your app. Not hidden, not obfuscated enough to matter: in it. I learned this the hard way when the Google Cloud project behind EverList was suspended for "abusive activity consistent with hijacking", and then, after I replaced the key, suspended a second time.

This is what went wrong, why rotating the key did not fix it, and the architecture I moved to instead.

How the key ended up in the app

EverList uses Gemini for a few features. Like my other apps, it read the API key from local.properties at build time and baked it into the app as a BuildConfig constant. The shared Kotlin client then called the Gemini REST endpoint directly with that key. It is convenient, it works in development, and it ships the key to every person who downloads the app.

Anyone can download an Android app from Google Play and open it with a decompiler such as jadx. String constants, including API keys, are right there. You do not need to be a skilled attacker for this, and keys scraped this way get used by other people at your expense, which is what triggers the abuse detection and the suspension.

Why rotating the key does not help

My first instinct each time was to rotate the key. It stops the abuse for a while, but the replacement key has to live in the next build of the app, so it ships in the next APK and the same thing can happen again. Rotation resets the clock. It does not remove the problem.

The only real fix is that no key exists on the device.

The fix: a proxy that holds the key, plus App Check

The pattern I moved to has three parts.

  1. A server-side function holds the key. A Firebase Cloud Function (a callable function) receives the request from the app, adds the Gemini key, calls Gemini, and returns the result. The key is stored as a secret in Secret Manager and never appears in the app or in the repository.
  2. App Check restricts who can call it. Firebase App Check proves a request is coming from a genuine copy of your app, using Play Integrity on Android and App Attest on iOS. The function rejects anything without a valid token, so someone who finds the function's URL cannot just script calls to it.
  3. A kill switch. A Remote Config flag turns the AI features off without shipping an update if something looks wrong.

The function side is small. The important part is the two options on the callable:

export const generateJson = onCall(
  { secrets: [GEMINI_KEY], enforceAppCheck: true },
  async (req) => {
    // validate the request, call Gemini with GEMINI_KEY.value(), return the result
  }
);

On the app side, the shared Kotlin code calls the function through the Firebase SDK instead of calling Gemini directly, and the GEMINI_API_KEY build field is deleted entirely. Deleting it is deliberate: if the key is no longer in the build, nobody can accidentally put it back.

The gotchas that cost me time

  • The first deploy can fail on build permissions. Cloud Functions needs a few roles granted to the project's build service account before the first deploy succeeds. The error message points at the missing permission.
  • The first call from the app can return 401. The function's Cloud Run service needs to allow invocation, and the real protection is App Check inside the function. Without that, opening up invocation would be unsafe, so do not skip enforceAppCheck.
  • App Check on its own does not protect a bundled key. App Check only guards endpoints that check it. A raw Gemini key in the app is still exposed even if App Check is turned on, which is why the key has to move to the server, not just sit behind App Check in the console.
  • Debug builds need a debug token. Register the debug token App Check prints so you can keep developing.
  • Set a budget alert. Google's abuse detection reacts after the fact. A budget alert on the billing account is what actually warns you early.

A checklist you can copy

  1. Search your app for any API key that ships in the build: BuildConfig, plist files, hard-coded strings.
  2. For each paid third-party API, move the call to a server function and store the key as a secret.
  3. Turn on App Check and enforce it on that function.
  4. Add a remote kill switch for the feature.
  5. Remove the key from the build config so it cannot come back.
  6. Set a billing budget alert on the project that owns the key.

The best way to keep a key from being stolen is to make sure the app never has one.

Do this first, not later

EverList was where I learned it, and it was the only app that got hit. My other apps were not released yet, so they never had the problem. Now every one of my apps follows this pattern, so none of them ships an API key at all. There is nothing in the app to extract, which is a much better position than hoping nobody looks.

It takes very little time, and it saves real money. A suspended project and an emergency migration cost far more than doing it properly up front.

If you are starting a new app that will call any paid API, set up the proxy and App Check first, before the feature exists. Adding it at the start means the key is never in the app, never in a build, and never something you have to remember to clean up later. The apps that get into trouble are the ones where the shortcut was meant to be temporary and was forgotten.

The same shared-Kotlin approach I describe in How to Move Fast: Reusable Cross-Platform Code in Kotlin kept the client change small, and for the wider picture of how my apps are built, see Why I Build iOS and Android Apps with Compose Multiplatform.

Tagged: software engineering · security · firebase · api keys · android · ios

Discussion

No comments yet. Be the first.