Last modified: Aug 11, 2025 By Alexander Williams

Python Typer Envvar Usage Guide

Typer makes CLI development easy in Python. One powerful feature is environment variable support. This guide explains how to use it.

Why Use Environment Variables in Typer?

Environment variables help keep secrets out of code. They make apps more secure and configurable. Typer integrates this seamlessly.

Use them for API keys, database URLs, or any sensitive data. They also allow different configurations per environment.

Basic Envvar Usage in Typer

The simplest way is using the envvar parameter in options. Here's an example:

 
import typer

app = typer.Typer()

@app.command()
def greet(name: str = typer.Option(..., envvar="USER_NAME")):
    typer.echo(f"Hello {name}!")

if __name__ == "__main__":
    app()

This code first checks the USER_NAME environment variable. If not found, it prompts the user.

Multiple Envvar Sources

Typer can check multiple environment variables. Pass a list to envvar:

 
@app.command()
def connect(
    api_key: str = typer.Option(
        ...,
        envvar=["API_KEY", "SECRET_KEY"]
    )
):
    typer.echo("Connected!")

Typer checks variables in order. It uses the first one found. This provides flexible configuration.

Default Values with Envvars

Combine envvars with default values for robust apps:

 
@app.command()
def config(
    log_level: str = typer.Option(
        "INFO",
        envvar="LOG_LEVEL"
    )
):
    typer.echo(f"Log level: {log_level}")

If no envvar exists, it uses "INFO". This prevents errors while allowing customization.

Required vs Optional Envvars

Mark options as required or optional based on needs. Learn more in our Arguments vs Options guide.

Required option with envvar:

 
@app.command()
def auth(
    token: str = typer.Option(..., envvar="API_TOKEN")
):
    typer.echo("Authenticated")

Best Practices for Envvar Usage

Follow these tips for better Typer apps:

1. Prefix envvars with your app name (MYAPP_DB_URL)

2. Document all supported envvars

3. Use .env files for development

4. Provide sensible defaults when possible

For complex apps, consider our Subcommands guide.

Real-World Example

Here's a complete example with multiple options:

 
import typer

app = typer.Typer()

@app.command()
def deploy(
    env: str = typer.Option("dev", envvar="DEPLOY_ENV"),
    region: str = typer.Option(..., envvar="AWS_REGION"),
    verbose: bool = typer.Option(False, envvar="VERBOSE_MODE")
):
    typer.echo(f"Deploying to {env} in {region}")
    if verbose:
        typer.echo("Verbose mode enabled")

if __name__ == "__main__":
    app()

Run it with envvars set:


export AWS_REGION=us-east-1
python app.py deploy

Output:


Deploying to dev in us-east-1

Debugging Envvar Issues

If envvars aren't working:

1. Check variable names match exactly

2. Verify variables are exported in shell

3. Use print(os.environ) to debug

For more user input techniques, see our User Prompting Guide.

Conclusion

Typer's envvar support makes CLI apps more secure and flexible. It simplifies configuration management across environments.

Combine this with typer.Option for powerful command-line tools. Remember to document all environment variables for users.

Start using envvars in your Typer apps today for cleaner, more professional tools.