Function Signature
Parameters
The environment variable name that points to the secret file path
Returns
Promise<string | undefined> - The string value from the secret file, or undefined if not found
How It Works
This is the optional variant ofsecretString. It reads secrets from the filesystem without throwing errors when the file is missing:
- Checks if the environment variable
keycontains a file path - If no path is set, defaults to
/run/secrets/{key} - Reads the file contents as a UTF-8 string (trimmed)
- Returns
undefinedif the secret file doesn’t exist or is empty (instead of throwing)
Difference from secretString
- maybeSecretString: Returns
undefinedwhen the secret file is missing or empty - secretString: Throws an error when the secret file is missing or empty (unless a fallback is provided)
maybeSecretString for truly optional secrets where absence is a valid state.
Example
Docker Secrets Usage
undefined instead of crashing your application.
Error Handling
Returnsundefined if:
- The secret file doesn’t exist
- The file is empty
