Back to all posts
Backend7 min read·November 22, 2026

Keeping API Keys and Secrets Out of Your Mobile App

TB
ThynkBlox Apps Team
App Engineering

You cannot hide an API key inside a Flutter app, or any mobile app, in a way that stops a determined person from finding it. Obfuscation, encoding and build-time variables only make extraction slower. Keys designed to be public can ship if you restrict them. Real secrets belong on a server that you control, and the app should call that server instead.

Why can't a secret be hidden inside an app?

An app is a package that you hand to every user. Anyone can download it, unpack it and inspect its files and strings, and with some effort the network requests it makes. The device is not under your control, so nothing stored on it is private from the person holding it.

Common tricks do not change this:

  • Splitting a key into pieces and joining them at run time. The joined value can still be read from memory or from the request it is sent in.
  • Encoding or encrypting the key inside the app. The app needs the decryption method to use the key, so the method ships too.
  • Keeping the key in a configuration file that is left out of version control. This keeps it out of your repository, but it still ends up in the built app.
  • Code obfuscation. It renames symbols to make the code harder to read, but it does not encrypt resources or stop reverse engineering, and the key must still be sent with each request, where a determined user can capture it.

A useful rule: if the app can use a value, a determined user can read it. Decide for each key whether you can live with that.

Which keys are designed to be public?

Some identifiers exist so that client apps can identify themselves to a service. Their safety comes from restrictions applied on the service side, not from secrecy. Check your provider's documentation to confirm how each of your keys is meant to be used, and which restrictions it offers.

Typical restrictions to look for:

  • Limiting the key to your app's package name and signing certificate. These values can be imitated, so treat this as one layer, not the only one.
  • Limiting the key to the specific services your app needs.
  • Setting usage quotas and alerts, so abuse becomes visible early.
  • Using separate keys for testing and production.

A public key with no restrictions is effectively an open invitation. Treat the restriction step as part of shipping the key.

Which keys are safe to ship, and which are not?

Key typeSafe to ship?What to do
Public client identifier that the provider documents as client-sideYes, with restrictionsRestrict to your app identity and required services; set quotas and alerts
Publishable key for a payment or similar service, documented as client-sideYes, with restrictionsUse only for the actions the provider allows from a client; confirm outcomes on your server
Secret or server key for any serviceNoKeep on your server only; call the service from there
Database admin credentialsNoNever place in an app; give the app access only through your backend
App signing keys and upload keysNoKeep them secret, with the account owner or in secure storage outside the app; only the certificates made from them are meant to be shared
User login tokensNot shipped, but stored on the deviceStore in secure device storage; see [how to store login tokens securely](/blog/store-login-tokens-securely-mobile-app)
Key from a third-party SDK that you do not fully understandUnknownRead what it can do, then restrict it or move it behind your server

If you are unsure which row a key belongs in, treat it as a secret until the provider's documentation says otherwise.

How do you move a secret behind your own backend?

The pattern is the same for almost every service. The app talks to your server, and your server talks to the service using the secret.

  1. List what the app does with the key. For example: send an email, call a paid data service, or create a payment.
  2. Create one endpoint per action. Make it narrow, such as "send receipt" rather than "forward anything to the service".
  3. Store the secret on the server. Keep it in the server's protected configuration, not in the code repository.
  4. Authenticate the caller. Require the signed-in user's token, so only real users can trigger the action.
  5. Validate the input. Check the request, apply per-user limits and reject anything unexpected.
  6. Return only what the app needs. Do not pass the service's raw response back if it contains more than the screen requires.
  7. Remove the old key from the app and rotate it. Assume the old value has been exposed and replace it with a new one.

A worked example, with illustrative figures only: an app sends order confirmation emails through a paid email service. If the service key sits in the app, anyone who extracts it could send many emails at your cost. If the app calls your "send confirmation" endpoint, which accepts only a valid order for the signed-in user, abuse is limited to that user's own orders.

If you do not yet have a backend, see how to choose a backend for your first app. If you have one, check whether it is ready for a mobile app. A small proxy endpoint also adds running costs, covered in estimating backend running costs.

What about Flutter-specific options like environment files?

Flutter lets you pass values at build time, for example with the --dart-define option. These are useful for switching between staging and production, which our guide on separating staging and production covers. They are not a way to protect secrets, because the values still end up in the compiled app.

Use build-time configuration for values that are fine to be public. Keep everything else on the server.

Do third-party SDKs add risk?

Yes. An SDK may need its own key, request extra permissions or send data to its provider. Before adding one, check what the key allows, whether it can be restricted, and what the SDK sends. Our guide to evaluating third-party SDKs gives criteria you can check yourself.

What should you check before release?

  • Every key in the app is on your list, with its type and purpose recorded.
  • No secret, server or admin key is present in the code, assets, configuration files or build settings.
  • Every public key is restricted to your app identity and only the services it needs.
  • Quotas and usage alerts are set for each paid service.
  • Test and production keys are separate.
  • Any key that was ever in the app and should not have been has been rotated.
  • Backend endpoints that use secrets require a signed-in user and apply limits.
  • Logs and error messages do not print keys or tokens.
  • Keys are not committed to the code repository history. If one was, rotate it.
  • The Google Play release checklist is complete.

What common mistakes should you avoid?

Relying on obfuscation. It slows a reader down. It does not remove the key.

Shipping an unrestricted public key. Public does not mean unprotected. Apply the restrictions the provider offers.

Fixing the app but not the key. Old versions stay installed on users' phones. If a secret has shipped, rotate it, then plan how to handle old versions, as in keeping the API compatible with old app versions.

Building a proxy that forwards everything. An open relay lets anyone use your secret through your server. Keep endpoints narrow and authenticated.

Treating the backend as optional. If your app truly needs a secret, it needs a server component. Plan it into the scope early.

When does this not apply?

If an app uses only on-device features and public, restricted identifiers, it may hold no secrets at all, and there is nothing to move. Also, a value the user supplies themselves, such as their own key for a service, is theirs to store, though it still needs secure device storage.

If you want help reviewing what your app ships, see our services or get in touch.

What questions do founders often ask?

Is it safe to put an API key in an environment file in Flutter?

Only if the key is meant to be public and is restricted. Whether its values are compiled in or the file is packaged with the app, they ship to every user and can be extracted. Secrets should stay on your server and be used from there.

Does code obfuscation protect my keys?

No. Obfuscation makes the code harder to read, but it does not stop reverse engineering, and the app must still send the key with its requests. Treat it as a minor delay, not protection, and keep secrets off the device.

What should I do if a secret key already shipped in a release?

Replace the key with a new one at the provider, move the call behind your server, and release an updated app. Treat the old value as exposed, and check the provider's usage records for unexpected activity.

Do I need a backend just to hide one key?

If the service requires a secret key, yes. It can be a single small, authenticated endpoint rather than a full system. Choosing a provider with a client-safe option, restricted properly, may avoid the need.

Ready when you are

Planning an app of your own?

Tell us where your project is today. We will help you work out the scope before you commit to a budget.