Quick answer
Keys in .env files must follow a strict format: start with a letter (A-Z, a-z) or underscore (_), then only letters, digits, and underscores.
.env Invalid Key
Keys in .env files must follow a strict format: start with a letter (A-Z, a-z) or underscore (_), then only letters, digits, and underscores. Keys starting with a number or containing hyphens, spaces, or other characters are invalid.
Common causes
- Key starting with a number (e.g. 1ST_KEY=value).
- Key containing a hyphen (e.g. my-key=value).
- Key with spaces (e.g. my key=value).
How to fix
- Use only letters, digits, and underscores in key names.
- Start the key with a letter or underscore (e.g. MY_KEY, _PRIVATE).
- Replace hyphens with underscores (e.g. my_key instead of my-key).
- Validate with Env File Validator to get the exact line.
Examples
Bad
my-key=value
Good
MY_KEY=value
.env Invalid Key means one of your environment variable names does not follow standard key syntax. In most .env parsers, a key must start with a letter or underscore, then use only letters, digits, and underscores. That means names like API_KEY or DB_HOST are valid, while 1API_KEY, API-KEY, or API KEY are not. This validator helps developers quickly spot formatting issues that can break local development, deployment pipelines, container builds, and configuration loading in applications that rely on .env files.
How This Validator Works
This checker evaluates each environment variable name against common .env syntax rules used by many parsers and configuration libraries. It looks for invalid starting characters, unsupported symbols, and spacing issues that can prevent a key from being read correctly. The core rule is simple: the key should begin with a letter or underscore and then contain only letters, digits, or underscores.
- Checks the first character of the key name
- Flags hyphens, spaces, dots, and other unsupported characters
- Identifies keys that begin with numbers
- Helps catch formatting issues before deployment or runtime loading
Common Validation Errors
- Starts with a number:
1DATABASE_URLis invalid because keys cannot begin with digits. - Contains hyphens:
DATABASE-URLis invalid in most .env formats because hyphens are not allowed. - Contains spaces:
DATABASE URLbreaks key parsing and is not a valid variable name. - Uses special characters: Symbols like
@,#,$, or.may cause parsing errors. - Empty key name: A line without a proper key before the equals sign cannot be parsed as a valid variable.
Where This Validator Is Commonly Used
- Local development environments
- Docker and containerized application setups
- CI/CD pipelines and build systems
- Node.js, Python, PHP, Ruby, and Go projects that load .env files
- Staging and production configuration reviews
- Secrets management workflows and deployment checks
Why Validation Matters
Environment variables are often used to store application settings, service endpoints, and secret references. If a key name is invalid, the application may ignore it, fail to load configuration, or fall back to defaults unexpectedly. Validating key syntax helps reduce configuration drift, avoid runtime surprises, and make environment files easier to maintain across teams and deployment targets.
Technical Details
Most .env parsers follow a conservative identifier pattern for variable names. A common rule is:
- First character: letter or underscore
- Remaining characters: letters, digits, or underscores
- No spaces, hyphens, or punctuation in the key name
In regular expression form, this is often represented as ^[A-Za-z_][A-Za-z0-9_]*$. Exact behavior can vary slightly by parser, shell, or framework, so it is a good idea to confirm the rules used by your specific runtime or configuration library.
| Example | Status | Reason |
|---|---|---|
API_KEY |
Valid | Starts with a letter and uses underscores only |
_SECRET_TOKEN |
Valid | Starts with an underscore and contains allowed characters |
1API_KEY |
Invalid | Starts with a digit |
API-KEY |
Invalid | Contains a hyphen |
FAQ
What does “.env invalid key” mean?
It means the variable name in your .env file does not match the syntax expected by the parser. Most tools require the key to start with a letter or underscore and use only letters, digits, and underscores after that. If the name contains spaces, hyphens, or starts with a number, it is usually rejected.
Can I use hyphens in .env variable names?
In most .env formats, no. Hyphens are commonly treated as invalid characters in key names. If you need a readable name, use underscores instead, such as DATABASE_URL rather than DATABASE-URL. This keeps the key compatible with common parsers and shell-style environment handling.
Why are keys not allowed to start with a number?
Many configuration parsers and shell environments treat variable names as identifiers, and identifiers typically cannot begin with digits. Starting with a letter or underscore helps keep the name compatible across tools, frameworks, and deployment environments. It also reduces parsing ambiguity.
Are .env rules the same in every framework?
Not exactly. Many frameworks and libraries follow the same basic pattern, but some parsers have small differences in how they handle quotes, comments, whitespace, or export syntax. The safest approach is to use conservative key names that follow the common identifier pattern accepted by most tools.
What is the safest format for a .env key?
The safest format is uppercase letters, digits, and underscores, starting with a letter or underscore. Examples include APP_ENV, DATABASE_URL, and _PRIVATE_KEY. This style is widely supported and easy for teams to read and maintain.
Can spaces around the equals sign cause problems?
Spaces around the equals sign are handled differently depending on the parser. Some tools allow them, while others are stricter. Even when spaces are accepted, it is best to keep .env files consistent and avoid unnecessary formatting that could lead to confusion or parsing differences.
Does this validator check values too?
This page focuses on key name syntax, not the value assigned to the key. A key can be valid even if the value later causes a separate issue, such as an invalid URL, malformed JSON, or unsupported escape sequence. Value validation is a different check from key validation.
Why do invalid keys matter in deployment?
Invalid keys can cause configuration to be skipped or misread during builds and deployments. That may lead to missing settings, incorrect service endpoints, or unexpected defaults in production. Catching these issues early helps keep environment-based configuration predictable across local, staging, and production systems.
Related Validators & Checkers
- .env Value Validator — checks whether environment variable values are formatted correctly
- JSON Validator — useful when .env values contain JSON strings or config payloads
- URL Validator — validates endpoint values commonly stored in environment files
- Email Validator — checks email addresses used in configuration or notifications
- API Key Format Checker — helps review token-like strings and secret identifiers
- YAML Validator — useful when environment values are mirrored in deployment manifests
FAQ
- Can I use hyphens in .env keys?
- Many loaders allow it, but strict validators and some runtimes expect only [A-Za-z_][A-Za-z0-9_]*. Use underscores for portability.
Fix it now
Try in validator (prefill this example)