# Get AI Assistant with Python and Flask ## What Does This Example Do? Build a production-ready Flask endpoint that retrieves AI assistant details using the Telnyx Python SDK. This tutorial demonstrates the new client-based initialization pattern, proper error handling for AI APIs, and secure credential management via environment variables. ## Who Is This For? - **Python developers** building ai features with Flask. - **Backend engineers** integrating telephony or messaging into existing applications. - **DevOps teams** looking for containerized, production-ready telecom examples. - **Startups and enterprises** replacing legacy telecom providers with a modern API-first platform. ## Why Telnyx? Telnyx is an **AI Communications Infrastructure** platform that gives developers a single API for [voice](https://telnyx.com/products/voice-ai-agents), [messaging](https://telnyx.com/products/sms-api), [SIP](https://telnyx.com/products/sip-trunks), [AI](https://telnyx.com/ai-assistants), and [IoT](https://telnyx.com/products/iot-sim-card) — no Frankenstack required. - **Integrated platform** — [Voice](https://telnyx.com/products/voice-ai-agents), [SMS](https://telnyx.com/products/sms-api), [SIP trunking](https://telnyx.com/products/sip-trunks), [AI assistants](https://telnyx.com/ai-assistants), and [IoT SIM management](https://telnyx.com/products/iot-sim-card) under one roof. No stitching together multiple vendors. - **Global private network** — Calls and messages traverse the Telnyx-owned IP network for lower latency and higher reliability than the public internet. - **Developer-first** — SDKs for Python, Node.js, Go, Ruby, Java, and PHP. Comprehensive webhook event model. Sandbox environment for testing. - **Competitive pricing** — Pay-as-you-go with no minimums, contracts, or per-seat fees. ## Prerequisites - Python 3.8 or higher. - A Telnyx account with an active API key from the [Telnyx Portal](https://portal.telnyx.com). - At least one AI assistant created in your Telnyx account. - pip (Python package manager). ## Quick Start ### Option 1: Local (recommended) ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/get-ai-assistant-python cp .env.example .env # Edit .env with your Telnyx API key and phone number make setup make run ``` ### Option 3: Manual See the [Implementation Details](#implementation-details) section below for step-by-step instructions. ## Implementation Details Create `app.py` and initialize the Telnyx client using the new pattern. Define a helper function to handle assistant retrieval with proper validation: ```python import os import telnyx from dotenv import load_dotenv load_dotenv() # Initialize client with the new SDK pattern client = telnyx.Telnyx(api_key=os.getenv("TELNYX_API_KEY")) def get_assistant(assistant_id: str) -> dict: """Retrieve AI assistant by ID and return JSON-serializable response data.""" if not assistant_id or not assistant_id.strip(): raise ValueError("Assistant ID is required") # Use client.ai_assistants.retrieve() — NOT client.ai_assistants.retrieve() response = client.ai_assistants.retrieve(assistant_id) # Extract serializable data — SDK objects are NOT JSON-serializable return { "id": response.data.id, "name": response.data.name, "model": response.data.model, "instructions": response.data.instructions, "tools": response.data.tools, "enabled_features": response.data.enabled_features, "created_at": response.data.created_at, } ``` ## Complete Code See [`app.py`](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/get-ai-assistant-python/app.py) for the full implementation. ## Troubleshooting | Issue | Problem | Solution | |-------|---------|----------| | Authentication Error (401) | The endpoint returns `{"error": "Invalid API key"}` with HTTP 401. | Verify your `TELNYX_API_KEY` in the `.env` file matches the key shown in the [Telnyx Portal](https://portal.telnyx.com). Ensure there are no trailing spaces or quotes. If the key was regenerated recently, update your environment file and restart the Flask server. | | Assistant Not Found (404) | You receive a 404 error or `APIStatusError` indicating the assistant doesn't exist. | Confirm the assistant ID exists in your Telnyx account by checking the AI Assistants section in the portal. Ensure you're using the correct assistant ID format (typically starts with `assistant_`). Verify the assistant wasn't deleted or belongs to a different account. | | Environment Variable Not Set | The application fails to start with an authentication error or `None` API key. | Confirm your `.env` file exists in the same directory as `app.py` and contains the `TELNYX_API_KEY` variable. Ensure the file is named exactly `.env` (not `.env.txt` or `env`). The `load_dotenv()` call must execute before `os.getenv()` is called—verify this import order in your code. | ## FAQ **Q: Do I need a Telnyx account to run this example?** Yes. Sign up at [portal.telnyx.com](https://portal.telnyx.com) to get an API key. Telnyx offers free trial credit for testing. **Q: Can I use this AI example in production?** Yes. This example includes error handling and environment-based configuration. Review the security and scaling sections before deploying to production. **Q: What Python version do I need?** Python 3.8 or higher. Python 3.12+ is recommended. **Q: How is Telnyx different from Twilio?** Telnyx is an AI Communications Infrastructure platform with a private global network, integrated voice + messaging + AI + SIP + IoT under one API, and significantly lower pricing. No need to stitch together multiple vendors. **Q: Where do I get a Telnyx phone number?** Log into the [Telnyx Portal](https://portal.telnyx.com), navigate to Numbers > Search & Buy, and purchase a number with the capabilities you need (SMS, voice, or both). ## Resources - [AI Assistants Guide](https://developers.telnyx.com/docs/inference/ai-assistants/no-code-voice-assistant) - [Assistants API Reference](https://developers.telnyx.com/api-reference/assistants/create-an-assistant) - [Python SDK](https://developers.telnyx.com/development/sdk/python) - [Telnyx AI Assistants](https://telnyx.com/ai-assistants) - [Voice AI Agents](https://telnyx.com/products/voice-ai-agents) ## Related Examples - [List AI Assistants](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main//tutorials/ai/python/list-ai-assistants). - [Update AI Assistant](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main//tutorials/ai/python/update-ai-assistant). - [Chat with AI Assistant](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main//tutorials/ai/python/chat-with-ai-assistant).