# Create AI Assistant with PHP and Laravel ## What Does This Example Do? Build a production-ready Laravel endpoint that creates AI assistants using the Telnyx PHP SDK. This tutorial demonstrates the new client-based initialization pattern, proper error handling for telecom APIs, secure credential management via environment variables, and Laravel's idiomatic patterns for request validation and response serialization. ## Who Is This For? - **PHP developers** building ai features with Laravel. - **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 - PHP 8.1 or higher. - Laravel 10 or higher. - Composer (PHP package manager). - A Telnyx account with an active API key from the [Telnyx Portal](https://portal.telnyx.com). - Basic familiarity with Laravel routing and controllers. ## Quick Start ### Option 1: Local (recommended) ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/create-ai-assistant-php 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 a controller to handle AI assistant creation. Generate it using Artisan: ```bash php artisan make:controller AiAssistantController ``` Update `app/Http/Controllers/AiAssistantController.php`: ```php client = new Client(apiKey: getenv('TELNYX_API_KEY')); } /** * Create a new AI assistant. * * @param Request $request * @return JsonResponse */ public function store(Request $request): JsonResponse { // Validate incoming request data $validated = $request->validate([ 'name' => 'required|string|max:255', 'model' => 'required|string', 'instructions' => 'required|string', 'enabled_features' => 'array', 'enabled_features.*' => 'string|in:telephony,messaging', ]); try { // Create assistant via Telnyx API $response = $this->client->aiAssistants->create([ 'name' => $validated['name'], 'model' => $validated['model'], 'instructions' => $validated['instructions'], 'enabled_features' => $validated['enabled_features'] ?? [], ]); // Extract serializable data — SDK objects are NOT JSON-serializable return response()->json([ 'id' => $response->data->id, 'name' => $response->data->name, 'model' => $response->data->model, 'instructions' => $response->data->instructions, 'enabled_features' => $response->data->enabled_features, 'created_at' => $response->data->created_at, ], 201); } catch (\Telnyx\Exception\AuthenticationException $e) { return response()->json(['error' => 'Invalid API key'], 401); } catch (\Telnyx\Exception\RateLimitException $e) { return response()->json(['error' => 'Rate limit exceeded. Please slow down.'], 429); } catch (\Telnyx\Exception\ApiErrorException $e) { return response()->json( ['error' => $e->getMessage()], $e->getHttpStatus() ?? 400 ); } catch (\Exception $e) { return response()->json(['error' => 'An unexpected error occurred'], 500); } } } ``` Register the route in `routes/api.php`: ```php 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) - [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/php/list-ai-assistants). - [Get an AI Assistant](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main//tutorials/ai/php/get-ai-assistant). - [Chat with an AI Assistant](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main//tutorials/ai/php/chat-with-ai-assistant).