How to Configure Environment Variables on DigitalOcean App Platform
Configure environment variables on DigitalOcean App Platform by navigating to your app's Settings tab in the Control Panel and adding variables at the app-level (accessible to all components) or component-level (service-specific, overriding app-level). Set scopes to RUN_TIME for runtime access or BUILD_TIME for build-only access, enable Encrypt for sensitive values, and use bindable variables like ${APP_URL} to reference dynamic app-specific values.
Prerequisites
- A DigitalOcean account with an existing App Platform app deployed
- Access to the DigitalOcean Control Panel at cloud.digitalocean.com/apps
- Basic understanding of environment variables and their use in applications
- For bindables: linked managed databases or app domains already configured
- Optional: familiarity with app spec YAML for advanced configurations
Step-by-Step Instructions
Log in to the DigitalOcean Control Panel
Navigate to the Apps section at https://cloud.digitalocean.com/apps. Select your existing app from the list or create a new one by clicking Create App. Ensure you have the correct app selected before proceeding to configure environment variables.
Add App-Level Environment Variables During App Creation
When creating a new app, scroll to the App-level environment variables section on the Create an App screen. Click Add environment variable and enter a Key (e.g., NODE_ENV) and Value (e.g., production). Select a Scope from the dropdown—use RUN_TIME for runtime access or BUILD_TIME for build-only access. Toggle Encrypt to hide sensitive values from build, deployment, and application logs. Click Save or Create App to apply the variables.
Add Component-Level Runtime Variables After App Creation
In the Apps section, click your app, then select the Settings tab. Click the target component (e.g., Web Service, Worker, or Static Site) from the component list. In the Environment VariablesEdit, then Add environment variable. Fill in the Key (e.g., DATABASE_URL) and Value (e.g., postgres://user:pass@host:port/db). Select Scope as RUN_TIME for runtime access, toggle Encrypt for sensitive data, and click Save. The component will automatically redeploy with the new variables.
Add App-Level Environment Variables After App Creation
In the Apps section, click your app, then the Settings tab. Scroll to App-Level Environment Variables and click Edit. Click Add environment variable and enter a Key (e.g., APP_ENV) and Value (e.g., staging). Select a Scope and toggle Encrypt if the value is sensitive. Click Save. These variables are immediately accessible to all components at both build and runtime without requiring a full app redeploy.
Use Bindable Variables to Reference Dynamic Values
Bindable variables allow environment variables to reference dynamic app-specific values provided by DigitalOcean. Follow steps 3 or 4, but set the Value to a bindable expression using the syntax ${BINDABLE_NAME}. Common app-wide bindables include ${APP_URL} (e.g., https://my-app.ondigitalocean.app), ${APP_DOMAIN} (application's primary domain), and ${APP_ID} (application's unique ID). For managed databases, use component-specific bindables like ${postgres-db.HOSTNAME}, ${postgres-db.PORT}, ${postgres-db.DATABASE}, ${postgres-db.USERNAME}, and ${postgres-db.PASSWORD}. These values are automatically resolved at build and runtime. Example: set Key to URL and Value to ${APP_URL}.
Define Build-Time Variables via App Spec (YAML)
For advanced configurations, edit your app's app.yaml specification file to define build-time environment variables. Add an envs section to your service or component with scope: BUILD_TIME. Example structure:
services: - name: my-service envs: - key: BUILD_VAR value: build-value scope: BUILD_TIMEBuild-time variables are available only during the build phase and are useful for build tools, compilers, or dependency managers. These variables are not available at runtime.
Set Build-Time Variables for Dockerfiles Using --build-arg
If you are deploying a Dockerfile-based application, environment variables are only available at build time if you set them using the --build-arg option before deploying. Use the Docker CLI to pass build arguments: docker build --build-arg EXAMPLE=your-value -t my-image .. In your Dockerfile, reference the argument with ARG EXAMPLE and use it in build commands. When deploying to App Platform, ensure these arguments are configured in your app spec or passed during the build process.
Encrypt Sensitive Environment Variables
For any environment variable containing sensitive information (API keys, database passwords, tokens), toggle the Encrypt option when adding or editing the variable. This hides the variable value from all build logs, deployment logs, and application logs, protecting sensitive data from accidental exposure. Encrypted variables are decrypted only when the application accesses them at runtime. Always encrypt database credentials, API keys, and authentication tokens.
Verify Environment Variables via the Console Tab
After adding or modifying environment variables, verify they are correctly set by opening the Console tab in your app's settings. Run the command printenv | grep KEY_NAME to check if a specific variable is available at runtime, replacing KEY_NAME with your variable name. For example, printenv | grep NODE_ENV will display the value of the NODE_ENV variable. If the variable does not appear, check that it is set at the correct level (app-level or component-level) and that the scope is RUN_TIME.
Monitor Deployment and Redeploy if Needed
Changes to environment variables trigger automatic redeployments of affected components, which typically take 5-10 minutes to complete. Monitor the deployment progress in the Deployments tab of your app. If a deployment fails or variables do not take effect after the deployment completes, manually trigger a redeploy by clicking the Redeploy button on the component. Check application logs in the Logs tab to identify any errors related to missing or incorrect variable values.
Common Issues & Troubleshooting
Environment variables are not visible or not taking effect in the application.
Verify that you are checking the correct level—component-level variables override app-level variables with the same key. Use the Console tab and run printenv | grep KEY_NAME to confirm the variable is set at runtime. If the variable is missing, check that the scope is set to RUN_TIME (not BUILD_TIME) and that the component has finished redeploying. Manually redeploy the component if needed.
Bindable variables are not resolving to their expected values.
Ensure you are using the exact bindable syntax: ${BINDABLE_NAME} with curly braces and dollar sign. Verify that the bindable exists for your app—app-wide bindables like ${APP_URL} are always available, but database bindables like ${postgres-db.HOSTNAME} require a linked managed database. Check the app spec or documentation for the correct bindable name and spelling.
Build-time variables are not available during the Docker build process.
For Dockerfile deployments, build-time variables must be passed using the --build-arg option in the Docker CLI before deploying to App Platform. Alternatively, define build-time variables in the app spec with scope: BUILD_TIME. Note that build-time variables are not available at runtime—use RUN_TIME scope for runtime access.
Sensitive variable values are visible in logs or deployment output.
Ensure the Encrypt toggle is enabled when adding or editing the variable. Encrypted variables are hidden from build logs, deployment logs, and application logs. If the variable was already created without encryption, edit it and enable the Encrypt option, then redeploy the component.
Changes to environment variables are not taking effect after 10+ minutes.
Check the Deployments tab to confirm the component has finished redeploying. If the deployment is still in progress, wait for it to complete. If the deployment has completed but variables are still not updated, manually trigger a redeploy by clicking the Redeploy button. Verify the variable is set at the correct level (app-level or component-level) and check application logs for any errors related to the variable.