Structure
EnvironmentVariablesProvider
A configuration provider that reads values from environment variables.
- iOS 18.0+
- macOS 15.0+
- tvOS 18.0+
- visionOS 2.0+
- watchOS 11.0+
struct EnvironmentVariablesProvider
Mentioned In
Overview
This provider reads configuration values from environment variables, supporting both the current process environment and .env files. It automatically converts hierarchical configuration keys into standard environment variable naming conventions and handles type conversion for all supported configuration value types.
Key transformation
This provider transforms configuration keys into environment variable names using these rules:
Joins components with underscores.
Converts all characters to uppercase.
Detects CamelCase and marks word boundaries with underscores.
Replaces non-alphanumeric characters with underscores.
For example: http.serverTimeout becomes HTTP_SERVER_TIMEOUT
Supported data types
The provider supports all standard configuration types:
Strings, integers, doubles, and booleans
Arrays of strings, integers, doubles, and booleans (comma-separated by default)
Byte arrays (base64-encoded by default)
Arrays of byte chunks
Secret handling
You can mark environment variables as secrets using a SecretsSpecifier. The provider automatically redacts secret values in debug output and logging.
Important
This provider performs case-insensitive lookup of environment variable names.
Usage
Reading environment variables in the current process
// Assuming the environment contains the following variables:
// HTTP_CLIENT_USER_AGENT=Config/1.0 (Test)
// HTTP_CLIENT_TIMEOUT=15.0
// HTTP_SECRET=s3cret
// HTTP_VERSION=2
// ENABLED=true
let provider = EnvironmentVariablesProvider(
secretsSpecifier: .specific(["HTTP_SECRET"])
)
// Prints all values, redacts "HTTP_SECRET" automatically.
print(provider)
let config = ConfigReader(provider: provider)
let isEnabled = config.bool(forKey: "enabled", default: false)
let userAgent = config.string(forKey: "http.client.user-agent", default: "unspecified")
// ...
Reading environment variables from a .env-style file
// Assuming the local file system has a file called `.env` in the current working directory
// with the following contents:
//
// HTTP_CLIENT_USER_AGENT=Config/1.0 (Test)
// HTTP_CLIENT_TIMEOUT=15.0
// HTTP_SECRET=s3cret
// HTTP_VERSION=2
// ENABLED=true
let provider = try await EnvironmentVariablesProvider(
environmentFilePath: ".env",
secretsSpecifier: .specific(["HTTP_SECRET"])
)
// Prints all values, redacts "HTTP_SECRET" automatically.
print(provider)
let config = ConfigReader(provider: provider)
let isEnabled = config.bool(forKey: "enabled", default: false)
let userAgent = config.string(forKey: "http.client.user-agent", default: "unspecified")
// ...
Config context
The environment variables provider ignores the context passed in context.
Topics
Creating an environment variable provider
init(secretsSpecifier: SecretsSpecifier<String, String>, bytesDecoder: some ConfigBytesFromStringDecoder, arraySeparator: Character)Creates a new provider that reads from the current process environment.
init(environmentVariables: [String : String], secretsSpecifier: SecretsSpecifier<String, String>, bytesDecoder: some ConfigBytesFromStringDecoder, arraySeparator: Character)Creates a new provider from a custom dictionary of environment variables.
init(environmentFilePath: FilePath, allowMissing: Bool, secretsSpecifier: SecretsSpecifier<String, String>, bytesDecoder: some ConfigBytesFromStringDecoder, arraySeparator: Character) async throwsCreates a new provider that reads from an environment file.
Inspecting an environment variable provider
Relationships
Conforms To