Skip to main content
Secrets provide a dictionary of environment variables for images. Secrets are a secure way to add credentials and other sensitive information to the containers your functions run in. You can create and edit secrets on the dashboard, or programmatically from Python code. See the secrets guide for more information.

Factory methods

Secret.from_name

modal.Secret.from_name(
    name: str,
    *,
    environment_name: Optional[str] = None,
    required_keys: list[str] = [],
    client: Optional[Client] = None,
) -> Secret
Reference a Secret by its name.
name
str
required
Name of the Secret.
environment_name
str
Environment to look up the Secret in.
required_keys
list[str]
List of required environment variable names (validated server-side).
Example:
secret = modal.Secret.from_name("my-secret")

@app.function(secrets=[secret])
def run():
    ...

Secret.from_dict

modal.Secret.from_dict(
    env_dict: dict[str, Optional[str]] = {},
) -> Secret
Create a secret from a str-str dictionary.
env_dict
dict[str, Optional[str]]
required
Dictionary of environment variable name-value pairs. Values can be None (ignored).
Example:
@app.function(secrets=[modal.Secret.from_dict({"FOO": "bar"})])
def run():
    print(os.environ["FOO"])  # "bar"

Secret.from_local_environ

modal.Secret.from_local_environ(
    env_keys: list[str],
) -> Secret
Create secrets from local environment variables automatically.
env_keys
list[str]
required
List of local env var names to include for remote execution.
Example:
@app.function(secrets=[modal.Secret.from_local_environ(["MY_API_KEY"])])
def run():
    print(os.environ["MY_API_KEY"])

Secret.from_dotenv

modal.Secret.from_dotenv(
    path=None,
    *,
    filename=".env",
    client: Optional[Client] = None,
) -> Secret
Create secrets from a .env file automatically.
path
Union[str, Path]
Starting point for finding .env files. Defaults to current working directory.
filename
str
default:".env"
Name of the env file to load.
Example:
@app.function(secrets=[modal.Secret.from_dotenv(__file__)])
def run():
    print(os.environ["USERNAME"])  # From .env file
You can also specify a different filename:
@app.function(secrets=[modal.Secret.from_dotenv(filename=".env-dev")])
def run():
    ...

Properties

secret.name

secret.name -> Optional[str]
Name of the Secret, if it has one.

Manager methods

Secret.objects.create

modal.Secret.objects.create(
    name: str,
    env_dict: dict[str, str],
    *,
    allow_existing: bool = False,
    environment_name: Optional[str] = None,
    client: Optional[Client] = None,
) -> None
Create a new Secret object.
name
str
required
Name for the new Secret.
env_dict
dict[str, str]
required
Key-value pairs to set in the Secret.
allow_existing
bool
default:"False"
If True, no-op when the Secret already exists.
Example:
contents = {"MY_KEY": "my-value", "MY_OTHER_KEY": "my-other-value"}
modal.Secret.objects.create("my-secret", contents)

Secret.objects.list

modal.Secret.objects.list(
    *,
    max_objects: Optional[int] = None,
    created_before: Optional[Union[datetime, str]] = None,
    environment_name: str = "",
    client: Optional[Client] = None,
) -> list[Secret]
Return a list of hydrated Secret objects. Example:
secrets = modal.Secret.objects.list()
print([s.name for s in secrets])

Secret.objects.delete

modal.Secret.objects.delete(
    name: str,
    *,
    allow_missing: bool = False,
    environment_name: Optional[str] = None,
    client: Optional[Client] = None,
)
Delete a named Secret.
Deletion is irreversible and will affect any Apps currently using the Secret.

Build docs developers (and LLMs) love