Function Signature
Parameters
The environment variable name that points to the secret file path
Returns
Promise<URL | undefined> - A URL object from the secret file, or undefined if not found
How It Works
This is the optional variant ofsecretUrl. 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 and parses it as a URL
- Returns
undefinedif the secret file doesn’t exist (instead of throwing) - Still throws an error if the file exists but contains an invalid URL
Difference from secretUrl
- maybeSecretUrl: Returns
undefinedwhen the secret file is missing - secretUrl: Throws an error when the secret file is missing (unless a fallback is provided)
maybeSecretUrl for truly optional URL secrets where absence is a valid state.
Example
Secret File Format
The secret file should contain a valid URL string:Error Handling
Returnsundefined if:
- The secret file doesn’t exist
- The file is empty
- The file exists but the value is not a valid URL
