Function Signature
Parameters
The environment variable name that points to the secret file path
Returns
Promise<number | undefined> - The integer value from the secret file, or undefined if not found
How It Works
This is the optional variant ofsecretInt. 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 an integer
- Returns
undefinedif the secret file doesn’t exist (instead of throwing) - Still throws an error if the file exists but contains an invalid number
Difference from secretInt
- maybeSecretInt: Returns
undefinedwhen the secret file is missing - secretInt: Throws an error when the secret file is missing (unless a fallback is provided)
maybeSecretInt for truly optional integer secrets where absence is a valid state.
Example
Error Handling
Returnsundefined if:
- The secret file doesn’t exist
- The file is empty
- The file exists but cannot be parsed as a number (NaN)
