get_api_key

Function get_api_key 

Source
pub fn get_api_key(provider: &str, sources: &ApiKeySources) -> Result<String>
Expand description

Get API key for a specific provider with secure fallback mechanism

This function implements a secure retrieval mechanism that:

  1. First checks environment variables (highest priority for security)
  2. Then checks .env file values
  3. Falls back to configuration file values if neither above is set
  4. Supports all major providers: Gemini, Anthropic, OpenAI, OpenRouter, and xAI
  5. Automatically infers the correct environment variable based on provider

§Arguments

  • provider - The provider name (“gemini”, “anthropic”, or “openai”)
  • sources - Configuration for where to look for API keys

§Returns

  • Ok(String) - The API key if found
  • Err - If no API key could be found for the provider