dlt.secrets accessor provides secure, dictionary-like access to sensitive configuration values like API keys, passwords, and tokens. It only queries providers that support secrets storage.
Basic Usage
Methods
__getitem__(field: str)
Get a secret value by field name. Raises ConfigFieldMissingException if not found.
Secret key to retrieve. Use dot notation for nested values (e.g.,
"destination.postgres.password").get(field: str, expected_type: Type = None)
Get a secret value with optional type casting. Returns None if not found.
Secret key to retrieve.
Expected type to deserialize the value into.
expected_type, or None if not found.
Example:
__setitem__(field: str, value: Any)
Set a secret value programmatically.
Secret key to set. Use dot notation for sections.
Secret value to store.
__contains__(field: str)
Check if a secret exists.
Example:
Automatic Injection with dlt.secrets.value
Use dlt.secrets.value as a default argument value to automatically inject secrets:
Type Annotations for Secrets
Use type annotations to indicate secret parameters:Secret Storage Providers
Thedlt.secrets accessor reads from secure providers only:
- Environment variables (e.g.,
SOURCES__GITHUB__API_KEY) secrets.tomlfile in.dlt/directory- Airflow Variables/Connections (when running in Airflow)
- Google Secret Manager
- AWS Secrets Manager
- Azure Key Vault
- Other configured secret backends
config.toml as it’s not considered secure.
Secrets File Example
In.dlt/secrets.toml:
.dlt/secrets.toml to .gitignore to prevent committing secrets to version control.
Environment Variables
Secrets can be provided via environment variables:Credentials Configuration Classes
For structured credentials, useCredentialsConfiguration subclasses:
Secret Validation
dlt ensures secrets are only retrieved from secure providers. If a value marked as secret is found in a non-secure provider (likeconfig.toml), a ValueNotSecretException is raised.
See Also
- dlt.config - For accessing non-sensitive configuration values
- Credentials documentation - Detailed guide on managing credentials
- Secret providers - Available secret storage backends