I want to have my agents SMS users also, How do I ...
# ask-ai
b
I want to have my agents SMS users also, How do I do that?
✅ 1
t
Thanks for reaching out, Loyd. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I can help you get your agents sending SMS to users. You're asking how to enable SMS from your LiveKit agents; LiveKit doesn't provide SMS delivery itself, but your agents can call an external SMS provider via a function/tool. Below is a concise plan and a simple example to get you started. What you need to know first • LiveKit agents can invoke custom function tools. Use a tool that calls a third‑party SMS provider (for example, Twilio, Plivo, or AWS SNS). • You must supply the recipient phone number and message content to the tool. • Make sure you follow local compliance rules (e.g., opt‑in requirements and DLT registration where applicable). High-level steps 1. Choose an SMS provider (Twilio, Plivo, AWS SNS, etc.) and create credentials. 2. Implement a function tool in your agent code that makes an HTTP request to the provider’s send‑SMS API. 3. Add the tool to your agent so the LLM can invoke it when it needs to send an SMS. 4. Add error handling and logging so you can track delivery status and failures. 5. Ensure legal/compliance checks and user consent are handled before sending messages. Minimal example (Python, using Twilio-style REST call) • This shows the structure of a function tool that sends SMS. Replace endpoints, parameters, and auth for your chosen provider and secure credentials via environment variables or a secrets manager.
Copy code
import os
import httpx
from livekit.agents import function_tool, RunContext

@function_tool()
async def send_sms(
    self,
    context: RunContext,
    phone_number: str,
    message: str,
) -> str:
    # Example: Twilio REST API call (adjust for your provider)
    account_sid = os.environ["TWILIO_SID"]
    auth_token = os.environ["TWILIO_TOKEN"]
    twilio_number = os.environ["TWILIO_FROM"]  # your Twilio phone

    url = f"<https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json>"
    data = {
        "To": phone_number,
        "From": twilio_number,
        "Body": message,
    }

    async with httpx.AsyncClient() as client:
        resp = await <http://client.post|client.post>(url, data=data, auth=(account_sid, auth_token))
        if resp.status_code in (200, 201):
            return "SMS sent successfully."
        return f"Failed to send SMS: {resp.status_code} {resp.text}"
Tips and considerations • Protect credentials: use environment variables or a secrets manager (never hard‑code keys). • Validate phone numbers and user opt‑ins before sending to avoid compliance issues. • If you need delivery confirmation, use the provider’s webhook callbacks to update message status in your system. • Include retry logic for transient failures and clear error messages when the provider returns an error. If you’d like, I can: • Provide an example adapted to a specific provider (Twilio, AWS SNS, Plivo). • Show how to register the tool with your agent and an example agent prompt pattern that triggers it. • Help design webhook handling for delivery receipts. Which SMS provider do you plan to use, or would you like a recommendation and an example for Twilio? Sources: Support data sovereignty in India | LiveKit | Handling payments with PCI compliance | LiveKit
b
Was going to use telnyx
t
Thanks — great choice. Since you’ll be using Telnyx, here’s a clear, safe path to get your LiveKit agent sending SMS messages, plus a ready-to-run example you can adapt. What I’m responding to • You said you’ll use Telnyx to send SMS from your LiveKit agents. Below I’ll show the required steps, a Python example function tool for LiveKit agents, and important safety/compliance tips. High-level steps 1. Create a Telnyx account and obtain an API key. 2. Purchase and configure a Telnyx phone number (the number messages will be sent from). 3. Install the Telnyx SDK in your agent runtime (or call the REST API directly). 4. Implement a function tool in your LiveKit agent that calls Telnyx to send SMS. 5. Secure credentials (env vars or a secrets manager), validate numbers and consent, and add logging/webhook handling for delivery receipts. Minimal Python example (using Telnyx SDK) 1. Install: pip install telnyx 2. Set environment variables in your runtime:
TELNYX_API_KEY
and
TELNYX_FROM_NUMBER
(do not hard-code keys).
Copy code
import os
import telnyx
from livekit.agents import function_tool, RunContext

# Initialize Telnyx with API key from environment
telnyx.api_key = os.environ["TELNYX_API_KEY"]
FROM_NUMBER = os.environ["TELNYX_FROM_NUMBER"]  # e.g. "+1XXXXXXXXXX"

@function_tool()
async def send_sms(
    self,
    context: RunContext,
    to_number: str,
    message_body: str,
) -> str:
    """
    Send an SMS using Telnyx.
    to_number: E.164 formatted recipient number (e.g. "+15551234567")
    message_body: Text content to send
    """
    try:
        msg = telnyx.Message.create(
            to=to_number,
            from_=FROM_NUMBER,
            text=message_body
        )
        # msg will contain an id and initial status; you may want to persist msg.id
        return f"SMS queued (id={msg.id})."
    except Exception as e:
        # Return error for the agent to surface; log full exception server-side
        return f"Failed to send SMS: {str(e)}"
Notes and enhancements • Phone formatting: Use E.164 format (e.g., +15551234567). Validate before calling the API. • Credentials: Store
TELNYX_API_KEY
and
TELNYX_FROM_NUMBER
in environment variables or a secrets manager. • Delivery receipts: Telnyx supports webhooks for delivery status. Configure a webhook endpoint to receive callbacks and update message state in your system. • Error handling & retries: Add retry logic for transient network/provider errors and surface clear messages for permanent failures. • Rate limits & throughput: Check Telnyx account limits and implement queuing/throttling if you expect high volume. • Consent & compliance: Ensure users have opted in to receive SMS and follow regional rules (opt-in, opt-out handling, DLT or local registration where required). • Logging & audit: Log message ids, timestamps, statuses, and any provider errors for debugging and compliance. If you’d like, I can next: • Provide a Node.js example instead. • Show how to wire Telnyx delivery webhooks into your app and map statuses back to the agent. • Help craft an agent prompt pattern that asks for confirmation before sending SMS (to capture consent). Which would you prefer next — Node.js sample, webhook handling, or the agent prompt flow? Sources: Handling payments with PCI compliance | LiveKit
b
We wanted to you the livekit cloud, but wanted our clients to only login to our softer. We wanted to use livekit’s sdk for the front end. Do you have a GitHub for it? And any suggestions? Thanks
t
Thanks — this is a great setup and totally doable. I’ll cover where to find the SDKs on GitHub, how to let your clients sign in only through your system, and a few practical suggestions to make it secure and smooth. Where to find the SDKs (GitHub) • JavaScript / TypeScript (web): https://github.com/livekit/client-sdk-js • React components (prebuilt UI): https://github.com/livekit/components-js • Server SDKs (to mint tokens): https://github.com/livekit/server-sdk • Full list of client SDKs and platform repos: https://docs.livekit.io/client-sdks/ How to keep logins inside your app (recommended pattern) 1. Keep all secrets on your backend — never ship API keys or server secrets to the browser or mobile app. 2. When a user logs into your app, authenticate them with your system. 3. Your backend uses a LiveKit Server SDK to generate a short‑lived JWT access token for that user (embedding their identity and room permissions). 4. Return the token to the frontend and call the LiveKit client SDK to connect: ◦ Example connect call:
await room.connect('<wss://your-project.livekit.cloud>', token);
1. Use short token lifetimes (e.g., minutes) and mint new tokens as needed to reduce risk. Practical suggestions and best practices • Token scope: Encode only the minimal permissions (room, participant name, roles) required for each user. • Room & identity management: Maintain canonical room membership on your backend so you can enforce access rules, recording settings, and moderation. • Refresh flow: Provide a lightweight endpoint for the frontend to request a fresh token when the current one nears expiration. • Use HTTPS and secure storage for tokens on clients (e.g., secure cookies or secure storage on mobile). • Webhooks / server callbacks: If you need events (participant join/leave, recording status), configure server-side webhooks to keep your system in sync. • Prebuilt UI: If you want faster front-end development, evaluate
@livekit/components-react
— you can style/extend it so the UX remains within your own app’s look and feel. • Monitoring & logging: Log token issuance, connection attempts, and failures to help triage auth/connect issues. If you’d like, I can next: • Provide a short Node.js or Python example showing how to mint a LiveKit access token on your backend. • Show a minimal front-end snippet that receives the token and connects using the JS SDK. • Walk through how to include room permissions/metadata in the token payload. Which would help most right now — a backend token example (Node/Python), or the front-end connect snippet?
b
Audio Clip (2026-03-19 173154).m4a
t
Thanks — it looks like your last message was empty. Do you want to continue with one of these next steps? • A Node.js or Python example showing how to mint LiveKit access tokens on your backend. • A minimal front-end snippet that uses the JS SDK to connect with the token. • A Node.js or Python example for sending SMS via Telnyx (webhook handling, delivery receipts). • Help designing an agent prompt flow that asks for SMS consent before sending. • Anything else you're stuck on (permissions, token lifetime, deployment, or scalability). Tell me which option (or options) you want first and any details I should tailor the examples to (Node vs Python, web vs mobile, expected message volume, etc.). , with interactive elements
b
I sent you a voice memo. Can you not listen to those?
t
Thanks for the voice memo — I can’t listen to audio messages here. Could you please paste a short text summary or the key points from the memo (or type your question)? If it helps, here are a few prompts you could copy/paste: • “I want the agent to send SMS when X happens — do this…” (describe trigger) • “Show me a Node/Python example for minting LiveKit tokens.” • “Show Telnyx webhook handling for delivery receipts.” • “How should I confirm SMS consent with users before sending?” Tell me which one matches your memo (or paste the memo text) and I’ll pick up from there. , with interactive elements
b
5:34 € •il 5G+ 805) Audio Transcript (auto-generated) Give Feedback 00:02 Hey, so I'm working on the Livekit integration. 00:06 I was gonna use Teleex as the Voice and, and for text message. 00:14 And How am I Like, how am I interacting? 00:22 Is everything, like all communication go from Life kit and then it gets distributed. 00:29 To I don't know, through the Tenex like APl so that I can send a text, you know, stuff like that. 00:36 I'm just kinda confused about how like The interaction because Livekit doesn't handle SMS messages like manual or, otherwise, right? 00:50 Even what, if, even if you were to buy one of their phone numbers. 00:52 So does that, hopefully that makes sense. 00:54 Can you help me out with that? 00:55 Thanks.
t
Thanks for the transcript — that helps. You're asking whether LiveKit handles SMS or if SMS goes through Telnyx, and how the interaction should look. Short answer: LiveKit handles real‑time audio/video (and can connect to PSTN via SIP/Telnyx for voice), but SMS is sent and received through Telnyx’s API — LiveKit does not proxy SMS. Below is a clear architecture and practical steps to implement this flow. How it works (high level) • LiveKit handles real‑time audio/video and connects to phone networks for voice via SIP trunking with Telnyx. • SMS messages are managed entirely by Telnyx. Your app/backend must call Telnyx’s messaging API to send SMS and configure webhooks to receive inbound SMS. • Your LiveKit agent (or backend logic) invokes a function/tool that calls Telnyx’s API to send SMS when needed. Typical interaction flow 1. A phone user calls your Telnyx number. 2. Telnyx bridges the call into LiveKit via SIP (this creates a LiveKit room with the caller and your agent). 3. The LiveKit agent processes the conversation and decides to send an SMS (for example, a confirmation or link). 4. The agent invokes a function/tool that triggers your backend to POST to Telnyx’s /messages API with the recipient, from number, and message text. 5. Telnyx delivers the SMS independently of LiveKit; delivery receipts are provided by Telnyx via webhooks to your backend. Concrete example (how to trigger SMS from an agent) • Add a function tool to your agent that calls Telnyx’s messaging API. • Trigger that tool from within the agent’s dialog when the agent should send a message (after confirming consent with the caller). Minimal Python example (calling Telnyx API from an agent tool):
Copy code
@function_tool()
async def send_sms(context: RunContext, phone_number: str, message: str) -> str:
    import os, requests
    api_key = os.getenv("TELNYX_API_KEY")
    from_number = os.getenv("TELNYX_FROM_NUMBER")  # your Telnyx number
    resp = requests.post(
        "https://api.telnyx.com/v2/messages",
        headers={"Authorization": f"Bearer {api_key}"},
        json={
            "from": from_number,
            "to": phone_number,
            "text": message,
            "messaging_profile_id": "your_profile_id"
        }
    )
    if resp.ok:
        return "SMS sent successfully."
    return f"Failed to send SMS: {resp.status_code} {resp.text}"
Inbound SMS handling • Configure a Telnyx webhook to POST incoming messages to your backend. • Your backend can create/route messages into the agent’s session or start a new interaction (for example, notify an agent or initiate a callback). Important considerations • Consent & compliance: Always verify the user has opted in to SMS. Follow regional rules (e.g., opt‑out handling, DLT registration where required). • Phone number formatting: Use E.164 (+countrycode) formatting. • Webhooks: Use webhooks to track delivery status and incoming messages; persist message IDs and statuses for auditing. • Error handling & retries: Implement retries and proper error logging for provider errors or rate limits. • Security: Keep Telnyx API keys on your backend or a secrets manager; never expose keys in the frontend. If you want, I can next: • Provide a Node.js example for sending SMS via Telnyx and handling webhooks. • Show how to prompt the caller for SMS consent in a voice flow before sending. • Walk through wiring Telnyx inbound SMS webhooks into a LiveKit agent session. Which would help you most right now? Sources: livekit/livekit_composite | DeepWiki | Telnyx LLM plugin guide | LiveKit Documentation | Agents telephony integration | LiveKit Documentation | Introducing LiveKit Phone Numbers: zero to ringing in 60 seconds | LiveKit | Company directory phone assistant | LiveKit Documentation, with interactive elements
b
Do you have any like GitHub examples or examples of this that I can give my agent so I don’t have to so we don’t have to like relearn all this stuff or just some type of documentation or anything you can think of to help with that?
t
Thanks — I can get you a compact pack of concrete examples and links so your team doesn’t have to reinvent the wheel. Below are GitHub repos, docs, and recipes that show LiveKit + Telnyx voice and how to wire Telnyx SMS into agent flows, plus next steps I can provide for you. Quick links (code + docs) • LiveKit JavaScript client SDK (web): https://github.com/livekit/client-sdk-js • LiveKit React components (prebuilt UI): https://github.com/livekit/components-js • LiveKit Server SDKs (mint tokens on your backend): https://github.com/livekit/server-sdk • LiveKit Telephony / Agents (telephony docs & recipes): https://docs.livekit.io/agents/start/telephony/ • Telnyx LLM plugin guide (Telnyx usage with agents): https://docs.livekit.io/agents/models/llm/telnyx/ • SIP lifecycle and call recipes (examples for bridging calls): https://docs.livekit.io/recipes/sip_lifecycle/ • Outbound call recipe (utility to place calls via SIP/trunk): https://docs.livekit.io/recipes/make_call/ • Prebuilt agent tools list (useful for DTMF/webhook/HTTP tools): https://docs.livekit.io/agents/prebuilt/tools/ • Voice AI quickstart (step-by-step agent + telephony): https://docs.livekit.io/agents/start/voice-ai/ What these cover for you • Voice integration: Examples and recipes showing how Telnyx SIP trunks connect phone calls into LiveKit rooms (so your voice agent can handle calls). • SMS sending/receiving: Telnyx handles SMS via its REST API; LiveKit does not proxy SMS. Use an agent function/tool or backend endpoint to POST to Telnyx’s /messages endpoint for outbound SMS, and configure Telnyx webhooks for inbound SMS. • Full sample flows: The recipes and agent docs show how to trigger external HTTP tools from an agent, how to wire webhooks, and how to use prebuilt tools instead of coding everything from scratch. Practical deliverables I can prepare for you (pick one or more) • A ready-to-drop-in Node.js example that: ◦ Mints LiveKit tokens on your backend ◦ Connects an agent to Telnyx for voice (SIP) ◦ Sends SMS via Telnyx (
/v2/messages
) and handles inbound SMS webhooks • A Python example with the same pieces (token mint, SIP call handling, SMS send/receive) • A small repo layout or checklist your engineers can fork (env vars, routes, webhook handlers, tests) • A short agent prompt + function-tool example that asks for SMS consent in voice flow and calls Telnyx when confirmed One suggested next step • If you want the Node.js example, tell me: Do you prefer Express or Fastify? Also confirm whether your Telnyx setup will use a messaging profile (recommended). I’ll produce a concise repo-style example (backend + agent tool + webhook handler) you can hand to your engineers. Which deliverable would help you most right now? Sources: Telnyx LLM plugin guide | LiveKit Documentation | Introducing LiveKit Phone Numbers: zero to ringing in 60 seconds | LiveKit | Agents telephony integration | LiveKit Documentation | SIP Lifecycle Management Agent | LiveKit Documentation | Prebuilt tools | LiveKit Documentation, with interactive elements