02 - Environment Setup¶
This guide covers setting up all environment variables, API keys, and configuration files needed to run the API.
📋 Overview¶
The API requires several environment variables and configuration files:
- Required: Gemini API, ElevenLabs API, Google Cloud credentials
- Optional: YouTube API credentials, URL shortening services
🔑 Step 1: Environment Variables File¶
Create .env File¶
# Navigate to project directory
cd /home/supradmin/apiv2
# Copy the existing .env or create new one
cp .env .env.backup # backup existing if needed
# Edit .env file
nano .env
# or
vim .env
Required Environment Variables¶
Create/update your .env file with these required variables:
# =================================
# REQUIRED API KEYS
# =================================
# Google Gemini AI API Key (for text summarization)
GEMINI_API=your_gemini_api_key_here
# ElevenLabs API Key (for text-to-speech)
ELEVENLABS_API_KEY=your_elevenlabs_api_key_here
# =================================
# GOOGLE CLOUD CONFIGURATION
# =================================
# Path to Google Service Account JSON file
GOOGLE_APPLICATION_CREDENTIALS=sa/supradmin-0c4280905b78.json
# =================================
# OPTIONAL - YOUTUBE CONFIGURATION
# =================================
# YouTube OAuth2 Client ID
YOUTUBE_CLIENT_ID=your_youtube_client_id
# YouTube OAuth2 Client Secret
YOUTUBE_CLIENT_SECRET=your_youtube_client_secret
# OAuth2 configuration files
CLIENT_SECRETS_FILE=sa/oauth.json
CREDENTIALS_PICKLE_FILE=sa/youtube_token.pickle
SCOPES=https://www.googleapis.com/auth/youtube.upload
🔐 Step 2: Obtain API Keys¶
2.1 Google Gemini API Key¶
-
Visit Google AI Studio:
-
Create API Key:
- Click "Create API Key"
- Select or create a Google Cloud Project
-
Copy the generated API key
-
Add to .env:
2.2 ElevenLabs API Key¶
-
Sign up at ElevenLabs:
-
Get API Key:
- Go to Profile > API Keys
-
Copy your API key
-
Add to .env:
2.3 Google Cloud Service Account (Required)¶
- Create Google Cloud Project:
- Visit Google Cloud Console
-
Create new project or select existing
-
Enable APIs:
-
Create Service Account:
- Go to IAM & Admin > Service Accounts
- Click "Create Service Account"
- Name:
api-service-account -
Add roles:
- Storage Admin
- YouTube Data API v3 (if using YouTube)
-
Download JSON Key:
- Click on service account
- Go to "Keys" tab
- Click "Add Key" > "Create New Key" > JSON
- Save as
sa/supradmin-0c4280905b78.json
2.4 YouTube API Setup (Optional)¶
Only needed if you want YouTube upload functionality.
- Create OAuth2 Client ID:
- Go to Google Cloud Console > APIs & Services > Credentials
- Click "Create Credentials" > "OAuth client ID"
- Application type: "Desktop application"
-
Name: "YouTube API Client"
-
Download OAuth2 JSON:
- Download the client secrets JSON
-
Save as
sa/oauth.json -
Add to .env:
📁 Step 3: Directory Structure Setup¶
Create Required Directories¶
# Create service account directory
mkdir -p sa
# Create media directories
mkdir -p audio images videos_output tts
# Verify structure
ls -la
Expected Directory Structure:¶
apiv2/
├── .env # Environment variables
├── main.py # Main application
├── requirements.txt # Python dependencies
├── sa/ # Service account files
│ ├── supradmin-0c4280905b78.json # GCS service account
│ ├── oauth.json # YouTube OAuth2 config
│ └── youtube_token.pickle # YouTube credentials (auto-generated)
├── audio/ # Audio files storage
├── images/ # Image files storage
├── videos_output/ # Generated videos
├── tts/ # Generated TTS files
└── internal/ # Application code
├── config/
├── models/
├── services/
└── utils/
🔒 Step 4: Secure File Permissions¶
Set Proper Permissions¶
# Secure service account files
chmod 600 sa/*.json
chmod 600 .env
# Make directories writable
chmod 755 audio images videos_output tts
✅ Step 5: Validate Environment Setup¶
Test Environment Loading¶
# Test environment variable loading
python -c "
import os
from dotenv import load_dotenv
load_dotenv()
# Check required variables
required_vars = ['GEMINI_API', 'ELEVENLABS_API_KEY', 'GOOGLE_APPLICATION_CREDENTIALS']
missing = []
for var in required_vars:
value = os.getenv(var)
if value:
print(f'✅ {var}: {value[:10]}...')
else:
missing.append(var)
print(f'❌ {var}: Not set')
if missing:
print(f'Missing required variables: {missing}')
exit(1)
else:
print('✅ All required environment variables are set!')
"
Test Google Cloud Authentication¶
# Test GCS authentication
python -c "
import os
from google.cloud import storage
# This should not raise an error
client = storage.Client()
print('✅ Google Cloud Storage authentication successful!')
"
Test API Keys¶
# Test Gemini API
python -c "
import os
import google.generativeai as genai
from dotenv import load_dotenv
load_dotenv()
genai.configure(api_key=os.getenv('GEMINI_API'))
try:
model = genai.GenerativeModel('gemini-2.0-flash')
response = model.generate_content('Test')
print('✅ Gemini API key working!')
except Exception as e:
print(f'❌ Gemini API error: {e}')
"
🚨 Troubleshooting Environment Issues¶
Issue: Google Cloud Authentication Fails¶
# Method 1: Set credentials explicitly
export GOOGLE_APPLICATION_CREDENTIALS="/home/supradmin/apiv2/sa/supradmin-0c4280905b78.json"
# Method 2: Use gcloud auth
gcloud auth application-default login
# Method 3: Verify file exists and has correct permissions
ls -la sa/supradmin-0c4280905b78.json
Issue: API Keys Not Working¶
# Verify .env file is in correct location
pwd
ls -la .env
# Test environment loading manually
python -c "
import os
print('Current directory:', os.getcwd())
print('Files:', os.listdir('.'))
from dotenv import load_dotenv
result = load_dotenv()
print('Dotenv loaded:', result)
print('GEMINI_API exists:', bool(os.getenv('GEMINI_API')))
"
Issue: Permission Denied¶
# Fix file permissions
sudo chown -R $USER:$USER /home/supradmin/apiv2
chmod -R 755 /home/supradmin/apiv2
chmod 600 /home/supradmin/apiv2/.env
chmod 600 /home/supradmin/apiv2/sa/*.json
🔐 Environment Variables Reference¶
Required Variables¶
| Variable | Purpose | Example |
|---|---|---|
GEMINI_API |
Google Gemini AI API key | AIzaSyC... |
ELEVENLABS_API_KEY |
ElevenLabs TTS API key | sk_1cb4be... |
GOOGLE_APPLICATION_CREDENTIALS |
Path to GCS service account JSON | sa/service-account.json |
Optional Variables¶
| Variable | Purpose | Default |
|---|---|---|
YOUTUBE_CLIENT_ID |
YouTube OAuth2 client ID | None |
YOUTUBE_CLIENT_SECRET |
YouTube OAuth2 client secret | None |
CLIENT_SECRETS_FILE |
YouTube OAuth2 config file | sa/oauth.json |
CREDENTIALS_PICKLE_FILE |
YouTube token storage | sa/youtube_token.pickle |
Service Configuration¶
| Variable | Purpose | Default |
|---|---|---|
DEFAULT_BUCKET_NAME |
GCS bucket name | merchant-ary |
DEFAULT_VOICE_ID |
ElevenLabs voice ID | 21m00Tcm4TlvDq8ikWAM |
DEFAULT_MODEL |
Gemini model name | gemini-2.0-flash |
🎯 Next Steps¶
Once environment setup is complete, proceed to: - 03-running-the-api.md - Start the API server
📝 Security Notes¶
- Never commit
.envfiles to version control - Keep service account files secure with proper permissions
- Regularly rotate API keys for security
- Use different keys for development and production environments