#!/usr/bin/env python3
"""Test ElevenLabs API directly to list agents."""
import os
import sys
import asyncio
import json
# Add the src directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from elevenlabs_mcp.client import ElevenLabsClient, ElevenLabsError
async def test_list_agents():
"""Test listing agents directly via ElevenLabs API."""
print("๐ Testing ElevenLabs API - List Agents")
print("=" * 50)
# Get API key from environment
api_key = os.environ.get('ELEVENLABS_API_KEY')
if not api_key:
print("โ ELEVENLABS_API_KEY not found in environment")
print("๐ก Set it with: export ELEVENLABS_API_KEY='your-api-key'")
return False
print(f"โ
API Key found: {api_key[:8]}...{api_key[-4:]}")
try:
# Create client
async with ElevenLabsClient(api_key=api_key) as client:
print("โ
ElevenLabs client created successfully")
# Test API connectivity by listing agents
print("\n๐ Calling ElevenLabs API to list agents...")
result = await client.list_agents()
print("โ
API call successful!")
print(f"๐ Response: {json.dumps(result, indent=2)}")
# Parse the response
if "agents" in result:
agents = result["agents"]
print(f"\n๐ Summary: Found {len(agents)} agents")
if agents:
for i, agent in enumerate(agents, 1):
print(f"\n๐ค Agent {i}:")
print(f" ID: {agent.get('agent_id', 'N/A')}")
print(f" Name: {agent.get('name', 'N/A')}")
print(f" Created: {agent.get('metadata', {}).get('created_at', 'N/A')}")
# Show conversation config if available
if 'conversation_config' in agent:
config = agent['conversation_config']
if 'agent' in config and 'language' in config['agent']:
print(f" Language: {config['agent']['language']}")
if 'tts' in config and 'voice_id' in config['tts']:
print(f" Voice ID: {config['tts']['voice_id']}")
else:
print("๐ No agents found in your account")
else:
print("โ ๏ธ Unexpected response format")
return True
except ElevenLabsError as e:
print(f"โ ElevenLabs API error: {e}")
return False
except Exception as e:
print(f"โ Unexpected error: {e}")
return False
async def test_api_connectivity():
"""Test basic API connectivity."""
print("\n๐ Testing API Connectivity")
print("=" * 30)
api_key = os.environ.get('ELEVENLABS_API_KEY')
if not api_key:
print("โ No API key available")
return False
try:
async with ElevenLabsClient(api_key=api_key) as client:
# Test with a simple request
print("๐ Testing connection to ElevenLabs API...")
# Try to list agents (simplest endpoint)
result = await client.list_agents(page_size=1)
print("โ
API connection successful!")
print(f"๐ก Response received: {type(result)}")
return True
except Exception as e:
print(f"โ Connection failed: {e}")
return False
async def main():
"""Main test function."""
print("๐งช ElevenLabs API Direct Test")
print("=" * 60)
# Check environment
print("1. Checking Environment...")
api_key = os.environ.get('ELEVENLABS_API_KEY')
if not api_key:
print("โ ELEVENLABS_API_KEY not set")
print("\n๐ก To set it:")
print(" export ELEVENLABS_API_KEY='sk_your_api_key_here'")
print(" python test_api_direct.py")
return
print(f"โ
API Key: {api_key[:8]}...{api_key[-4:]}")
# Test connectivity
print("\n2. Testing API Connectivity...")
connectivity_ok = await test_api_connectivity()
if connectivity_ok:
# Test listing agents
print("\n3. Listing Your Agents...")
await test_list_agents()
else:
print("โ Cannot proceed - API connectivity failed")
print("\n๐ง Troubleshooting:")
print(" - Check your API key is valid")
print(" - Verify internet connection")
print(" - Ensure ElevenLabs service is accessible")
if __name__ == "__main__":
asyncio.run(main())