Environment Variables
Environment variables are named settings stored outside your code, such as API keys, database addresses and feature switches, that a program reads when it runs in a given environment.
Also called: environment variables, env vars, .env, env file, secrets
Environment variables are named settings stored outside your code. A program reads them when it runs. They hold things that change between environments or must stay secret: API keys, database addresses, webhook URLs and feature switches. The same script can then run safely on a laptop, in a staging environment and in production, each with its own values.
How it works
Instead of writing a key into the code, you reference a name:
CRM_API_KEY=pat-na1-xxxx
SLACK_WEBHOOK_URL=https://hooks.slack.example/xxxx
DRY_RUN=true
The code asks for CRM_API_KEY at runtime. Where the value comes from depends on where the code runs:
- Locally: a .env file in the project folder, loaded when the program starts. It is listed in the ignore file so version control never stores it.
- Hosting platforms: a settings screen per project and environment, often separate values for preview, staging and production.
- Pipelines: encrypted secrets in your CI/CD settings, injected during builds and deploys.
- Serverless functions: variables set on each serverless function at deploy time.
A shared .env.example file lists every variable name with blank values. New teammates know what to fill in without seeing anyone's secrets.
Variables are not only for secrets. A DRY_RUN switch lets a script log what it would change without touching the CRM. That one variable prevents many accidents.
Example
A GTM engineer builds an enrichment script that writes to the CRM (all details hypothetical). Compare two setups:
| Hardcoded | Environment variables | |
|---|---|---|
| Where the API key lives | In the script | In .env locally, in platform settings in production |
| Test vs live CRM | Edit the code each time | Separate values per environment |
| Sharing the code | Leaks the key | Safe to share |
| Rotating the key | Code change and redeploy | Update one setting |
In the hardcoded version, a contractor pastes the script into a public code snippet site for help debugging. The CRM key is exposed. Rotating it breaks four other automations that shared the same key. In the second version, the shared code contains only the variable name, each workflow uses its own scoped key, and the test run points at a sandbox by default.
By company size
Small business: Owners meet environment variables when connecting a website to a form tool, payment processor or email service. The rule is simple: paste keys only into the hosting platform's settings screen, never into page code or a shared doc.
Mid-market: Every GTM script and integration should read secrets from environment variables, with separate keys per workflow and per environment. Keep an inventory of which key powers what, so rotation does not cause surprise outages.
Enterprise: Secrets live in a dedicated secrets manager with access controls, rotation schedules and audit logs. Environment variables are injected at runtime from that manager. Security teams scan repos for leaked keys automatically.
Environment variables vs OAuth
An API key in an environment variable is a long-lived secret your code holds. OAuth is a sign-in flow where a user grants access and the app gets tokens that expire and refresh. OAuth tokens still need safe storage, often as environment variables or in a database, but they limit damage because they expire and can be revoked per user.
Why it matters for GTM teams
GTM engineers handle keys to the CRM, enrichment providers, ad accounts and email tools. A leaked key can expose customer data or burn through credits overnight. AI coding assistants raise the stakes, because they read and write project files quickly. We'd keep every secret in environment variables from day one, never commit .env files, use one scoped key per workflow, and add a DRY_RUN switch to anything that writes data.
Common mistakes
- Committing .env files or hardcoded keys to a repo.
- Sharing one admin-level key across every automation.
- Pasting secrets into chat tools, tickets or AI prompts.
- Pointing local tests at the production CRM by default.
- Forgetting to set variables in production, so the deploy fails at runtime.
- Never rotating keys, even after someone leaves the team.
FAQ
What is a .env file?
A .env file is a plain-text file in a project folder that lists environment variables as NAME=value lines for local development. Tools load it when the program starts. It should never be committed to version control, so projects list it in their ignore file and share an example file with blank values instead.
What should I do if I committed an API key to Git?
Treat the key as leaked, even if the repo is private. Revoke or rotate it in the vendor's dashboard right away, then remove it from the code and history. Deleting the line in a new commit is not enough, because the old commit still contains the key.
Are environment variables secure?
They are much safer than hardcoding secrets in code, but they are not a vault. Anyone with access to the server, hosting dashboard or pipeline settings can read them. Limit who has that access, use scoped keys and consider a secrets manager as the team grows.