* Added best practices document * Refactored global variables page * added category * move-security-page * link * Removed title from Security Best Practices * Avoid empty headings * Sentence case for headings * Remove general best practices not related to Langflow * Add bulleted list instead of numbered list * changed default secret locations to use the default secret key locations * Add periods on end of sentences * Changed title to be imperative --------- Co-authored-by: Mendon Kissling <59585235+mendonk@users.noreply.github.com>
6.7 KiB
| title | sidebar_position | slug |
|---|---|---|
| Global variables | 5 | /configuration-global-variables |
import ReactPlayer from "react-player"; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
Global variables let you store and reuse generic input values and credentials across your projects. You can use a global variable in any text input field that displays the 🌐 icon.
Langflow stores global variables in its internal database, and encrypts the values using a secret key.
Create a global variable
-
In the Langflow UI, click your profile icon, and then select Settings.
-
Click Global Variables.
-
Click Add New.
-
In the Create Variable dialog, enter a name for your variable in the Variable Name field.
-
Optional: Select a Type for your global variable. The available types are Generic (default) and Credential.
No matter which Type you select, Langflow still encrypts the Value of the global variable.
-
Enter the Value for your global variable.
-
Optional: Use the Apply To Fields menu to select one or more fields that you want Langflow to automatically apply your global variable to. For example, if you select OpenAI API Key, Langflow will automatically apply the variable to any OpenAI API Key field.
-
Click Save Variable.
You can now select your global variable from any text input field that displays the 🌐 icon.
:::info Because values are encrypted, you can't view the actual values of your global variables. In Settings > Global Variables, the Value column shows the encrypted hash for Generic type variables, and shows nothing for Credential type variables. :::
Edit a global variable
-
In the Langflow UI, click your profile icon, and then select Settings.
-
Click Global Variables.
-
Click on the global variable you want to edit.
-
In the Update Variable dialog, you can edit the following fields: Variable Name, Value, and Apply To Fields.
-
Click Update Variable.
Delete a global variable
:::warning Deleting a global variable permanently deletes any references to it from your existing projects. :::
-
In the Langflow UI, click your profile icon, and then select Settings.
-
Click Global Variables.
-
Click the checkbox next to the global variable that you want to delete.
-
Click the Trash icon.
The global variable, and any existing references to it, are deleted.
Add global variables from the environment
Custom environment variables
You can use the LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENT environment variable to source global variables from your runtime environment.
If you installed Langflow locally, you must define the LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENT environment variable in a .env file.
-
Create a
.envfile and open it in your preferred editor. -
Add the
LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENTenvironment variable as follows:LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENT=VARIABLE1,VARIABLE2Replace
VARIABLE1,VARIABLE2with a comma-separated list (no spaces) of variables that you want Langflow to source from the environment. For example,my_key,some_string. -
Save and close the file.
-
Start Langflow with the
.envfile:VARIABLE1="VALUE1" VARIABLE2="VALUE2" python -m langflow run --env-file .env:::note In this example, the environment variables (
VARIABLE1="VALUE1"andVARIABLE2="VALUE2") are prefixed to the startup command. This is a rudimentary method for exposing environment variables to Python on the command line, and is meant for illustrative purposes. Make sure to expose your environment variables to Langflow in a manner that best suits your own environment. ::: -
Confirm that Langflow successfully sourced the global variables from the environment.
-
In the Langflow UI, click your profile icon, and then select Settings.
-
Click Global Variables.
The environment variables appear in the list of Global Variables.
-
If you're using Docker, you can pass LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENT directly from the command line or from a .env file.
To pass LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENT directly from the command line:
docker run -it --rm \
-p 7860:7860 \
-e LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENT="VARIABLE1,VARIABLE2" \
-e VARIABLE1="VALUE1" \
-e VARIABLE2="VALUE2" \
langflowai/langflow:latest
To pass LANGFLOW_VARIABLES_TO_GET_FROM_ENVIRONMENT from a .env file:
docker run -it --rm \
-p 7860:7860 \
--env-file .env \
-e VARIABLE1="VALUE1" \
-e VARIABLE2="VALUE2" \
langflowai/langflow:latest
:::info When adding global variables from the environment, the following limitations apply:
-
You can only source the Name and Value from the environment. To add additional parameters, such as the Apply To Fields parameter, you must edit the global variables in the Langflow UI.
-
Global variables that you add from the environment always have the Credential type. :::
:::tip
If you want to explicitly prevent Langflow from sourcing global variables from the environment, set LANGFLOW_STORE_ENVIRONMENT_VARIABLES to false in your .env file:
LANGFLOW_STORE_ENVIRONMENT_VARIABLES=false
:::
Default environment variables
Langflow automatically detects and converts some environment variables into global variables of the type Credential, which are applied to the specific fields in components that require them. Currently, the following variables are supported:
OPENAI_API_KEYANTHROPIC_API_KEYGOOGLE_API_KEYCOHERE_API_KEYGROQ_API_KEYHUGGINGFACEHUB_API_TOKENSEARCHAPI_API_KEYSERPAPI_API_KEYAZURE_OPENAI_API_KEYAZURE_OPENAI_API_VERSIONAZURE_OPENAI_API_INSTANCE_NAMEAZURE_OPENAI_API_DEPLOYMENT_NAMEAZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAMEPINECONE_API_KEYASTRA_DB_APPLICATION_TOKENASTRA_DB_API_ENDPOINTUPSTASH_VECTOR_REST_URLUPSTASH_VECTOR_REST_TOKENVECTARA_CUSTOMER_IDVECTARA_CORPUS_IDVECTARA_API_KEYAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY
For information about other environment variables and their usage, see Environment Variables.
Security best practices
For information about securing your global variables and other sensitive data, see Security best practices.