Blog / Build with AI /

Environment variables for beginners

There’s a mistake almost every beginner makes once. You get an API key, paste it straight into your code, push the project somewhere public, and a few hours later you’re reading an email about unusual activity on your account.

Environment variables exist to stop that. An environment variable is a named value that lives outside your code, in the setting where your program runs. Your code asks for it by name, and the actual value stays out of the files you share.

The analogy

Think of a house key. You wouldn’t write “the key is under the blue pot” on the front door and then hang the door in a public gallery. You’d keep the key somewhere separate and just tell the people who need it where to find it.

Your code is the door. The environment variable is the key. Anyone can see the instructions (“get the key called API_KEY”), but the key itself is kept elsewhere.

What it looks like

In your code, instead of this:

const apiKey = 'sk-abc123-this-is-my-secret';

you write this:

const apiKey = process.env.API_KEY;

The second line reads the value from the environment when the program starts. The secret isn’t in the file, so sharing the file doesn’t share the secret.

The .env file

On your own computer, the usual way to set these is a plain text file called .env, in your project folder:

API_KEY=sk-abc123-this-is-my-secret
SITE_NAME=My Test Site
DEBUG=true

A small helper (in Node.js, a package called dotenv) reads this file when the program starts and loads the values. Other languages have the same idea.

The crucial rule: never upload the .env file. If you use Git, add .env to a file called .gitignore, which lists things Git must skip. Do that before your first commit, not after. If a secret has already been committed once, it stays in the project’s history even after you delete the file, and the only safe fix is to generate a new key. Git basics for non-developers explains how this tracking works.

Why it matters beyond secrets

Secrets are the headline reason, but environment variables also help with settings that differ between places:

  • Your laptop vs the live server. The database address, the debug setting and the site URL are usually different. With variables, the same code runs in both places.
  • Test vs live keys. A payment provider might give you a test key and a live key. You swap one for the other without touching the code.
  • Sharing with a team. Each person has their own .env, so nobody sees someone else’s keys.

Where the values go when you publish

Your .env file stays on your computer. When you deploy your project, you tell the hosting service the values another way. Most hosts, including the small free ones, have a “settings” or “environment” page where you type name and value pairs. Your app reads them just as it did locally.

On shared WordPress hosting the idea shows up in a different costume: sensitive settings go in wp-config.php rather than in theme files, which is why you shouldn’t paste keys into a theme’s functions file that you might share or sell.

Working with AI

AI coding tools tend to do two things you should watch.

First, they sometimes put placeholder keys straight into the code (“YOUR_API_KEY_HERE”) and then lose track of them. Ask explicitly: “Read all secrets from environment variables and tell me which ones I need to set.”

Second, never paste a real key into a chat with an AI tool. Treat anything you paste as shared. Use a fake value in the prompt and add the real one in your .env yourself.

A prompt that works:

Update this code so the API key, database URL and site name are read from environment variables. Create an example file called .env.example with placeholder values, and add .env to .gitignore. List every variable I need to set.

The .env.example file is a handy habit. It’s a copy of .env with fake values, safe to share, which tells other people (or future you) what needs filling in.

Common mistakes

  • Putting secrets in front-end code. Environment variables protect secrets on a server. Anything sent to the visitor’s browser can be read by the visitor, even if it started life as a variable. If you want to learn what that split means, read front end vs back end explained for beginners.
  • Committing the file by accident. Check your project’s files list before the first upload.
  • Forgetting to restart. Most programs read variables when they start. Change the file, and the old value stays until you restart.
  • Typos in names. API_KEY and APIKEY are different things. The code gets “undefined”, and the error message rarely tells you why.
  • Sharing keys in screenshots. It happens all the time. Crop before posting.

What to do if a key leaks

  1. Don’t panic, but act quickly.
  2. Go to the provider’s dashboard and revoke or regenerate the key.
  3. Update your .env and any hosted settings with the new key.
  4. Check the provider’s usage page for anything you don’t recognise.
  5. Make sure the file is now ignored by Git.

Deleting the file isn’t enough. Assume the old key is known to someone.

Key takeaways

  • An environment variable keeps a value outside your code.
  • Use a .env file locally and your host’s settings page when live.
  • Add .env to .gitignore before the first commit.
  • Never paste real keys into AI chats or screenshots.
  • If a key leaks, replace it. Don’t just delete it.

FAQ

Is a .env file encrypted? No. It’s plain text. It’s about keeping secrets out of shared code, not hiding them from someone who has access to your computer.

Do I need this for a tool with no API? Not for secrets, but it’s still tidy for settings.

If you already have a project with a key sitting in the code, go and move it today. Which of your projects is it?