API keys and secrets (your payment gateway key, your AI model key, your database password) never go inside your code, the browser or a mobile app. Store them in environment variables or a secrets store on the server, and call outside services from the server only. If a key leaks, revoke it immediately, issue a new one, then review your logs.
- how to keep api keys safe in an app
- where to store api keys
- api key leaked what to do
- can i put an api key in a mobile app
- environment variables vs hardcoded api key
- is it safe to paste api keys into ai app builder
How do you keep API keys and secrets safe in your app?
The short answer: API keys and secrets live on the server and nowhere else. You do not paste them into code files, you do not ship them to the browser, and you do not bundle them inside an iOS or Android app. They sit in environment variables or a dedicated secrets store, the backend reads them at runtime, and every call to a sensitive outside service goes through your server.
Why should a business owner care, not just the developer? Because a secret key works like a bank card with its PIN written on it. Whoever gets your payment gateway key may be able to issue refunds or see your customers' transactions. Whoever gets your AI model key can run thousands of requests on your account. Leaks are usually invisible until the bill arrives.
Building with AI makes the risk closer than it used to be. You paste a key into the chat so the integration works quickly, and the model drops it straight into a file that ships to the browser. This guide covers the kinds of secrets you have, where to keep them, how to check your app, and what to do on the day a key leaks. It is one part of the wider security checklist for AI-built apps.
Anything that reaches the user's device can be read by the user. A secret that reaches the browser is no longer a secret.
What secrets does your app actually have?
Start by knowing what you are protecting. Most small apps hold five to ten secrets, some added quietly while wiring up a service.
| Secret | Example | What happens if it leaks? |
|---|---|---|
| Payment gateway secret key | A Moyasar, Tap, HyperPay or Network International key | Refunds issued or customer transaction data exposed |
| AI model key | A language-model provider key for chat or summaries | Heavy usage on your account and a surprise bill |
| Database credentials | Connection string and password | All customer and order data read or deleted |
| Messaging key | OTP or WhatsApp messaging provider | Messages sent on your account, numbers spammed |
| Webhook signing secret | Signature on payment notifications | A fake 'paid' event for an order nobody paid |
| Storage and email keys | Image storage key, email service key | Malicious uploads or fake email in your name |
Where do secrets usually leak?
Keys rarely leak through some sophisticated hack. Most of the time they walk out through very ordinary places:
- Frontend code: a key written into a JavaScript file that every visitor downloads and can read with the browser's developer tools.
- Inside the mobile app: a published app package can be unpacked, so any key inside it can be extracted.
- On GitHub: a key went into an old commit and was later deleted, but it is still in the repository history. Public repositories are scanned automatically by many parties.
- In chats and groups: a key sent over WhatsApp to a freelancer or visible in a screenshot.
- In logs: code that prints the full request, headers included, so the key ends up in a log several people can read.
- In error pages: an error screen that shows server settings to the visitor.
The right pattern: environment variables and server-side calls
The safe pattern is simple: the frontend asks your server, and your server talks to the payment gateway or the AI model using the secret key. The user never sees the key, and on the server you can check who the user is, what they are allowed to do, and how many requests they have made before passing anything on.
- The browser calls the AI provider directly with the key
- The key sits in a settings file inside the repository
- The same key for testing and production
- One all-powerful key for everything
- A freelancer keeps the key on their laptop
- The browser calls your server, the server calls the provider
- The key lives in environment variables or a secrets store
- Separate test keys and production keys
- Each key limited to what it needs
- Access to secrets through the hosting dashboard with roles
- 1List your secrets
Write down every service connected to your app, which key type it uses, and who can access it.
- 2Move them to environment variables
Each secret is stored in the hosting settings under a clear name and read by backend code only.
- 3Separate environments
Sandbox keys in staging, live keys in production only. See staging vs production.
- 4Restrict every key
Use the narrowest permissions, restrict by domain or IP address where the service allows it, and set a monthly spending cap on AI keys.
- 5Verify signatures
Every payment webhook has its signature checked with the signing secret before an order is marked as paid.
On Fanann hosting, secrets are stored in the project settings on the server, and the agent calls payment and AI services from backend code by default. Reviewing the result before launch is still your job.
What if you are building with AI?
AI builders save a lot of time, but they sometimes do exactly what you say. If you paste a key into the chat and write 'connect payments with this key', the model may put it wherever works fastest, which is not always the safest place. The fix is to ask for the safe pattern explicitly, then check it.
Run a security review of my project focused on secrets. 1) List every API key, password or secret the app uses and where it is read. 2) Make sure no secret is written inside the code, the frontend files or the mobile app, and move any secret to server-side environment variables. 3) Route every call to the payment gateway, the AI model and the OTP messaging service through the server only, require a signed-in user, and limit requests per user per minute. 4) Verify the payment webhook signature before marking an order as paid. 5) Never print secrets in logs or error messages. 6) Keep test keys separate from production keys. Give me a short report of what you changed.
Try it in FanannThen check it yourself: open the app in your browser, open developer tools, and search the loaded files for the first characters of your secret key. If it shows up, something is wrong. For more on reviewing what the AI writes, read is AI-generated code secure?.
A key leaked? The five-step response plan
Leaks happen to professional teams too. What matters is how fast you act:
- Revoke the key immediately in the service dashboard (revoke or roll). Do not wait to find out whether it was used.
- Issue a new key, put it in your environment variables, redeploy, and confirm the service works.
- Review logs and bills: odd payments or refunds, unusual AI usage, messages sent from your account.
- Close the source of the leak: remove the key from the code, make a public repository private, and remove access for anyone who does not need it.
- Record it and notify when required: if the leak may have exposed customers' personal data, check your obligations under the data protection law that applies to you.
Secrets checklist before launch
- No secret key anywhere in the frontend files or the mobile app.
- Every secret in environment variables, with clear names and a list of who can see them.
- Test keys are separate, and live keys are only used in production.
- Every AI key has a spending cap and an alert.
- The payment webhook signature is verified before any order is confirmed.
- The repository is private, its history has no keys, and the local settings file is excluded from Git.
- When a freelancer's engagement ends, rotate the keys they saw.
- The leak plan is written down and at least two people know it.
If you connect many outside services, also read connect your app to other services with APIs and webhooks, and for payments specifically, payment security basics and 3D Secure. Your practical next step: block one hour this week to list your secrets and work through the checklist above.
Key takeaways
- A secret key belongs on the server only: not in the code, the browser or the mobile app.
- Keep test keys separate from production keys, and never test with a live key.
- Give every key the least access it needs, and put spending limits on AI keys.
- Git remembers everything: deleting a key from the latest version is not enough, you must revoke it.
- Write a five-step leak plan before you need it: revoke, replace, redeploy, review, record.
Frequently asked questions
Where should I store API keys in my app?
Can I put an API key inside a mobile app?
What should I do if my payment gateway key leaks?
Is deleting a key from GitHub enough?
How do I stop someone running up my AI API bill?
Ask your AI assistant about this
Copy this prompt into the AI assistant you use, then add the details of your own case.
I am reading this guide: “API keys and secrets: how to keep them out of your code” https://fanann.com/en/blog/api-keys-secrets-safety/ My question: how to keep api keys safe in an app Use that page as a source and explain the answer step by step for my situation, tell me what to check before I start, and cite the page link.