# Secrets | Modal Docs

- **URL:** https://modal.com/docs/guide/secrets
- **Summary:** Securely provide credentials and other sensitive information to your Modal Functions with Secrets.

Copy page

Secrets
=======

Securely provide credentials and other sensitive information to your Modal Functions with Secrets.

You can create and edit Secrets via the [dashboard](https://modal.com/secrets)
, the command line interface ([`modal secret`](https://modal.com/docs/cli/latest/secret)
), and programmatically from Python code ([`modal.Secret`](https://modal.com/docs/sdk/py/latest/modal.Secret)
).

To inject Secrets into the container running your Function, add the `secrets=[...]` argument to your `app.function` or `app.cls` decoration.

Limits 

Each key-value pair in a Secret is subject to the following limits:

*   **Key names** can be at most **16,384 characters** long. They must contain only letters, digits, and underscores, and cannot start with a digit.
*   **Values** can be at most **32,768 characters** long.

If you need to provide larger values to your containers, consider writing the data to a [Volume](https://modal.com/docs/guide/volumes)
 and reading it at runtime.

Deploy Secrets from the Modal Dashboard 

The most common way to create a Modal Secret is to use the [Secrets panel of the Modal dashboard](https://modal.com/secrets)
, which also shows any existing Secrets.

When you create a new Secret, you’ll be prompted with a number of templates to help you get started. These templates demonstrate standard formats for credentials for everything from Postgres and MongoDB to Weights & Biases and Hugging Face.

Use Secrets in your Modal Apps 

You can then use your Secret by constructing it `from_name` when defining a Modal App and then accessing its contents as environment variables. For example, if you have a Secret called `secret-keys` containing the key `MY_PASSWORD`:

    @app.function(secrets=[modal.Secret.from_name("secret-keys")])
    def some_function():
        import os
    
        secret_key = os.environ["MY_PASSWORD"]
        ...

Each Secret can contain multiple keys and values but you can also inject multiple Secrets, allowing you to separate Secrets into smaller reusable units:

    @app.function(secrets=[\
        modal.Secret.from_name("my-secret-name"),\
        modal.Secret.from_name("other-secret"),\
    ])
    def other_function():
        ...

The Secrets are applied in order, so key-values from later `modal.Secret` objects in the list will overwrite earlier key-values in the case of a clash. For example, if both `modal.Secret` objects above contained the key `FOO`, then the value from `"other-secret"` would always be present in `os.environ["FOO"]`.

Create Secrets programmatically 

In addition to defining Secrets on the web dashboard, you can programmatically create a Secret directly in your script and send it along to your Function using `Secret.from_dict(...)`. This can be useful if you want to send Secrets from your local development machine to the remote Modal App.

    import os
    
    if modal.is_local():
        local_secret = modal.Secret.from_dict({"FOO": os.environ["LOCAL_FOO"]})
    else:
        local_secret = modal.Secret.from_dict({})
    
    
    @app.function(secrets=[local_secret])
    def some_function():
        import os
    
        print(os.environ["FOO"])

If you have [`python-dotenv`](https://pypi.org/project/python-dotenv/)
 installed, you can also use `Secret.from_dotenv()` to create a Secret from the variables in a `.env` file

    @app.function(secrets=[modal.Secret.from_dotenv()])
    def some_other_function():
        print(os.environ["USERNAME"])

Interact with Secrets from the command line 

You can create, list, and delete your Modal Secrets with the `modal secret` command line interface.

View your Secrets and their timestamps with

    modal secret list

Create a new Secret by passing `{KEY}={VALUE}` pairs to `modal secret create`:

    modal secret create database-secret PGHOST=uri PGPORT=5432 PGUSER=admin PGPASSWORD=hunter2

or using environment variables (assuming below that the `PGPASSWORD` environment variable is set e.g. by your CI system):

    modal secret create database-secret PGHOST=uri PGPORT=5432 PGUSER=admin PGPASSWORD="$PGPASSWORD"

Remove Secrets by passing their name to `modal secret delete`:

    modal secret delete database-secret

[Secrets](https://modal.com/docs/guide/secrets#secrets)
[Limits](https://modal.com/docs/guide/secrets#limits)
[Deploy Secrets from the Modal Dashboard](https://modal.com/docs/guide/secrets#deploy-secrets-from-the-modal-dashboard)
[Use Secrets in your Modal Apps](https://modal.com/docs/guide/secrets#use-secrets-in-your-modal-apps)
[Create Secrets programmatically](https://modal.com/docs/guide/secrets#create-secrets-programmatically)
[Interact with Secrets from the command line](https://modal.com/docs/guide/secrets#interact-with-secrets-from-the-command-line)

See it in action

[OpenAI Secret for LangChain RAG](https://modal.com/docs/examples/potus_speech_qanda)

[Write to Google Sheets](https://modal.com/docs/examples/db_to_sheet)
