Managing Application State with Heroku Config Vars
Learn how to use Heroku Config Vars to separate sensitive credentials and environment settings from your source code, including CLI implementation and runtime behavior.
14 May 2026, 17:02 UTC

Decoupling Configuration from Code
Hardcoding API keys, database URLs, or environment-specific flags into your source code creates security vulnerabilities and deployment bottlenecks. The solution is Config Vars: environment variables injected into your Heroku dynos (virtualized containers) at runtime. By using Config Vars, you can use the same build artifact across staging and production environments, changing only the variables that define the environment.
How Config Vars Work
When a dyno starts, Heroku injects the defined Config Vars into the process environment. Your application accesses these using standard language libraries. Because these variables reside outside the git repository, sensitive credentials never enter your version control system.
Implementation Example: Node.js and Python
To set a variable, use the Heroku CLI from your local terminal. You must be logged in and have the appropriate permissions for the app.
# Run this from your local machine in the project directory
# Syntax: heroku config:set KEY=VALUE --app app-name
heroku config:set STRIPE_API_KEY=sk_test_4eC39HqLy
heroku config:set APP_COLOR=blue
Within your application code, retrieve these values using the environment lookup method for your specific language:
| Language | Access Method | Example Code |
|---|---|---|
| Node.js | process.env |
const key = process.env.STRIPE_API_KEY; |
| Python | os.environ |
import os; key = os.environ.get('STRIPE_API_KEY') |
| Ruby | ENV |
key = ENV['STRIPE_API_KEY'] |
Operational Impact and Limitations
Changing a Config Var is not a silent update. Every time you run heroku config:set or update a value via the Dashboard, Heroku triggers a rolling restart of all active dynos. This ensures that the new environment state is propagated to every process.
- Restart Overhead: Frequent updates to variables can lead to temporary instability or a spike in startup latency as dynos cycle.
- Character Limits: Config Vars are intended for short strings (keys, flags, URLs). Attempting to store large JSON blobs or certificates may hit platform character limits.
- Visibility: Any user with 'view' or 'manage' permissions on the Heroku app can see the values of these variables in the dashboard. For highly sensitive secrets, consider a dedicated secret management vault.
Verification and Diagnostics
To verify that your variables are correctly set and active, use the following diagnostic steps:
- CLI Audit: Run
heroku configto list all current variables for the application. This confirms the platform has registered the key/value pair. - Log Monitoring: Check the Heroku Activity Log or run
heroku logs --tailduring a config change to confirm the dyno restart sequence was triggered. - Runtime Check: Create a temporary, protected diagnostic route in your app that returns the value of a non-sensitive Config Var (e.g.,
APP_COLOR) to ensure the application process is reading the environment correctly.
Rollback Procedure
If a configuration change causes the application to crash (e.g., a typo in a database URL), you must manually revert the variable to its previous state:
# Revert to the previous known-good value
heroku config:set DATABASE_URL=postgres://previous_valid_url
This will trigger another restart, restoring the application to its functional state.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.