cd /news/developer-tools/stop-pasting-api-keys-into-your-term… · home › topics › developer-tools › article
[ARTICLE · art-151329] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Stop Pasting API Keys Into Your Terminal: Use Windows Environment Variables Instead

A developer has published a walkthrough showing how Windows System Environment Variables can store API keys and secrets at the operating system level, eliminating terminal history leaks and accidental GitHub commits. The approach sets credentials once through Windows settings and references them in code via process.env in Node.js or os.getenv() in Python, separating configuration from source files. The writeup argues that environment variables, a standard since 1970s Unix systems and used by cloud platforms like Heroku, Vercel and AWS Lambda, are foundational rather than advanced.

by read9 min views2 publishedOct 16, 2026

TL;DR: Windows System Environment Variables store API keys and secrets at the operating system level, eliminating terminal history leaks and accidental GitHub commits. Set them once through Windows settings, reference them in code via process.env or os.getenv(), and never paste another credential into a terminal again.

Every developer has done it at least once. You copy an API key from OpenAI or Anthropic, paste it directly into a terminal command or test script, and run your code. It works. You move on. A few weeks later, you push that script to GitHub without thinking. Your key is now public. Forever.

Terminal history leaks and accidental commits are not edge cases. They are the default outcome when you treat secrets like throwaway strings. Windows System Environment Variables exist to solve this exact problem, yet most developers skip them because they assume environment variables are "too advanced" or "only for production."

That assumption is wrong. Environment variables are not advanced. They are foundational. Once you understand how they work, you will never paste another API key into a terminal again.

Windows System Environment Variables are key-value pairs stored inside Windows itself. They live at the operating system level, not inside your code, not inside your terminal session, and not inside any file you will ever commit to version control.

Each variable has a name and a value. The name is how your application references it. The value is what your application retrieves. You set them once through Windows settings, and every application running under your user account can read them.

Two types exist: User Environment Variables and System Environment Variables. User variables apply only to your Windows user account. System variables apply to every user on the machine. As a developer, you will almost always use User Environment Variables because you want secrets scoped to you alone.

When you store a variable like OPENAI_API_KEY, Windows makes it available to every process you launch. Node.js can read it. Python can read it. PowerShell can read it. Your text editor can read it. You set it once. It persists across reboots. It never touches your codebase.

This is not new technology. Environment variables have been part of operating systems since Unix systems in the 1970s. They are battle-tested, widely supported, and built into every major programming language runtime. They are also the standard way production environments manage secrets in cloud platforms, CI/CD pipelines, and containerized deployments.

Environment variables have been part of operating systems since Unix systems in the 1970s, and they remain the industry standard for managing secrets in production environments today.

The core reason is simple: separation of configuration from code. Your source code should describe logic, not contain live credentials. Hard-coding an API key into a script ties your code to one environment, one key, and one security failure waiting to happen.

Environment variables decouple secrets from source files. Your code references process.env.OPENAI_API_KEY in Node.js or os.getenv("OPENAI_API_KEY") in Python. The actual key lives in Windows, completely separate from your repository.

This separation unlocks three immediate benefits:

process.env.OPENAI_API_KEY but not the actual key. Production deployments use environment variables for the same reasons. Heroku, Vercel, AWS Lambda, and every major cloud platform inject secrets as environment variables at runtime. Learning to use them locally means you are already practicing production patterns on your own machine, which directly ties into AI tools impact your data privacy in modern deployment workflows.

Let's walk through what actually happens when you paste an API key directly into PowerShell or a script file.

You open PowerShell. You run a command like this:

powershell

curl -H "Authorization: Bearer sk-proj-abc123yourkeyhere" https://api.openai.com/v1/models The command works. You get your response. What you don't see is PowerShell writing that entire command, including your API key, into its history file.

On Windows, PowerShell history is stored at %APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt where anyone with access to your user account can extract every command you've ever run.

Anyone with access to your user account can open that file and read every command you have ever run. If you share your machine, if you back up that folder to cloud storage, or if malware scans common history paths, your key is exposed.

The second failure mode happens when you paste a key into a script file. You create test-openai.js and write:

javascript

const apiKey = "sk-proj-abc123yourkeyhere";

fetch("https://api.openai.com/v1/models", {

  headers: { Authorization: `Bearer ${apiKey}` }

});

You test it. It works. You keep working. A week later, you commit that file to GitHub without realizing the key is still inside. GitHub scans for exposed secrets and may revoke the key automatically, but that depends on pattern matching. If you obfuscated it slightly or if it doesn't match known patterns, the key stays public.

Even if you delete the file later, it remains permanently accessible in Git commit history. Anyone who clones your repository can run git log and extract the key from an earlier commit. Deletion doesn't remove history. It just hides it from the current branch view.

Here's a real failure case. You initialize a new Node.js project and create test.js:

javascript

const apiKey = "sk-proj-realkeyfromanthropichere";

console.log("Testing API key:", apiKey);

You run it:

bash

node test.js

Output:

plaintext

Testing API key: sk-proj-realkeyfromanthropichere You forget about the file. Two weeks later, you run git add . and git commit -m "initial commit" without checking what's staged. The key is now in your repository forever. You can remove the file in a later commit, but the original commit still contains it. Anyone who forks or clones your repo can retrieve it.

The fix is simple: never put the key in the file in the first place.

They can be reviewed using a system configuration audit checker. Here's how to store an API key as a User Environment Variable on Windows, then reference it in your code.

Press Win + S and search for "environment variables." Click Edit the system environment variables. In the System Properties window, click Environment Variables at the bottom.

You will see two sections: User variables (top) and System variables (bottom). Click New under User variables.

In the New User Variable dialog:

OPENAI_API_KEY sk-proj-yourrealkeyhere Click OK, then OK again to close all dialogs. The variable is now stored in Windows.

Environment variables are loaded when a process starts. If you already have PowerShell, Command Prompt, or VS Code open, close and reopen them. The new variable will be available in all new sessions.

In Node.js:

javascript

const apiKey = process.env.OPENAI_API_KEY;

console.log("API key loaded:", apiKey ? "Yes" : "No");

In Python:

python

import os

api_key = os.getenv("OPENAI_API_KEY")

print(f"API key loaded: {'Yes' if api_key else 'No'}")

In PowerShell:

powershell

$apiKey = $env:OPENAI_API_KEY

Write-Host "API key loaded: $($apiKey -ne $null)"

Your code now reads the key from Windows at runtime. The key never appears in your source file. You can commit test.js or test.py to GitHub safely. Anyone who clones your repo will see process.env.OPENAI_API_KEY but not the actual key.

Run your script again:

Expected output:

plaintext

API key loaded: Yes

If you see "No", you either mistyped the variable name or forgot to restart your terminal.

Using environment variables changes how you write, share, and deploy code. Here's what you gain immediately:

No more terminal history leaks. You never paste a key into a command again. Your PowerShell history file contains no secrets. If you share your machine or back up that folder, no credentials are exposed.

No more accidental commits. Your source files reference process.env.OPENAI_API_KEY, not the actual key. You can commit every script, push to GitHub, and share your code publicly without exposing credentials.

Portable code. Your teammate clones your repo, sets their own OPENAI_API_KEY variable on their machine, and runs the same code without modification. Different developers, different keys, identical codebase.

Production-ready patterns. You are already using the same secret management approach that Heroku, Vercel, AWS Lambda, and every major cloud platform uses. When you deploy, you set environment variables in the platform's dashboard. Your code stays identical.

Easier key rotation. You can change an API key by updating one Windows setting. You don't hunt through every script where you hard-coded it. You update the variable once, and every script that references it picks up the new value.

Separation of configuration from code is not just a best practice. It is the foundational pattern that makes modern DevOps workflows possible.

This approach scales beyond API keys. Database connection strings, OAuth client secrets, feature flags, deployment URLs, and any configuration that changes between environments should live in environment variables. Your code references the variable. The environment provides the value. This pattern is outlined in detail in our DevOps best practices guide.

Even after setting environment variables correctly, developers make predictable mistakes. Here are the most common:

Logging the variable value during debugging. You write console.log(process.env.OPENAI_API_KEY) to verify it loaded, then forget to remove the line. Now your key is in your application logs. If those logs go to a monitoring service or get committed to a repo, the key is exposed again. Log "loaded" or "not loaded", never the actual value.

Committing .env files. Many Node.js projects use .env files with libraries like dotenv. These files store environment variables locally for development. They are not environment variables in the Windows sense. They are plain text files. If you commit .env to Git, you commit all the secrets inside. Always add .env to .gitignore.

Using System Environment Variables instead of User. System variables apply to all users on the machine. If you store a personal API key as a System variable, every user account can read it. Use User variables for personal secrets, System variables only for shared configuration.

Forgetting to restart terminals. Environment variables are loaded when a process starts. If you set a variable and immediately try to use it in an already-open terminal, it won't be there. Close and reopen the terminal first.

Hardcoding fallback values. You write const apiKey = process.env.OPENAI_API_KEY || "sk-proj-fallbackkey"; intending to make the code "more robust." Now your code works even when the variable isn't set, and the fallback key ends up in your repository. Never hardcode fallback secrets. If the variable is missing, fail loudly.

You now know how to store secrets outside your codebase using Windows System Environment Variables. This is the single most effective way to prevent terminal history leaks and accidental commits.

Start by identifying every API key, database password, or secret you currently have hardcoded in a script or pasted in a terminal. Move each one to a User Environment Variable. Update your code to reference process.env.VARIABLE_NAME or os.getenv("VARIABLE_NAME"). Restart your terminal. Run your code. Verify it works.

Do this once for every secret you use. From that point forward, you will never paste a key into a terminal again. Your code will be shareable, portable, and production-ready. Your secrets will stay secret.

This is not advanced. This is foundational. Every developer should know how to do this before they run their first API call. If you skipped it until now, you haven't lost anything. You just gained the most important security habit you will ever build.

Selected: Stop Pasting API Keys Into Your Terminal: Use Windows Environment Variables Instead

Alternates:

stop-pasting-api-keys-use-windows-environment-variables security, tutorial, beginners, envvars

── more in #developer-tools 4 stories · sorted by recency
── more on @windows 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/stop-pasting-api-key…] indexed:0 read:9min 2026-10-16 · —