Menu

API Key Obfuscator

Extract hardcoded API keys from your code and generate proper .env files. Guided security workflow — not fake encryption.

API Key & .env Studio Security Workflow
Ready
Code with API Keys 0 lines • 0 chars
Cleaned Code 0 lines • 0 chars
Security workflow: Detect hardcoded keys → extract to .env → replace with env var references → generate .env, .env.example, and .gitignore. Copy the outputs to your project.
Critical: API keys in browser-side code are always visible to anyone who opens DevTools. This tool moves keys to .env files, which are only secure when used server-side. For client-side API calls, use a backend proxy or a service that issues short-lived tokens. Never ship long-lived API keys to clients.
Complete Guide to API Key Security & .env Files

The Real Problem This Tool Solves

Developers routinely hardcode API keys in source code. Common patterns look like:

const API_KEY = 'sk-1234567890abcdef';
const DB_URL = 'postgres://user:pass@localhost:5432/mydb';
fetch(`https://api.example.com/data?key=${API_KEY}`);

These keys end up in:

  • Git commit history — even if you delete the file later
  • Build artifacts — bundled into client-side JS
  • Deployed containers — visible in process listings
  • Error logs and crash reports — leaked via stack traces

Once a key is in git history, it is compromised. Removing it requires a full history rewrite (git filter-repo) and key rotation.

What This Tool Does

StepWhat Happens
1. DetectScans your code for common API key patterns: sk-... (Stripe), AIza... (Google), ghp_... (GitHub), AWS keys, JWT tokens, database URLs, and generic long strings assigned to variables named API_KEY, SECRET, TOKEN, PASSWORD.
2. ReplaceEach detected key is replaced in your code with a language-appropriate environment variable reference: process.env.API_KEY (Node.js), os.environ['API_KEY'] (Python), $_ENV['API_KEY'] (PHP), ENV['API_KEY'] (Ruby), System.getenv("API_KEY") (Java), Environment.GetEnvironmentVariable("API_KEY") (C#).
3. Generate .envProduces a valid .env file containing all extracted keys with their original values.
4. Generate .env.exampleProduces a template with placeholder values, safe to commit to git.
5. Generate .gitignoreProduces the entries you need to prevent .env files from being committed.
6. Generate JSON backupProduces a JSON file with all extracted keys for your password manager or CI/CD secret store.

What This Tool Cannot Do

  • It cannot hide API keys from clients. If your code ships to a browser, the API key is visible in DevTools regardless of where it lives in the source. .env files only protect server-side code.
  • It cannot encrypt API keys. API keys are meant to be used as plaintext tokens. Encryption at rest is a server-side concern — the tool does not implement it.
  • It cannot recover leaked keys. If a key was committed to git history, this tool cannot remove it from history. Use git filter-repo and rotate the key.
  • It cannot protect against server-side compromise. If your server is compromised, .env files can be read. Use a secrets manager (AWS Secrets Manager, HashiCorp Vault) for production.

What is a .env File?

A .env file is a plain-text file containing environment variables in KEY=value format. It is not part of the source code — it is loaded at runtime by libraries like dotenv (Node.js), python-dotenv (Python), phpdotenv (PHP), dotenv (Ruby), and similar.

# .env
API_KEY=sk-1234567890abcdef
DB_URL=postgres://user:pass@localhost:5432/mydb
STRIPE_SECRET=sk_live_xyz

Your code reads these values at runtime:

// Node.js with dotenv
require('dotenv').config();
const apiKey = process.env.API_KEY;

Why .env Files Are the Standard Solution

  • They are not committed to git — .gitignore prevents it
  • They are per-environment — development, staging, and production each have their own .env
  • They are standard across platforms — every major framework supports .env
  • They are compatible with CI/CD — you can set env vars in the CI config and skip .env entirely
  • They are compatible with Docker — docker run --env-file .env loads them
  • They are compatible with secret managers — you can inject env vars from a secrets manager

Detected API Key Patterns

ProviderPattern
Stripesk_live_..., sk_test_..., pk_live_...
GoogleAIza... (34-39 chars)
GitHubghp_..., gho_..., ghs_..., ghu_...
AWS Access KeyAKIA... (20 chars)
Slackxoxb-..., xoxp-..., xoxa-...
SendGridSG.... (long base64)
TwilioSK... (32 hex chars)
Mailgunkey-...
JWT tokenseyJ... (three base64 segments)
Database URLspostgres://, mysql://, mongodb://, redis://
GenericAny string assigned to api_key, secret, token, password, auth variables

What Happens After You Run the Tool

  1. Copy the .env file to your project root as .env (not .env.txt)
  2. Copy the .env.example file to your project root as .env.example and commit it to git
  3. Add the .gitignore entries to your existing .gitignore file (or create one)
  4. Replace the old code with the cleaned code from the tool
  5. Verify .env is not in git by running git status — the .env file should be ignored
  6. Load .env at startup using the appropriate library for your language
  7. Rotate the original keys if they were ever committed to git history

The One Rule You Cannot Break

API keys in client-side code are always visible. If your JavaScript runs in a browser, or if your mobile app ships to users, any API key in that code is exposed to anyone who opens DevTools or unpacks the app. .env files protect keys only when the code reading them runs on a server you control.

For client-side API calls, use one of these safe patterns:

  • Backend proxy — your client calls your server, your server calls the third-party API with the secret key
  • Short-lived tokens — your server issues a temporary token that the client uses for a specific action
  • Public keys only — some APIs offer a public/client-side key that is safe to expose (Stripe publishable key, Firebase web config)
  • Server-side rendering — the API call happens on the server and only the result is sent to the client

Frequently Asked Questions

Will this tool hide my API keys from clients?
No. If your code runs in a browser, .env files do not protect anything — the values are baked into the client bundle. .env files protect keys only when the code reading them runs on a server you control.
Is my code uploaded to your server?
No. Everything runs in your browser using JavaScript. Your code never leaves your device.
Does it detect all API keys automatically?
It detects common patterns (Stripe, Google, GitHub, AWS, JWT, DB URLs) and generic variable names like API_KEY, SECRET, TOKEN. Custom API keys with unusual patterns may not be auto-detected. Always review the output manually.
Can I customize the environment variable name?
The tool uses the original variable name from your code (API_KEY, DB_URL, etc.). If you want to rename it, edit the cleaned code and .env output manually.
What if I already committed an API key to git?
Rotate the key immediately — treat it as compromised. Then run git filter-repo or the BFG Repo-Cleaner to remove it from history. Simply deleting the file does not remove it from previous commits.
Does the tool support .env.local, .env.production, etc.?
The generated .env is the standard format. You can rename it to .env.local, .env.production, .env.staging, etc. — most dotenv libraries support these variants (with different loading priorities).
What about Docker Compose env files?
The generated .env is compatible with Docker Compose. Use env_file: .env in your docker-compose.yml, or docker run --env-file .env.
Does it work with all programming languages?
The tool detects keys in any language. It generates language-appropriate replacements for Node.js, Python, PHP, Ruby, Java, and C#. For other languages, it uses a generic ENV_API_KEY format that you can adapt.
Can I use this for CI/CD secrets?
Yes. The JSON backup format is compatible with most CI/CD secret managers (GitHub Actions secrets, GitLab CI variables, CircleCI env vars, Travis CI env vars). Import the JSON into your CI secret store.
Is .env committed to git by default?
No — the tool generates a .gitignore snippet that prevents .env from being committed. Make sure the .gitignore is applied to your repository root and to any subdirectories that have their own .env files.
What about frontend apps (React, Vue, Angular)?
Frontend apps ship to clients. Any key prefixed with VITE_ (Vite), REACT_APP_ (Create React App), or NEXT_PUBLIC_ (Next.js) becomes part of the client bundle and is visible to anyone. Never put server-side secrets in these prefixes. Only use them for public values (analytics IDs, CDN URLs, publishable keys).
Can I share the .env file with my team?
No. .env should never be shared over email, Slack, or any chat. Use a password manager (1Password, Bitwarden) or a secrets manager (Vault, AWS Secrets Manager) to share secrets with your team.
How do I load .env in my project?
Install the appropriate library: dotenv for Node.js, python-dotenv for Python, phpdotenv for PHP, dotenv for Ruby. Add a single line at the top of your main file to load it. See the library docs for details.
What if the tool misses a key?
Manually add it to the .env file and replace the hardcoded value in the cleaned code. The tool is a helper, not a substitute for careful code review.
Can I use this for a production deployment?
The .env format is production-ready, but for serious deployments use a proper secrets manager. .env files are fine for small teams and single-server deployments, but a secrets manager provides audit logs, rotation, and access control.

Related Tools