API key in your iOS app can be extracted: how to fix
An API key that ships inside an iOS app is public. Remove every secret key from the app and call the AI service from a server. Rotate each key that was ever in a build. Obfuscation and Keychain do not help. The app must contain the key to use it, so the attacker can read it.
This guide shows how to find keys in your build and which keys are safe to ship. It also shows how to move the call to a server and how to handle old app versions.
Why is a key in an iOS app not secret?
An iOS app is a file. Anyone can download it, unzip it, and read it. An IPA file is a ZIP archive. It holds the compiled code, the JavaScript bundle for React Native apps, and files such as Info.plist.
Generated apps make this worse in two ways:
- Expo inlines public variables. Every variable that starts with
EXPO_PUBLIC_goes into the JavaScript bundle as a plain string. The namePUBLICis a warning, and AI tools often ignore it. - Swift code uses constants. AI-generated Swift often puts the key in a
let apiKey = "sk-..."constant, or inInfo.plist, or in aSecrets.plistfile.
The risk is not only theory. A 2026 study from Wake Forest University tested 444 AI chatbot apps for iPhone. The Hacker News reports that 282 apps leaked paid AI access. Of these, 54 apps sent a plaintext key. The study used traffic analysis, not app cracking. The article also says that only 28% of developers had clearly fixed the problem three months after the notice.
The result of a leak is direct cost. A Kodu article on vibe-coded apps says a leaked key can cost money within minutes and tells teams to revoke the key first. See also the Rork security checklist, which lists this as a launch blocker.
How do I confirm that my app contains a key?
Check the build that you send to the store. Check it as an attacker does.
- Make a release build. For Expo, use
eas build --platform ios --profile production. Download the IPA from the build page. - Copy the IPA to a new empty folder. Unzip it:
mkdir ipa-check && cd ipa-check
cp ~/Downloads/MyApp.ipa .
unzip -q MyApp.ipa
- Open the folder
Payload/MyApp.app. For an Expo or React Native app, find the JavaScript bundle. It is usually namedmain.jsbundle. With Hermes, the bundle is bytecode, but strings stay readable. - Search the whole app folder for key prefixes:
cd Payload/*.app
grep -rEao "sk-[A-Za-z0-9_-]{20,}|sk-proj-[A-Za-z0-9_-]{20,}|AIza[0-9A-Za-z_-]{35}|sb_secret_[A-Za-z0-9_-]+|sk_live_[A-Za-z0-9]+|service_role" . | sort -u
- Use
stringsfor binary files:
strings main.jsbundle | grep -E "sk-|AIza|sb_secret|service_role|Bearer " | head -50
strings MyApp | grep -E "sk-|AIza|api[_-]?key" | head -50
- Read the plist files. Convert them to text:
plutil -p Info.plist | grep -iE "key|secret|token"
find . -name "*.plist" -exec plutil -p {} \; | grep -iE "key|secret|token"
- Search your source code and your
.envfiles for variables that start withEXPO_PUBLIC_. Read the name and the value of each one.
grep -rn "EXPO_PUBLIC_" --include="*.ts" --include="*.tsx" --include=".env*" . | grep -v node_modules
Any match for a secret key is a finding. The token leak checker can test a web build of the same app.
Warning: Do not share the IPA or the search output in public. They contain live keys.
Which keys are safe to ship and which are not?
Divide keys into two groups. A public key is made for the client. A secret key gives access to money, data, or admin rights.
| Key | Safe in the app? | Condition |
|---|---|---|
| Supabase URL | Yes | None |
| Supabase anon or publishable key | Yes | RLS must protect every table |
| Firebase config (API key, app ID) | Yes | Security rules and App Check must protect the data |
| RevenueCat public SDK key | Yes | Verify purchases on the server |
| Sentry DSN, PostHog project key | Yes | They only accept data |
Supabase service role key or sb_secret_ key |
No | Bypasses RLS |
| OpenAI, Anthropic, Gemini keys | No | Each call costs money |
| Stripe secret key | No | Controls payments |
| Any webhook secret or signing key | No | Lets an attacker forge events |
The rule is simple. If a leak lets a stranger spend money, read private data, or act as an admin, the key must not ship. A key that is safe to ship is safe only with server-side rules behind it. The anon key guide explains this for Supabase.
How do I fix it now?
Do these steps in order. The first step stops the loss. The other steps remove the cause.
- Revoke or rotate the key. Open the provider dashboard. Create a new key and delete the old one. Do this before you change any code.
- Set a spend limit. Set a hard monthly cap at the AI provider.
- Check the usage log. Look for calls that your app did not make. See the section about abuse below.
- Move the call to a server. See the next section.
- Remove the key from the source. Delete it from
.env,Info.plist,app.json,app.config.ts, and anySecretsfile. - Remove the key from git history if you pushed it. Treat the old key as public anyway.
- Build and ship a new version. Run the search from the previous section on the new build. It must find nothing.
How do I move the AI call to a server?
The app must not call the AI provider directly. The app calls your server function. The function checks the user, adds the key, and calls the provider. The key lives in a server secret.
What does the server function look like?
This example uses a Supabase Edge Function. The same shape works on Vercel, Cloudflare Workers, or Firebase Cloud Functions.
// supabase/functions/chat/index.ts
import { createClient } from "npm:@supabase/supabase-js@2";
Deno.serve(async (req) => {
const userClient = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_ANON_KEY")!,
{ global: { headers: { Authorization: req.headers.get("Authorization") ?? "" } } },
);
const { data: { user }, error } = await userClient.auth.getUser();
if (error || !user) return new Response("Unauthorized", { status: 401 });
const { prompt } = await req.json();
if (typeof prompt !== "string" || prompt.length > 2000) {
return new Response("Bad request", { status: 400 });
}
const ai = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${Deno.env.get("OPENAI_API_KEY")}`,
},
body: JSON.stringify({
model: "gpt-4o-mini",
max_tokens: 500,
messages: [{ role: "user", content: prompt }],
}),
});
return new Response(await ai.text(), {
status: ai.status,
headers: { "Content-Type": "application/json" },
});
});
Store the key as a secret and deploy:
supabase secrets set OPENAI_API_KEY=<new key>
supabase functions deploy chat
What does the app code look like?
In Expo with TypeScript, call the function with the client. The client adds the user token.
import { supabase } from "@/lib/supabase";
export async function askAI(prompt: string) {
const { data, error } = await supabase.functions.invoke("chat", {
body: { prompt },
});
if (error) throw error;
return data;
}
In Swift with supabase-swift, the call has the same shape:
struct ChatRequest: Encodable { let prompt: String }
struct ChatResponse: Decodable { let id: String }
let result: ChatResponse = try await supabase.functions.invoke(
"chat",
options: FunctionInvokeOptions(body: ChatRequest(prompt: prompt))
)
Caution: A proxy with no user check is as open as a key in the app. The server must verify the user, set a per-user limit, and choose the model. See the guide for an open AI proxy and the guide for a per-user limit.
Also read the guide for an OpenAI key in the frontend. It covers the same problem for web apps.
Why do obfuscation and Keychain not help?
Teams often try to hide the key instead of removing it. These methods do not work.
- Obfuscation. A tool can split or encode the string. The app must rebuild the real key to send the request. An attacker reads it from memory with a debugger, or reads the request from the network.
- Keychain. The Keychain protects data that the app writes at run time. A key that ships in the bundle is in the file before the app starts. The Keychain is not involved.
- Encryption with a key in the app. The decryption key is also in the app. The problem moves one step.
- Base64 or a split string. These are not protection. A short script decodes them.
- A fixed header value. The value is in the bundle, so any attacker can send it.
The study cited above did not need to open the app. It watched the network traffic. Even a perfect hide in the bundle fails when the app sends the key in a request. Only a server that holds the key removes the risk.
How do I handle old app versions?
You cannot update an app on the phone of a user. Old versions keep the old key. This has two results.
- After you rotate the key, old versions stop working. The AI feature in these versions returns an error.
- Before you rotate the key, old versions still work, and so do attackers who copied the key.
Use these steps to manage the change:
- Ship the new version that calls the server. Wait for store approval.
- Add a minimum version check on the server. The app sends its version in a header. The server returns a clear error for old versions.
- Show an update screen in the app when the server returns that error.
- Rotate the key as soon as the new version is available. A short break for old versions is better than open billing.
- Keep the old key active for no more than a few days if you must. Watch the usage every day.
The Rork checklist gives the same plan: rotate on the server, invalidate the old credential, and force the upgrade with a minimum version check.
The Apple and vibe coding article explains why store review is slow and why bundle fixes must happen before the first release.
How do I check if someone used the key?
- Open the usage page of the provider. Set the date range to the time since the first release that held the key.
- Look for use that does not match your users. Examples are night peaks, a model that your app does not call, or a large number of requests from one project.
- Check for new resources. Some providers allow fine-tuning jobs, files, or assistants that an attacker can create.
- Compare the spend with your normal cost for each active user.
- If the numbers are wrong, contact the provider. Ask the provider about a review of the bill. Revoke the key first.
How do I stop this problem in the future?
- Never put a secret key in a variable that starts with
EXPO_PUBLIC_. - Add a step to your release process. Build, unzip, and search the IPA for key prefixes.
- Run a secret scanner on the repository before each release.
- Ask the AI tool to keep all third-party API calls in server code. Then check the result.
- Check the build again after each AI code regeneration. Regeneration can bring back old patterns.
When should I get help?
Get help when the provider bill shows large use that you cannot explain. Also get help when the leaked key was a service role key or a payment key. These keys give access to your data and money. Rotate them first, then review all data for changes.
Common questions
Is an EXPO_PUBLIC_ variable secret?
Is the Supabase anon key safe to ship?
Does Keychain hide an API key that ships in the app?
Does obfuscation hide the key?
Can I rotate the key without a new app release?
Check what your app exposes
A VibeEval scan tests the web app and API that your mobile app uses, and reports backend endpoints that accept the keys or tokens that leaked from a build.
14-day free trial · Cancel anytime