---
title: Add Environment Variables
description: Use environment variables to configure your app without changing code.
  Perfect for secrets, API keys, and other configuration values.
skip_exec: true
---
## What are environment variables in plash

Environment variables allow you to store configuration and sensitive information outside your codebase, making your application more secure and flexible across different environments.

Plash provides a simple way to set and use environment variables in your deployed applications.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Using environment variables in your own apps

You can use this pattern in any of your Plash apps. Simply:

1. Create a `plash.env` file in your project root
2. Add your environment variables using `export KEY=VALUE` format
3. Access them in your app (e.g. using `os.environ['KEY']`)

> [!TIP]
> Plash automatically sets environment variable `PLASH_PRODUCTION=1`, which you can use to detect whether your app is in production or development.

## Step by step tutorial

In this tutorial, you'll learn how to use environment variables in your Plash app.

**Prerequisites:**

- A registered account at [https://pla.sh](https://pla.sh)
- The Plash CLI installed (run `pip install plash-cli` if needed)
- Logged in via the CLI (run `plash-login` if needed)

### 1. Create your environment variable file

First, create a directory for your project named `environment-variables-tutorial` and navigate into it.

```python
%%bash
mkdir -p environment-variables-tutorial
cd environment-variables-tutorial
```

Then, create a file named `plash.env` in your project directory with the following content:

```python
%%writefile plash.env
export APP_NAME="My Environment Variables Demo"
```

### 2. Create your FastHTML app

Now, create a `main.py` file for your FastHTML application:

```python
%%writefile main.py
from fasthtml.common import *
import os

app, rt = fast_app()

@rt
def index():
    # Access environment variables using os.environ
    app_name = os.environ.get('APP_NAME', 'Default App Name')    
    environment = "Production" if os.environ.get('PLASH_PRODUCTION', None) else "Development"

    return Div(
        H1(app_name),
        P(f"Running in: {environment}"),
        style="font-family: system-ui; max-width: 600px; margin: 0 auto; padding: 2rem;"
    )

serve()
```

Also create a `requirements.txt` file:

```python
%%writefile requirements.txt
python-fasthtml
```

### Deploy your app

So far you've created the following files:

- plash.env
- main.py
- requirements.txt

Now you're ready to deploy your app. Run the following cli command:

```python
%%bash
plash-deploy
```

Follow the displayed link. You will see:

- The APP\_NAME environment variable as the heading
- A message describing the app is running in production

**🎉 Complete: Environment Variables Tutorial**
