Overview
The OpenAI Ruby client can be configured through environment variables or by passing parameters directly to theOpenAI::Client constructor. Environment variables provide a convenient way to manage configuration across different environments.
Environment Variables
The following environment variables are supported:Your OpenAI API key. This is required to authenticate with the OpenAI API.
If this environment variable is not set and no
api_key parameter is provided, an ArgumentError will be raised.Your OpenAI organization ID for scoping API requests.
Your OpenAI project ID for scoping API requests.
Your webhook secret for verifying webhook signatures.
Override the default base URL for the OpenAI API.Default:
"https://api.openai.com/v1"Useful for using a proxy, testing with a mock server, or connecting to alternative OpenAI-compatible endpoints.
Configuration Priority
When both environment variables and constructor parameters are provided, constructor parameters take precedence:Default Configuration Values
The following default values are used when parameters are not explicitly provided:| Parameter | Environment Variable | Default Value | Description |
|---|---|---|---|
api_key | OPENAI_API_KEY | (required) | Your OpenAI API key |
organization | OPENAI_ORG_ID | nil | Organization ID |
project | OPENAI_PROJECT_ID | nil | Project ID |
webhook_secret | OPENAI_WEBHOOK_SECRET | nil | Webhook secret |
base_url | OPENAI_BASE_URL | "https://api.openai.com/v1" | API base URL |
max_retries | (none) | 2 | Maximum retry attempts |
timeout | (none) | 600.0 | Request timeout in seconds |
initial_retry_delay | (none) | 0.5 | Initial retry delay in seconds |
max_retry_delay | (none) | 8.0 | Maximum retry delay in seconds |
Retry Configuration
The client implements automatic retries with exponential backoff and jitter for failed retryable requests:The retry delay grows exponentially with each retry attempt and includes random jitter to prevent thundering herd problems. The delay is capped at
max_retry_delay.Timeout Configuration
Control how long the client waits for API responses:The default timeout of 600 seconds is suitable for most API operations, including streaming responses. Adjust this value based on your specific use case.
Complete Configuration Example
Best Practices
Security: Never hardcode API keys in your source code. Use environment variables or a secure credential management system.
Development vs Production: Use different API keys and organization IDs for development, staging, and production environments.