Environment variables
Environment variables can be used for data that you don't want to store in plain text in your app. For example, when connecting to external APIs it is recommended to not use credentials in your app code directly, but pass them to the code as environment variables. First, a section is dedicated to describing how to set environment variables for local development. Subsequently, it is described how to set up environment variables for published apps. This section ends with a short guide on how to access these environment variables in your app code.
Defining environment variables for local development
Environment variables can be set with the CLI for your local development environment. You can set environment variables with the --env
, -e
flag when
running the install
, start
and run
commands. For example, the environment variable with key FOO
and corresponding value bar
can be used as follows:
viktor-cli start --env FOO=bar
The CLI will connect the local app code to your VIKTOR environment as usual, but with the addition that the environment variable has been set on the running Python process.
Defining environment variables for published apps
Maintainers and environment administrators can create, update or remove environment variables for published apps. Environment variables can be set from the 'Apps' menu. Environment variables are encrypted at rest in your organization's database using an AES encryption algorithm. Follow the steps below to create, update or remove environment variables:
- Navigate to the 'Apps' page in your VIKTOR environment and find the app you want to set environment variables for and click 'Details'.
- Select the 'Variables' tab to create, update or remove environment variables.
- New variables can be added with the 'Add Variable' button on the top right, while variables can be updated or deleted by clicking the actions button on the corresponding row.
If an environment variable is used in the app publication process, for example for authenticating with a private Python package index, please make sure to add the variable before publishing the app.
Environment variables that start with VIKTOR_
(with the exception of VIKTOR_APP_SECRET
) are reserved for internal use by VIKTOR
and cannot be set by the user.
Accessing environment variables in your app code
Environment variables can be accessed in your app code by using the built-in os
module. Consider the example mentioned earlier FOO=bar
. After creating this variable on the 'Variables' tab it can be accessed using os.environ
or os.getenv()
. For example, the code below will assign the value bar
to the variable my_var
by retrieving the environment variable FOO
.
import os
my_var = os.getenv("FOO")