Why
Your code may sit in a git repository; your Stripe key and database password should not. Environment variables are set per app in the console and handed to your app when it starts. Changing one needs no new build.
Add and change
- Open the app in the console and choose Environment.
- Add a name and a value, or paste several
NAME=valuelines at once. - Save. The app restarts with the new values; the build stays the same.
Values stay private to your account: the console shows the names and hides the values until you choose to show one.
Names and values
- Names use letters, digits and underscores, start with a letter or underscore, and are at most 128 characters:
DATABASE_URL,STRIPE_KEY,Logging__LogLevel__Default. - Names are case-sensitive:
DebugandDEBUGare two variables. - A value is one line. For a multi-line value such as a PEM key, store it base64-encoded and decode it in your app.
- No quotes needed: everything after the first
=is the value, spaces included.
PORT
We set PORT for every app and your app must listen on it (see Deploying). It can't be overridden: a PORT you add is ignored.
Reading them
Python
import os
db_url = os.environ["DATABASE_URL"] # fails loudly if missing
debug = os.environ.get("DEBUG", "0") == "1" # with a defaultPHP
$dbUrl = getenv('DATABASE_URL');
$debug = getenv('DEBUG') === '1';Java (Spring Boot)
# application.properties: Spring reads environment variables too
spring.datasource.password=${DB_PASSWORD}
stripe.key=${STRIPE_KEY}.NET
var dbPassword = Environment.GetEnvironmentVariable("DB_PASSWORD");
// or builder.Configuration["DB_PASSWORD"]; Section__Key maps to Section:KeyFor your database, copy the connection string from the app's Database tab into a variable such as DATABASE_URL; after you rotate the password, update it there (Databases).
The same on your machine
Keep a .env file locally (and in .gitignore) with the same names, loaded by python-dotenv, vlucas/phpdotenv, Spring profiles or dotnet user-secrets. Your code reads the same names in both places, and only the values differ.