Skip to content

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

  1. Visit Google AI Studio:

    https://makersuite.google.com/app/apikey
    

  2. Create API Key:

  3. Click "Create API Key"
  4. Select or create a Google Cloud Project
  5. Copy the generated API key

  6. Add to .env:

    GEMINI_API=AIzaSyC...your_actual_key_here
    

2.2 ElevenLabs API Key

  1. Sign up at ElevenLabs:

    https://elevenlabs.io/
    

  2. Get API Key:

  3. Go to Profile > API Keys
  4. Copy your API key

  5. Add to .env:

    ELEVENLABS_API_KEY=sk_...your_actual_key_here
    

2.3 Google Cloud Service Account (Required)

  1. Create Google Cloud Project:
  2. Visit Google Cloud Console
  3. Create new project or select existing

  4. Enable APIs:

    # Enable required APIs
    gcloud services enable storage-api.googleapis.com
    gcloud services enable youtube.googleapis.com
    gcloud services enable generativelanguage.googleapis.com
    

  5. Create Service Account:

  6. Go to IAM & Admin > Service Accounts
  7. Click "Create Service Account"
  8. Name: api-service-account
  9. Add roles:

    • Storage Admin
    • YouTube Data API v3 (if using YouTube)
  10. Download JSON Key:

  11. Click on service account
  12. Go to "Keys" tab
  13. Click "Add Key" > "Create New Key" > JSON
  14. Save as sa/supradmin-0c4280905b78.json

2.4 YouTube API Setup (Optional)

Only needed if you want YouTube upload functionality.

  1. Create OAuth2 Client ID:
  2. Go to Google Cloud Console > APIs & Services > Credentials
  3. Click "Create Credentials" > "OAuth client ID"
  4. Application type: "Desktop application"
  5. Name: "YouTube API Client"

  6. Download OAuth2 JSON:

  7. Download the client secrets JSON
  8. Save as sa/oauth.json

  9. Add to .env:

    YOUTUBE_CLIENT_ID=748075362085...
    YOUTUBE_CLIENT_SECRET=GOCSPX-...
    CLIENT_SECRETS_FILE=sa/oauth.json
    

📁 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 .env files 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