Skip to main content

Initialize Ruby SDK clients using Environment variables

· 2 min read

We have added support for initializing the Ruby SDK from environment to simplify setup and enhance developer experience.

Details

APIMatic's Ruby SDK now supports initializing clients directly from environment variables, eliminating the need for hardcoded configuration values. This streamlines environment-specific deployments, enhances credential security, and allows runtime configuration changes without code modifications.

The new initialization method supports configuration through:

  • .env files (using the dotenv gem)
  • Runtime environment variables
  • Optional overrides via keyword arguments

This reduces boilerplate and simplifies deployment across different environments and runtime contexts.

What's New

A new from_env method has been added to the Client class. This allows the SDK client to be initialized automatically using values loaded from environment variables with no manual configuration required.

Environment-Based Client Initialization

# Create client from environment
client = Client.from_env

To load values from a .env file automatically, install and use the dotenv gem:

gem install dotenv

You can populate the .env file like the sample given below.

# Set the environment
Environment=production

# Set number of retries
MAX_RETRIES=5

# Configure proxy settings
PROXY_PORT=8080

Note: require dotenv/load automatically loads .env variables into ENV, allowing from_env to read them seamlessly.

Developers can still override any values loaded from the environment by passing keyword arguments.

# Override client from environment
client = Client.from_env(
environment: Environment::TESTING,
timeout: 30
)

Developer Portal Enhancements

A new Environment-Based Client Initialization section has been added to the SDK Infrastructure section. This section contains information on how to initialize the SDK client from environment and includes .env file examples for the SDK with all configurable fields.

Learn more about Client Initialization from Environment feature