API Key Configuration

The doctai provides flexible ways to configure which environment variable contains your API key.

Table of contents

  1. Default Behavior
  2. Using Provider-Specific Environment Variables
    1. Method 1: Config File (Recommended)
    2. Method 2: Environment Variable
  3. Multiple Setups / Projects
    1. Project A (Using Claude):
    2. Project B (Using OpenAI):
    3. Project C (Using Gemini):
  4. Priority Order
  5. Examples
    1. Example 1: Using Standard Environment Variables
    2. Example 2: Different Keys for Different Environments
    3. Example 3: CI/CD with GitHub Actions
  6. Supported Config Keys
  7. Troubleshooting

Default Behavior

By default, the tool reads the API key from the DOCTAI_API_KEY environment variable:

export DOCTAI_API_KEY="your-api-key-here"
doctai --docs README.md

Using Provider-Specific Environment Variables

If you prefer to use provider-specific environment variables (e.g., ANTHROPIC_API_KEY, OPENAI_API_KEY), you can configure this in your .doctai.yml file:

Add the api_key_env_var field to your config file:

# .doctai.yml
provider: anthropic
model: claude-sonnet-4-20250514
api_key_env_var: ANTHROPIC_API_KEY

Then set your provider-specific environment variable:

export ANTHROPIC_API_KEY="sk-ant-..."
doctai

The tool will automatically read from ANTHROPIC_API_KEY instead of DOCTAI_API_KEY.

Method 2: Environment Variable

You can also specify which environment variable to use via DOCTAI_API_KEY_ENV_VAR:

export OPENAI_API_KEY="sk-..."
export DOCTAI_API_KEY_ENV_VAR=OPENAI_API_KEY
doctai --docs README.md

Multiple Setups / Projects

This feature is particularly useful when working with multiple projects that use different AI providers:

Project A (Using Claude):

# projectA/.doctai.yml
provider: anthropic
api_key_env_var: ANTHROPIC_API_KEY
cd projectA
export ANTHROPIC_API_KEY="sk-ant-..."
doctai

Project B (Using OpenAI):

# projectB/.doctai.yml
provider: openai
api_key_env_var: OPENAI_API_KEY
cd projectB
export OPENAI_API_KEY="sk-..."
doctai

Project C (Using Gemini):

# projectC/.doctai.yml
provider: gemini
api_key_env_var: GEMINI_API_KEY
cd projectC
export GEMINI_API_KEY="..."
doctai

Priority Order

The tool checks for the API key in the following order:

  1. Command-line argument: --api-key YOUR_KEY (highest priority)
  2. Specified environment variable: From config file or DOCTAI_API_KEY_ENV_VAR
  3. Default environment variable: DOCTAI_API_KEY (fallback)

Examples

Example 1: Using Standard Environment Variables

Many developers already have environment variables like OPENAI_API_KEY or ANTHROPIC_API_KEY set up. Just configure the tool to use them:

# .doctai.yml
provider: openai
model: gpt-4o
api_key_env_var: OPENAI_API_KEY

Example 2: Different Keys for Different Environments

# Development
export DEV_API_KEY="sk-dev-..."

# Production
export PROD_API_KEY="sk-prod-..."

# In dev config
# .doctai.yml
api_key_env_var: DEV_API_KEY

# In prod config
# .doctai.prod.yml
api_key_env_var: PROD_API_KEY

Example 3: CI/CD with GitHub Actions

# .github/workflows/test-docs.yml
name: Test Documentation

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      
      - name: Test Documentation
        env:
          ANTHROPIC_API_KEY: $
        run: |
          pip install -e .
          doctai

With config:

# .doctai.yml
provider: anthropic
api_key_env_var: ANTHROPIC_API_KEY

Supported Config Keys

The following config keys are recognized (in order of precedence):

  • api_key_env_var
  • api_key_env
  • api_key_var

All three work the same way - use whichever you prefer.

Troubleshooting

If the API key is not being found:

  1. Check environment variable name:
    echo $ANTHROPIC_API_KEY  # Should print your key
    
  2. Check config file:
    cat .doctai.yml | grep api_key_env_var
    
  3. Run with verbose output:
    doctai --docs README.md
    # Should print: "Using API key from environment variable: ANTHROPIC_API_KEY"
    
  4. Test with explicit key (for debugging):
    doctai --docs README.md --api-key "your-key"