MMakerToLaunch

Guides

How to Hide API Keys Before Deploying Your App

Deploying with API keys in the wrong place can mean stolen credentials, unexpected charges, or compromised user data within minutes. This guide walks you through exactly what to check and fix before your app goes live — whether you built it with AI or from scratch.

Updated July 2026 · 10 min read

Written for AI makers using tools like Cursor, Bolt, Lovable, Replit, Claude, and ChatGPT.

Quick answer

Before deploying: search your code for hardcoded keys, move them to a .env file, add .env to .gitignore, create .env.example with placeholders, rotate any keys that may have been exposed, and add the production values to your hosting platform's environment variables panel.

Who this is for

  • You are about to deploy for the first time and want to confirm no secrets will be exposed
  • Your AI coding tool may have put API keys in places they should not be
  • You found a key in your source code and are not sure what to do

What you'll need

  • Your project folder with all source files
  • Access to your API provider dashboards (OpenAI, Stripe, Supabase, etc.)
  • Access to your hosting platform (Vercel, Railway, Render, etc.)
  • A text editor for editing .env and .gitignore

Why pre-deploy is the critical moment

Local development happens on your machine — if a key is in the wrong place but never pushed to GitHub or deployed, the risk is contained. The moment you deploy, you create a public URL. And the moment you push to GitHub, bots start scanning.

Fixing key exposure after the fact requires more steps: rotating credentials, cleaning Git history, and verifying no unauthorized usage occurred. Fixing it before deploy is a much smaller task.

This checklist is designed to be completed in under an hour for most apps. Do it once before your first production deploy. It also makes future deploys safer because the infrastructure is correct from the start.

Find hardcoded keys in your project

Open your code editor and use project-wide search (Cmd+Shift+F on Mac, Ctrl+Shift+F on Windows). Search for patterns that look like API keys: sk-, pk-, Bearer, api_key, apiKey, secret, password, and any specific key prefixes you know your providers use.

Also search for actual key values you have seen — if your OpenAI key starts with sk-proj-abc, search for that prefix. Check every file type: .js, .ts, .jsx, .tsx, .py, .env (to confirm it exists), config files, and any README or documentation files.

If your AI tool generated the project, pay special attention to generated integration files, configuration modules, and any file that imports an external SDK. AI tools sometimes embed real values in the initial setup code to make things work quickly.

Move secrets to environment variables

For every hardcoded key you found, create a corresponding entry in your .env file. In the source code, replace the literal value with an environment variable reference: process.env.OPENAI_API_KEY in Node.js and Next.js, os.environ['OPENAI_API_KEY'] in Python and Django, import.meta.env.OPENAI_API_KEY in Vite (for non-secret values only).

Test locally after each change to confirm the app still works. The environment variable name must match exactly — these are case-sensitive. If your .env has OPENAI_API_KEY but your code reads process.env.openai_api_key, it will be undefined.

Pay attention to where the code runs. If your AI tool made API calls directly from a React component, the call needs to move to a backend route (a Next.js API route, a serverless function, or a separate server) so the key stays on the server and never reaches the browser.

Update .gitignore

Open .gitignore in your project root (create it if it does not exist). Verify that .env appears on its own line. Save the file.

Then confirm the change worked: run git status or look at GitHub Desktop's file list. Your .env file should not appear as a tracked, modified, or new file. If it still appears, there is a problem — .env may already be tracked from a previous commit. In that case you need to untrack it: git rm --cached .env.

Common variations to also ignore: .env.local, .env.development.local, .env.test.local, .env.production.local. Many framework templates include these patterns already — check your existing .gitignore against your framework's recommended defaults.

Create .env.example

.env.example is a safe, committed file that documents what environment variables your app needs. It uses placeholder values instead of real ones: OPENAI_API_KEY=your_openai_key_here, DATABASE_URL=your_database_url_here.

Copy your .env file to .env.example. Go through every line and replace the real value with a descriptive placeholder. Keep the variable names identical — they are the important part. Commit .env.example to GitHub.

Future collaborators and your future self will use .env.example as a checklist. When someone clones the project, they copy .env.example to .env and fill in real values. Without this file, the app is a mystery to anyone who did not set it up originally.

Rotate any keys that may have been exposed

If a key appeared in source code at any point — even briefly — assume it may have been seen or copied. This is especially true if you pushed to GitHub, shared the code, or deployed to a public URL before today.

Rotation is fast: open the provider's API dashboard, revoke the current key, generate a new one, update your local .env, and add the new value to your hosting environment variables. The old key is invalid immediately.

Check the provider's usage logs for unexpected activity after rotating. Look for unusual request volumes, requests from unfamiliar locations, or operations you did not initiate. Report significant anomalies to the provider.

Test that your app works locally with .env

After moving keys to environment variables, restart your local development server to reload the .env file. Verify that every feature requiring an API key still works. Check the browser console and server logs for missing variable errors.

A common failure pattern: the variable name in .env and the variable name in code do not match. STRIPE_KEY in .env and process.env.STRIPE_SECRET_KEY in code will leave the app reading undefined. Match them exactly.

If you have automated tests, run them now. Any test that was previously passing with hardcoded values will continue to pass with environment variables — if the values are actually being loaded correctly.

Set environment variables in production

Your .env file stays on your development machine. The hosting platform needs the same variable names with production key values. Log into your hosting dashboard and navigate to the environment variables section.

On Vercel: Project → Settings → Environment Variables. On Railway: your service → Variables tab. On Render: Environment section of your service. Add each variable from .env.example and provide the real production value.

If you have separate staging and production environments, add variables to each. Consider using test or sandbox keys in staging where providers offer them. After adding variables, trigger a new deployment so the running app inherits the latest values.

Step-by-step

  1. 1

    Step 1

    Search for hardcoded secrets

    Use project-wide search for sk-, pk-, Bearer, apiKey, secret, and any known key prefixes. Note every file and line where a real key value appears as a string literal.

  2. 2

    Step 2

    Move keys to .env

    Create .env in your project root if it does not exist. Add each discovered key as KEY=real_value. Replace string literals in source code with process.env.KEY or the equivalent for your stack.

  3. 3

    Step 3

    Add .env to .gitignore

    Open .gitignore and add .env on its own line. Verify with git status that .env no longer appears in the tracked file list. Untrack it (git rm --cached .env) if it was previously committed.

  4. 4

    Step 4

    Create .env.example with placeholders

    Copy .env to .env.example. Replace every real value with a placeholder string. Commit .env.example. This is the only env-related file that belongs in GitHub.

  5. 5

    Step 5

    Rotate any keys that may have been exposed

    For every key that appeared in source code or was ever pushed to GitHub, open the provider dashboard, revoke the old key, generate a new one, and update .env with the new value.

  6. 6

    Step 6

    Restart and test locally

    Restart your dev server to reload .env. Test every feature that uses an external service. Confirm there are no missing-variable errors in logs or the browser console.

  7. 7

    Step 7

    Add variables to your hosting platform

    Log into your hosting dashboard. Add each variable from .env with the production key value. Redeploy. Test the live URL to confirm production API calls succeed.

Common mistakes

  • Checking .gitignore but not verifying with git status

    How to fix it: Adding .env to .gitignore does not remove it if it was already tracked. Run git status to verify. If it still appears, run git rm --cached .env.

  • Copying real values into .env.example

    How to fix it: .env.example must only contain placeholder strings. A real key in .env.example is just as exposed as one in source code — both get committed to GitHub.

  • Skipping the rotation step because you think no one saw the key

    How to fix it: You cannot know for certain. Bots scan GitHub continuously. Rotation takes two minutes and eliminates the risk entirely.

  • Moving the key to .env but forgetting to update the hosting platform

    How to fix it: The .env file only affects local development. Every key must also be entered in the hosting platform's environment variables panel for production to work.

  • Putting backend secrets in NEXT_PUBLIC_ variables to make them accessible to frontend components

    How to fix it: Move the API call to a Next.js API route or server action. The frontend calls your own endpoint; your backend calls the external service with the secret key.

Copy-ready prompt

Prompt: hide API keys before deploying

Paste this into Cursor, Claude, ChatGPT, Bolt, Lovable, or your AI coding tool.

Ready to paste

Review this project and fix API key exposure before deployment.

Tasks:
1. Search all source files for hardcoded API key values, tokens, and secret-like strings.
2. Move every secret value to .env. Replace source code literals with process.env.KEY references (or the equivalent for this stack).
3. Verify .env is in .gitignore. Add it if missing. If .env was previously committed, run git rm --cached .env.
4. Create or update .env.example with a placeholder value for every real secret in .env.
5. Identify any API calls made in frontend code that should be proxied through a backend route. Create the backend route.
6. List all keys that appeared in source code and should be rotated with the provider.
7. Summarize all files changed and list the hosting platform variables I need to set.

Paste this into Cursor, Claude, ChatGPT, Bolt, Lovable, or your AI coding tool.

Ready check

Beginner checklist

  • Searched all source files for hardcoded key patterns
  • All secrets moved to .env with process.env references in code
  • .env is listed in .gitignore
  • git status confirms .env is not tracked
  • .env.example exists with placeholder values only
  • No secret keys use NEXT_PUBLIC_ or VITE_ prefixes
  • Rotated any key that appeared in source code or Git history
  • Production keys added to hosting platform environment variables
  • App tested locally after changes — all API calls working
  • App deployed and production URL tested for live API functionality

Frequently asked questions

What if my .env file was already committed to GitHub?
Rotate every key in the file immediately — assume they have been seen. Then remove .env from tracking with git rm --cached .env, add .env to .gitignore, and commit. Optionally clean Git history to remove past commits that included the file.
Do I need to restart my dev server after editing .env?
Yes. Most frameworks read environment variables once at startup. Stop the dev server and restart it after making .env changes.
Can I store .env in a password manager instead?
Yes, that is a good practice for sharing with teammates. Store the .env contents or key values in a shared password manager entry. Never share keys over Slack, email, or by committing them.
What does 'rotate a key' mean exactly?
Rotation means generating a new key with the same permissions and invalidating the old one. In most provider dashboards, you click 'Create new key' then 'Revoke old key'. Update .env and your hosting platform with the new value.
My app broke after moving keys to environment variables. What did I do wrong?
The most common cause is a variable name mismatch: the name in .env does not exactly match what the code reads via process.env. Variable names are case-sensitive. Also check that you restarted the dev server after changing .env.