Add Environment Variables

Use environment variables to configure your app without changing code. Perfect for secrets, API keys, and other configuration values.

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.

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
  • 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.

mkdir -p environment-variables-tutorial
cd environment-variables-tutorial

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

export APP_NAME="My Environment Variables Demo"

2. Create your FastHTML app

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

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-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:

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