Skip to main content

Creating a Client

The OpenAI::Client class is the main entry point for interacting with the OpenAI API. Create a client instance to access all API resources.

Basic Usage

require 'openai'

client = OpenAI::Client.new(api_key: 'your-api-key')

Configuration Options

The client accepts the following initialization parameters:
api_key
string
required
Your OpenAI API key. Defaults to ENV["OPENAI_API_KEY"].
The client will raise an ArgumentError if no API key is provided either as a parameter or environment variable.
organization
string
Your organization ID for multi-organization accounts. Defaults to ENV["OPENAI_ORG_ID"].Sets the openai-organization header on all requests.
project
string
Your project ID for project-scoped requests. Defaults to ENV["OPENAI_PROJECT_ID"].Sets the openai-project header on all requests.
webhook_secret
string
Secret key for validating webhook signatures. Defaults to ENV["OPENAI_WEBHOOK_SECRET"].
base_url
string
Override the default API base URL. Defaults to ENV["OPENAI_BASE_URL"] or "https://api.openai.com/v1".Useful for testing or using proxy servers.
max_retries
integer
default:"2"
Maximum number of retry attempts for failed requests. See Retries for details.
timeout
float
default:"600.0"
Request timeout in seconds. See Timeouts for details.
initial_retry_delay
float
default:"0.5"
Initial delay in seconds before the first retry attempt. See Retries for details.
max_retry_delay
float
default:"8.0"
Maximum delay in seconds between retry attempts. See Retries for details.

Environment Variables

1

Set your API key

The most common way to configure the client is via environment variables:
export OPENAI_API_KEY="sk-..."
export OPENAI_ORG_ID="org-..."
export OPENAI_PROJECT_ID="proj_..."
2

Initialize without parameters

When environment variables are set, you can create a client without passing parameters:
client = OpenAI::Client.new

Advanced Configuration

Custom Base URL

client = OpenAI::Client.new(
  api_key: 'your-api-key',
  base_url: 'https://your-proxy.example.com/v1'
)

Multiple Organizations

# Client for organization A
client_a = OpenAI::Client.new(
  api_key: 'key-a',
  organization: 'org-a'
)

# Client for organization B
client_b = OpenAI::Client.new(
  api_key: 'key-b',
  organization: 'org-b'
)

Custom Retry and Timeout Behavior

client = OpenAI::Client.new(
  api_key: 'your-api-key',
  max_retries: 5,
  timeout: 120.0,
  initial_retry_delay: 1.0,
  max_retry_delay: 16.0
)

Available Resources

Once initialized, the client provides access to all API resources:
client.chat              # Chat completions
client.completions       # Text completions
client.embeddings        # Embeddings
client.images            # Image generation
client.audio             # Audio transcription and TTS
client.files             # File management
client.models            # Model information
client.moderations       # Content moderation
client.fine_tuning       # Fine-tuning jobs
client.batches           # Batch processing
client.vector_stores     # Vector store management
client.beta              # Beta features
See the API reference for detailed documentation on each resource.

Build docs developers (and LLMs) love