modal.secret

modal.secret.Secret

class Secret(modal.object.Object)

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 page for more information.

def __init__(self, *args, **kwargs):

from_dict

@staticmethod
def from_dict(
    env_dict: dict[
        str, Union[str, None]
    ] = {},  # dict of entries to be inserted as environment variables in functions using the secret
):

Create a secret from a str-str dictionary. Values can also be None, which is ignored.

Usage:

@app.function(secrets=[modal.Secret.from_dict({"FOO": "bar"})])
def run():
    print(os.environ["FOO"])

from_local_environ

@staticmethod
def from_local_environ(
    env_keys: list[str],  # list of local env vars to be included for remote execution
):

Create secrets from local environment variables automatically.

from_dotenv

@staticmethod
def from_dotenv(path=None, *, filename=".env"):

Create secrets from a .env file automatically.

If no argument is provided, it will use the current working directory as the starting point for finding a .env file. Note that it does not use the location of the module calling Secret.from_dotenv.

If called with an argument, it will use that as a starting point for finding .env files. In particular, you can call it like this:

@app.function(secrets=[modal.Secret.from_dotenv(__file__)])
def run():
    print(os.environ["USERNAME"])  # Assumes USERNAME is defined in your .env file

This will use the location of the script calling modal.Secret.from_dotenv as a starting point for finding the .env file.

A file named .env is expected by default, but this can be overridden with the filename keyword argument:

@app.function(secrets=[modal.Secret.from_dotenv(filename=".env-dev")])
def run():
    ...

from_name

@staticmethod
def from_name(
    label: str,  # Some global identifier, such as "aws-secret"
    namespace=api_pb2.DEPLOYMENT_NAMESPACE_WORKSPACE,
    environment_name: Optional[str] = None,
    required_keys: list[
        str
    ] = [],  # Optionally, a list of required environment variables (will be asserted server-side)
) -> "_Secret":

Reference a Secret by its name.

In contrast to most other Modal objects, named Secrets must be provisioned from the Dashboard. See other methods for alternate ways of creating a new Secret from code.

secret = modal.Secret.from_name("my-secret")

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