Project: Prefect (Orchestration & Management / Scheduling & Orchestration)
File: variables.md

description: Prefect variables are dynamic-named, mutable string values, much like environment variables. tags: variables blocks search: boost: 2 Variables enable you to store and reuse non-sensitive bits of data, such as configuration information. Variables are named, mutable string values, much like environment variables. Variables are scoped to a Prefect server instance or a single workspace in Prefect Cloud. Variables are intended for values with infrequent writes and frequent reads, but you can create or modify variables at any time. Variable values may be cached for quicker retrieval. Variable values are most commonly loaded during flow runtime. But you can load them in other contexts at any time to pass configuration information to Prefect configuration files, such as deployment steps. !!! warning "Variables are not Encrypted" We do not recommend using variables to store sensitive information. Instead, use to store and access sensitive information. Create, read, edit, and delete variables through the Prefect UI, API, and CLI. Names must adhere to these traditional variable naming conventions: Have no more than 255 characters Only contain lowercase alphanumeric characters ([a-z], [0-9]) or underscores (_). Spaces are not allowed. Be unique Values must have less than or equal to 5000 characters. Optionally, you can add tags to the variable. View all the variables in your Prefect server instance or Prefect Cloud workspace on the Variables page of the Prefect UI. Both the name and value of all variables are visible to anyone with access to the server or workspace. To create a new variable, select the + button next to the header of the Variables page. Enter the name and value of the variable. You can create and delete variables through the REST API. You can also set and get variables through the API with either the variable name or ID. See the for more information. List, inspect, and delete variables in the CLI with the `prefect variable ls`, `prefect variable inspect <name>`, and `prefect variable delete <name>` commands, respectively. In addition to the UI and API, you can reference variables in code and in certain Prefect configuration files. You can access any variable through the Python SDK with the `Variable.get()` method. If you attempt to reference a variable that does not exist, the method will return `None`. You can create variables through the Python SDK with the `Variable.set()` method. Note that if a variable of the same name exists, you'll need to pass `overwrite=True`. ```python from prefect.variables import Variable variable = Variable.set(name="the_answer", value="42") answer = Variable.get('the_answer') print(answer.value) answer = await Variable.get('the_answer') print(answer.value) answer = Variable.get('nottheanswer') print(answer.value) answer = Variable.get('nottheanswer', default='42') print(answer.value) answer = Variable.get('the_answer') print(answer.value) answer = Variable.set(name="the_answer", value="43", overwrite=True) print(answer.value) ``` In `.yaml` files, variables are denoted by quotes and double curly brackets, such as: `"{{ prefect.variables.my_variable }}"`. Use variables to templatize deployment steps by referencing them in the `prefect.yaml` file used to create deployments. For example, you could pass in a variable to specify a branch for a git repo in a deployment `pull` step: ``` pull: prefect.deployments.steps.git_clone: repository: https://github.com/PrefectHQ/hello-projects.git branch: "{{ prefect.variables.deployment_branch }}" ``` The `deployment_branch` variable is evaluated at runtime for the deployed flow, allowing changes to variables used in a pull action without updating a deployment directly.