Retell AI Python SDK

5.25.1 · active · verified Thu Apr 16

The official Python library for the Retell API, designed to simplify building AI voice agents. It enables real-time, bidirectional voice conversations with LLMs. The library is actively maintained with frequent updates, often reflecting changes and new features in the core Retell API.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the RetellClient using an API key from an environment variable and demonstrates listing existing agents. Remember to replace 'YOUR_RETELL_API_KEY' or set the `RETELL_API_KEY` environment variable.

import os
from retell_sdk import RetellClient
from retell_sdk.models import AgentResponse

# Ensure your Retell API key is set as an environment variable or passed directly.
# It's recommended to set RETELL_API_KEY in your environment.
api_key = os.environ.get("RETELL_API_KEY", "YOUR_RETELL_API_KEY")

if api_key == "YOUR_RETELL_API_KEY":
    print("WARNING: Please set the RETELL_API_KEY environment variable or replace 'YOUR_RETELL_API_KEY' with your actual key.")

try:
    client = RetellClient(api_key=api_key)

    # Example: List all agents
    print("\nListing agents...")
    agents_list_response = client.agent.list_all(limit=5)
    
    if agents_list_response and agents_list_response.data:
        for agent in agents_list_response.data:
            print(f"  - Agent ID: {agent.agent_id}, LLM URL: {agent.llm_websocket_url}")
    else:
        print("No agents found or an error occurred.")

    # Example: Retrieve a specific agent (replace with an actual agent_id if available)
    # agent_id_to_retrieve = "your_agent_id_here"
    # try:
    #     agent: AgentResponse = client.agent.get(agent_id=agent_id_to_retrieve)
    #     print(f"\nRetrieved Agent ID: {agent.agent_id}, Voice: {agent.voice_id}")
    # except Exception as e:
    #     print(f"Error retrieving agent {agent_id_to_retrieve}: {e}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →