# OpenAI API Key Setup Guide

**Last Updated:** 2026-01-13

Guide for setting up OpenAI API key for FAQ answer generation.

## Overview

The FAQ automation system uses OpenAI API to generate FAQ answers. The API key can be configured in multiple ways, following the same pattern as other API keys in the codebase.

## Configuration Options

### Option 1: Environment Variable (Recommended)

Set the environment variable in your shell:

```bash
export OPENAI_API_KEY=sk-your-actual-api-key-here
```

**For persistent setup:**

**macOS/Linux:**
Add to `~/.zshrc` or `~/.bashrc`:
```bash
export OPENAI_API_KEY=sk-your-actual-api-key-here
```

**Windows (PowerShell):**
```powershell
[System.Environment]::SetEnvironmentVariable('OPENAI_API_KEY', 'sk-your-actual-api-key-here', 'User')
```

### Option 2: Config File (Alternative)

Create `v2/config/openai-api-key.php`:

```php
<?php
/**
 * OpenAI API Key Configuration
 * 
 * SECURITY: Never commit this file to version control.
 * This file is in .gitignore.
 */

return [
    'api_key' => 'sk-your-actual-api-key-here'
];
```

**Note:** Copy from `v2/config/openai-api-key.php.example` if needed.

### Option 3: PHP Constant (Development Only)

Define in your PHP configuration or bootstrap file:

```php
define('OPENAI_API_KEY', 'sk-your-actual-api-key-here');
```

## Priority Order

The system checks for API key in this order:

1. **Environment variable** `OPENAI_API_KEY` (highest priority)
2. **PHP constant** `OPENAI_API_KEY`
3. **Config file** `v2/config/openai-api-key.php`
4. **Fallback constant** `OPENAI_API_KEY_FALLBACK` (development only)

## Getting Your API Key

1. Go to https://platform.openai.com/api-keys
2. Sign in or create an account
3. Click "Create new secret key"
4. Copy the key (starts with `sk-`)
5. Store it securely (you won't be able to see it again)

## Verification

Test that the API key is configured:

```bash
# Check environment variable
echo $OPENAI_API_KEY

# Test script (dry-run)
php v2/scripts/blog/generate-faq-answers-ai.php --post=minijob --category=lexikon --dry-run
```

## Security Best Practices

1. **Never commit API keys to version control**
   - The config file `openai-api-key.php` is in `.gitignore`
   - Use environment variables in production

2. **Use environment variables in production**
   - More secure than config files
   - Easier to manage across environments

3. **Rotate keys regularly**
   - Update keys if compromised
   - Use different keys for dev/staging/production

4. **Monitor usage**
   - Check OpenAI dashboard for usage
   - Scripts track costs automatically

## Cost Tracking

The system automatically tracks API costs:

- Requests count
- Token usage
- Estimated cost (based on model pricing)

View costs after running scripts:
```php
$costSummary = get_openai_cost_summary();
print_r($costSummary);
```

## Troubleshooting

### Error: "OpenAI API key not configured"

**Solution:** Set one of the configuration options above.

### Error: "Invalid API key"

**Solution:** 
- Verify key starts with `sk-`
- Check key hasn't expired
- Ensure no extra spaces/quotes

### Error: "Rate limit exceeded"

**Solution:**
- Wait before retrying
- Check OpenAI dashboard for limits
- Consider upgrading plan

## Related Documentation

- `FAQ_AUTOMATION_IMPLEMENTATION_SUMMARY.md` - Complete feature overview
- `FAQ_AUTOMATION_QUICK_REFERENCE.md` - Quick command reference
- `CONTENT_CREATION_WORKFLOW.md` - Workflow guide
