Client Initialization from Environment Variables
APIMatic's SDKs support initializing clients directly from environment variables, allowing you to configure SDK settings automatically without hardcoded values. This enables seamless environment-specific deployments, secure credential management, and runtime configuration changes without modifying your code.
- .NET
- Python
- Ruby
The .NET SDK supports client initialization directly from IConfigurationSection, leveraging .NET's built-in configuration system. This allows developers to define SDK settings through multiple configuration sources such as configuration files, environment variables, user secrets, and command-line arguments.
The SDK integrates seamlessly with your existing .NET configuration setup (for example, appsettings.json or any other provider). If you don't have configuration set up yet, you can use ConfigurationBuilder as shown in the examples below to easily configure and utilize the configuration system.
The FromConfiguration Method
The FromConfiguration method has been added to both Client and Client.Builder classes. It accepts an IConfigurationSection and returns a configured client instance or a client builder instance.
Basic Client Initialization
using Sdk.Standard;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp;
// Build the IConfiguration using .NET conventions (JSON, environment, etc.)
var configuration = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddEnvironmentVariables() // [optional] read environment variables
.Build();
// Instantiate your SDK and configure it from IConfiguration
var client = SdkClient
.FromConfiguration(configuration.GetSection("SdkConfig"));
Advanced Client Initialization with Builder Pattern
using Sdk.Standard;
using Microsoft.Extensions.Configuration;
using Environment = Tester.Standard.Environment;
namespace ConsoleApp;
// Build the IConfiguration using .NET conventions (JSON, environment, etc.)
var configuration = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddEnvironmentVariables() // [optional] read environment variables
.Build();
// Instantiate your SDK builder and configure it from IConfiguration with overrides
var client = Sdk.Builder
.FromConfiguration(configuration.GetSection("SdkConfig"))
.Environment(Environment.Testing)
.HttpClientConfig(c => c.Timeout(TimeSpan.FromSeconds(60)))
.Build();
Note: You can also override configurations loaded from the
IConfigurationSectionif needed by using the builder methods onClient.Builder.
JSON Configuration Structure
Here's an example of how your config.json file should be structured to work with the above code samples:
{
"SdkConfig": {
"Environment": "testing",
"Port": "port",
"HttpClientConfig": {
"Timeout": "00:01:00",
"NumberOfRetries": 3,
"BackoffFactor": 2,
"RetryInterval": 1,
"MaximumRetryWaitTime": "00:02:00",
"StatusCodesToRetry": [408, 413],
"RequestMethodsToRetry": ["GET", "PUT", "DELETE"],
"ProxyConfiguration": {
"Address": "http://localhost:3000",
"Port": 8080,
"Tunnel": false,
"User": "username",
"Pass": "password"
}
}
}
}
Environment Variables Configuration
You can also configure individual settings using environment variables. .NET's configuration system uses double underscores (__) to represent nested configuration sections. Here are some examples:
# Set the environment
SdkConfig__Environment=production
# Set number of retries
SdkConfig__HttpClientConfig__NumberOfRetries=5
# Configure proxy settings
SdkConfig__HttpClientConfig__ProxyConfiguration__Port=8080
Configuration Precedence
When using environment variables, their precedence depends on the order in which you add configuration providers to the ConfigurationBuilder. Providers added later override values from earlier providers.
Our Python SDK supports environment driven client initialization to simplify the SDK client setup and enhance developer experience.
The from_environment Method
The from_environment() method is available in SDK Client class. It reads configuration from the process environment and, if provided/available, from a .env file. You can also pass keyword arguments to override any values resolved from the environment.
Basic Client Initialization
from sdk_client import SdkClient
# Initialize the client from environment variables
client = SdkClient.from_environment()
You can also specify a custom .env file path:
client = SdkClient.from_environment(dotenv_path="/path/to/.env")
Advanced Client Initialization with Overrides You can override environment-resolved values by passing keyword arguments. Arguments take precedence over environment variables:
from sdk_client import SdkClient
client = SdkClient.from_environment(
dotenv_path="/path/to/.env",
timeout=60,
max_retries=3,
backoff_factor=2,
retry_statuses=[408, 413],
retry_methods=["GET", "PUT", "DELETE"],
)
Example .env File
Here's an example of how your config.json file should be structured to work with the above code samples:
ENVIRONMENT=testing
PORT=80
TIMEOUT=60
MAX_RETRIES=3
BACKOFF_FACTOR=2
RETRY_STATUSES=408,413
RETRY_METHODS=GET,PUT,DELETE
# Proxy Configuration
PROXY_ADDRESS=http://localhost:3000
PROXY_PORT=8080
PROXY_USERNAME=username
PROXY_PASSWORD=password
Configuration Precedence
- Explicit keyword arguments passed to from_environment() override environment variables.
- Environment variables (including those loaded from .env) override the SDK’s built-in default values.
- If an environment variable isn't defined, the default SDK configuration value will be used.
Ruby Client Initialization from Environment
Our Ruby SDK supports environment driven client initialization to simplify the SDK client setup and enhance developer experience.
The from_env Method
The from_env method has been added to Client. It gives an option to override any values loaded from the environment and return a configured client instance.
Basic Client Initialization
# Create client from environment
client = Client.from_env
# Override client from environment
client = Client.from_env(
environment: Environment::TESTING,
timeout: 30
)
Example .env File
Here's an example of how your .env file should be structured to automatically load .env file variables into ENV so that it can work with the above code samples seamlessly:
# Set the environment
Environment=production
# Set number of retries
MAX_RETRIES=5
# Configure proxy settings
PROXY_PORT=8080