Eedesy
Book Demo
HomeProductsContact
AI Voice Agent
  • Introduction
  • Quick Start
  • Architecture
  • Core Concepts
  • Configuration
  • Creating an Agent
  • Using Templates
  • System Prompt Configuration
  • Language Selection
  • Settings Reference
  • Overview
  • Upload Files (PDF, DOCX)
  • Web Crawling (URLs & Sitemaps)
  • Document Processing
  • RAG Integration
  • Troubleshooting
  • Creating a Campaign
  • CSV Upload & Variables
  • Scheduling & Retry Logic
  • Campaign Analytics
  • Contact Management
  • Uploading Contacts
  • Managing Contacts
  • Call History Enrichment
  • Trial Numbers
  • Purchasing Numbers
  • BYOP Setup
  • Exotel Setup
  • Telephony Providers Guide
  • Credits System
  • Provider Costs Breakdown
  • Usage Analytics
  • Credit Packs
  • Overview
  • Finding Your Workspace ID
  • Common Issues
  • Audio Quality
  • Latency Optimization
  • Error Codes
  • Overview
  • Twilio
  • Exotel
  • Plivo
  • Telnyx
  • WhatsApp
  • WebRTC Browser
  • Overview
  • Deepgram
  • Google Chirp
  • Azure Speech
  • ElevenLabs Scribe
  • AssemblyAI
  • OpenAI Whisper
  • Overview
  • Cartesia
  • ElevenLabs
  • Google TTS
  • Azure Neural
  • OpenAI TTS
  • Deepgram Aura
  • Sarvam Bulbul
  • HeyPixa Luna
  • Overview
  • OpenAI GPT-4o
  • Gemini 2.0/2.5 Flash
  • Gemini Live (Native Audio)
  • OpenAI Realtime
  • Anthropic Claude
  • Azure OpenAI
  • Overview
  • VAD (Voice Activity Detection)
  • Interruption Handling
  • Turn Detection
  • Audio Processing
  • Function Calling
  • Call Transfer
  • DTMF Handling
  • Call Recording
  • Exporting Recordings
  • Transcripts
  • Webhooks
  • Variables & Templates
  • Multi-Language Support
  • Hindi
  • Tamil
  • Assamese
  • Other Indian Languages
  • Latency Optimization
  • Scaling & Performance
  • Error Handling
  • Monitoring & Logging
  • Security Best Practices
  • Trigger Call via API
  • REST API
  • WebSocket Protocol
  • Webhooks
  • Webhook Subscriptions
  • SDKs
  • Overview
  • Financial Services
  • E-commerce
  • Logistics
  • Hyperlocal
  • Healthcare
  • Education
  • Overview
  • Customer Support
  • Sales & Lead Conversion
  • Marketing Campaigns
  • Debt Collection
  • Overview
  • Customer Support Bot
  • Order Status IVR
  • Appointment Booking
  • Lead Qualification
  • Outbound Sales
  • Recruitment Screening
  • Overview
  • Migrate from Vapi
  • Migrate from Retell AI
  • Migrate from Traditional IVR

Related Products

  • AI Voice Agent
  • AI Voice Assistant
  • Try Free
AI Voice Agent
  • Introduction
  • Quick Start
  • Architecture
  • Core Concepts
  • Configuration
  • Creating an Agent
  • Using Templates
  • System Prompt Configuration
  • Language Selection
  • Settings Reference
  • Overview
  • Upload Files (PDF, DOCX)
  • Web Crawling (URLs & Sitemaps)
  • Document Processing
  • RAG Integration
  • Troubleshooting
  • Creating a Campaign
  • CSV Upload & Variables
  • Scheduling & Retry Logic
  • Campaign Analytics
  • Contact Management
  • Uploading Contacts
  • Managing Contacts
  • Call History Enrichment
  • Trial Numbers
  • Purchasing Numbers
  • BYOP Setup
  • Exotel Setup
  • Telephony Providers Guide
  • Credits System
  • Provider Costs Breakdown
  • Usage Analytics
  • Credit Packs
  • Overview
  • Finding Your Workspace ID
  • Common Issues
  • Audio Quality
  • Latency Optimization
  • Error Codes
  • Overview
  • Twilio
  • Exotel
  • Plivo
  • Telnyx
  • WhatsApp
  • WebRTC Browser
  • Overview
  • Deepgram
  • Google Chirp
  • Azure Speech
  • ElevenLabs Scribe
  • AssemblyAI
  • OpenAI Whisper
  • Overview
  • Cartesia
  • ElevenLabs
  • Google TTS
  • Azure Neural
  • OpenAI TTS
  • Deepgram Aura
  • Sarvam Bulbul
  • HeyPixa Luna
  • Overview
  • OpenAI GPT-4o
  • Gemini 2.0/2.5 Flash
  • Gemini Live (Native Audio)
  • OpenAI Realtime
  • Anthropic Claude
  • Azure OpenAI
  • Overview
  • VAD (Voice Activity Detection)
  • Interruption Handling
  • Turn Detection
  • Audio Processing
  • Function Calling
  • Call Transfer
  • DTMF Handling
  • Call Recording
  • Exporting Recordings
  • Transcripts
  • Webhooks
  • Variables & Templates
  • Multi-Language Support
  • Hindi
  • Tamil
  • Assamese
  • Other Indian Languages
  • Latency Optimization
  • Scaling & Performance
  • Error Handling
  • Monitoring & Logging
  • Security Best Practices
  • Trigger Call via API
  • REST API
  • WebSocket Protocol
  • Webhooks
  • Webhook Subscriptions
  • SDKs
  • Overview
  • Financial Services
  • E-commerce
  • Logistics
  • Hyperlocal
  • Healthcare
  • Education
  • Overview
  • Customer Support
  • Sales & Lead Conversion
  • Marketing Campaigns
  • Debt Collection
  • Overview
  • Customer Support Bot
  • Order Status IVR
  • Appointment Booking
  • Lead Qualification
  • Outbound Sales
  • Recruitment Screening
  • Overview
  • Migrate from Vapi
  • Migrate from Retell AI
  • Migrate from Traditional IVR
  1. Docs
  2. AI Voice Agent
  3. Telephony
  4. Telnyx

Telnyx Voice Agent Integration

Step-by-step guide to integrate Telnyx with AI voice agents. WebSocket media streaming, global phone numbers, call control API setup, and real-time audio configuration.

Telnyx Integration

Telnyx provides global phone numbers with WebSocket media streaming for real-time voice agent interactions.

Overview

Feature Value
Media Protocol WebSocket
Audio Format PCM 16-bit
Sample Rate 8000 Hz
Regions Global
Best For International coverage

Configuration

Environment Variables

TELNYX_API_KEY=your-api-key
TELNYX_APP_ID=your-app-id
TELNYX_PUBLIC_KEY=your-public-key

Phone Number Setup

  1. Purchase a phone number in Telnyx Portal
  2. Create a TeXML application or use Call Control API
  3. Configure webhook URL for incoming calls

Implementation

Webhook Handler

type TelnyxHandler struct {
    apiKey    string
    sessions  map[string]*Session
}

func (h *TelnyxHandler) HandleWebhook(w http.ResponseWriter, r *http.Request) {
    var event TelnyxEvent
    json.NewDecoder(r.Body).Decode(&event)

    switch event.EventType {
    case "call.initiated":
        h.handleCallInitiated(event)
    case "call.answered":
        h.handleCallAnswered(event)
    case "call.hangup":
        h.handleCallHangup(event)
    case "streaming.started":
        h.handleStreamingStarted(event)
    case "streaming.stopped":
        h.handleStreamingStopped(event)
    }

    w.WriteHeader(http.StatusOK)
}

type TelnyxEvent struct {
    EventType string `json:"event_type"`
    Payload   struct {
        CallControlID string `json:"call_control_id"`
        CallLegID     string `json:"call_leg_id"`
        CallSessionID string `json:"call_session_id"`
        From          string `json:"from"`
        To            string `json:"to"`
        StreamURL     string `json:"stream_url"`
    } `json:"payload"`
}

Starting Media Streaming

func (h *TelnyxHandler) handleCallAnswered(event TelnyxEvent) {
    callControlID := event.Payload.CallControlID

    // Start media streaming
    req := StartStreamRequest{
        CallControlID: callControlID,
        StreamURL:     "wss://your-server.com/telnyx/media",
        StreamTrack:   "inbound_track", // or "both_tracks"
        EnableDialogflow: false,
    }

    h.telnyxClient.StartStreaming(req)

    // Create session
    h.sessions[callControlID] = NewSession(event)
}

func (h *TelnyxHandler) StartStreaming(req StartStreamRequest) error {
    url := fmt.Sprintf("https://api.telnyx.com/v2/calls/%s/actions/streaming_start",
        req.CallControlID)

    body := map[string]any{
        "stream_url":   req.StreamURL,
        "stream_track": req.StreamTrack,
    }

    jsonBody, _ := json.Marshal(body)

    httpReq, _ := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
    httpReq.Header.Set("Authorization", "Bearer "+h.apiKey)
    httpReq.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(httpReq)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    return nil
}

WebSocket Media Handler

type TelnyxMediaHandler struct {
    pipeline *Pipeline
}

func (h *TelnyxMediaHandler) HandleConnection(ws *websocket.Conn) {
    for {
        _, message, err := ws.ReadMessage()
        if err != nil {
            return
        }

        var msg TelnyxMediaMessage
        json.Unmarshal(message, &msg)

        switch msg.Event {
        case "connected":
            h.handleConnected(msg)
        case "start":
            h.handleStart(msg)
        case "media":
            h.handleMedia(msg, ws)
        case "stop":
            h.handleStop(msg)
        }
    }
}

type TelnyxMediaMessage struct {
    Event     string `json:"event"`
    StreamSid string `json:"stream_sid"`
    Media     struct {
        Track     string `json:"track"`
        Chunk     string `json:"chunk"`
        Timestamp string `json:"timestamp"`
        Payload   string `json:"payload"`
    } `json:"media"`
}

func (h *TelnyxMediaHandler) handleMedia(msg TelnyxMediaMessage, ws *websocket.Conn) {
    // Decode audio from base64
    audio, _ := base64.StdEncoding.DecodeString(msg.Media.Payload)

    // Process through pipeline
    h.pipeline.ProcessAudio(audio)

    // Send response audio
    h.sendAudio(ws, h.pipeline.GetOutputAudio())
}

func (h *TelnyxMediaHandler) sendAudio(ws *websocket.Conn, audio []byte) {
    msg := map[string]any{
        "event": "media",
        "media": map[string]string{
            "payload": base64.StdEncoding.EncodeToString(audio),
        },
    }

    jsonMsg, _ := json.Marshal(msg)
    ws.WriteMessage(websocket.TextMessage, jsonMsg)
}

Call Control API

Answer Call

func (c *TelnyxClient) AnswerCall(callControlID string) error {
    url := fmt.Sprintf("https://api.telnyx.com/v2/calls/%s/actions/answer",
        callControlID)

    body := map[string]any{}
    jsonBody, _ := json.Marshal(body)

    req, _ := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
    req.Header.Set("Authorization", "Bearer "+c.apiKey)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    return nil
}

Hangup Call

func (c *TelnyxClient) HangupCall(callControlID string) error {
    url := fmt.Sprintf("https://api.telnyx.com/v2/calls/%s/actions/hangup",
        callControlID)

    body := map[string]any{}
    jsonBody, _ := json.Marshal(body)

    req, _ := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
    req.Header.Set("Authorization", "Bearer "+c.apiKey)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    return err
}

Transfer Call

func (c *TelnyxClient) TransferCall(callControlID, destination string) error {
    url := fmt.Sprintf("https://api.telnyx.com/v2/calls/%s/actions/transfer",
        callControlID)

    body := map[string]any{
        "to": destination,
    }
    jsonBody, _ := json.Marshal(body)

    req, _ := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
    req.Header.Set("Authorization", "Bearer "+c.apiKey)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    return err
}

Outbound Calls

func (c *TelnyxClient) DialCall(from, to, webhookURL string) (*Call, error) {
    url := "https://api.telnyx.com/v2/calls"

    body := map[string]any{
        "connection_id": c.connectionID,
        "to":            to,
        "from":          from,
        "webhook_url":   webhookURL,
        "answering_machine_detection": "detect",
    }
    jsonBody, _ := json.Marshal(body)

    req, _ := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
    req.Header.Set("Authorization", "Bearer "+c.apiKey)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    var result struct {
        Data Call `json:"data"`
    }
    json.NewDecoder(resp.Body).Decode(&result)

    return &result.Data, nil
}

TeXML Alternative

TwiML-Compatible Markup

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Connect>
        <Stream url="wss://your-server.com/telnyx/media" />
    </Connect>
</Response>

TeXML Handler

func (h *TelnyxHandler) HandleIncomingCall(w http.ResponseWriter, r *http.Request) {
    texml := `<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Connect>
        <Stream url="wss://your-server.com/telnyx/media" />
    </Connect>
</Response>`

    w.Header().Set("Content-Type", "application/xml")
    w.Write([]byte(texml))
}

Audio Format

Configuration

type TelnyxAudioConfig struct {
    Encoding   string // "PCMU" or "PCMA"
    SampleRate int    // 8000
    Channels   int    // 1
}

var DefaultAudioConfig = TelnyxAudioConfig{
    Encoding:   "PCMU", // μ-law
    SampleRate: 8000,
    Channels:   1,
}

Audio Processing

func (h *TelnyxMediaHandler) processInboundAudio(audio []byte) []byte {
    // Decode μ-law to linear PCM
    pcm := mulawDecode(audio)

    // Upsample to 16kHz for STT
    upsampled := resample(pcm, 8000, 16000)

    return upsampled
}

func (h *TelnyxMediaHandler) processOutboundAudio(audio []byte) []byte {
    // Downsample from TTS output
    downsampled := resample(audio, 24000, 8000)

    // Encode to μ-law
    mulaw := mulawEncode(downsampled)

    return mulaw
}

Error Handling

func (h *TelnyxHandler) handleError(callControlID string, err error) {
    log.Printf("Telnyx error for call %s: %v", callControlID, err)

    // Attempt to play error message
    h.speakAndHangup(callControlID,
        "I apologize, we're experiencing technical difficulties. Please try again later.")
}

func (h *TelnyxHandler) speakAndHangup(callControlID, message string) {
    url := fmt.Sprintf("https://api.telnyx.com/v2/calls/%s/actions/speak",
        callControlID)

    body := map[string]any{
        "payload": message,
        "voice":   "female",
        "language": "en-US",
    }
    jsonBody, _ := json.Marshal(body)

    req, _ := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
    req.Header.Set("Authorization", "Bearer "+h.apiKey)
    req.Header.Set("Content-Type", "application/json")

    http.DefaultClient.Do(req)

    // Wait and hangup
    time.AfterFunc(5*time.Second, func() {
        h.HangupCall(callControlID)
    })
}

Best Practices

1. Handle Answering Machine Detection

func (h *TelnyxHandler) handleCallAnswered(event TelnyxEvent) {
    amdResult := event.Payload.AnsweringMachineDetection

    if amdResult == "machine_start" || amdResult == "machine_end_beep" {
        // It's a voicemail
        h.handleVoicemail(event)
        return
    }

    // Human answered
    h.startVoiceAgent(event)
}

2. Implement Call Recording

func (c *TelnyxClient) StartRecording(callControlID string) error {
    url := fmt.Sprintf("https://api.telnyx.com/v2/calls/%s/actions/record_start",
        callControlID)

    body := map[string]any{
        "format":   "mp3",
        "channels": "dual",
    }
    jsonBody, _ := json.Marshal(body)

    req, _ := http.NewRequest("POST", url, bytes.NewReader(jsonBody))
    req.Header.Set("Authorization", "Bearer "+c.apiKey)
    req.Header.Set("Content-Type", "application/json")

    resp, err := http.DefaultClient.Do(req)
    return err
}

Next Steps

  • WebRTC - Browser-based calling
  • Twilio - Alternative provider
  • Audio Processing - Audio handling
Previous
Plivo
Next
WhatsApp

On this page

  • Overview
  • Configuration
  • Environment Variables
  • Phone Number Setup
  • Implementation
  • Webhook Handler
  • Starting Media Streaming
  • WebSocket Media Handler
  • Call Control API
  • Answer Call
  • Hangup Call
  • Transfer Call
  • Outbound Calls
  • TeXML Alternative
  • TwiML-Compatible Markup
  • TeXML Handler
  • Audio Format
  • Configuration
  • Audio Processing
  • Error Handling
  • Best Practices
  • 1. Handle Answering Machine Detection
  • 2. Implement Call Recording
  • Next Steps

Stay Updated

Get the latest updates on AI voice technology, product releases, and exclusive resources.

Get Started

Try our products for free
AI Voice Agent
Build voice AI for calls
WhatsApp AI Bot
Automate WhatsApp chats
Website Chatbot
AI chat for websites
Edesy CRM
Manage leads & customers
Book a DemoCall UsEmail Us
Eedesy

Your all-in-one platform for digital innovation. We build AI-powered solutions that transform how businesses operate.

[email protected]+91 95475 31359

Products

  • AI Voice Assistant
  • WhatsApp Voice AI
  • WhatsApp Bot Builder
  • AI Website Chatbot
  • AI-SDR
  • Number Masking
  • Shopify Apps
  • View All Products

Solutions

  • For E-commerce
  • For Healthcare
  • For Real Estate
  • For Restaurants
  • For Appointments
  • View All Use Cases

Services

  • AI Chatbot Development
  • Voice AI Development
  • Shopify Development
  • SaaS Development
  • WhatsApp API Integration
  • View All Services

Resources

  • Documentation
  • Voice Agent Docs
  • API Reference
  • Number Masking API Docs
  • Blog
  • Changelog
  • Book a Demo

Company

  • About Us
  • Contact
  • Careers
  • Privacy Policy
  • Terms of Service

Products

  • AI Voice Assistant
  • WhatsApp Voice AI
  • WhatsApp Bot Builder
  • AI Website Chatbot
  • AI-SDR
  • Number Masking
  • Shopify Apps
  • View All Products

Solutions

  • For E-commerce
  • For Healthcare
  • For Real Estate
  • For Restaurants
  • For Appointments
  • View All Use Cases

Services

  • AI Chatbot Development
  • Voice AI Development
  • Shopify Development
  • SaaS Development
  • WhatsApp API Integration
  • View All Services

Resources

  • Documentation
  • Voice Agent Docs
  • API Reference
  • Number Masking API Docs
  • Blog
  • Changelog
  • Book a Demo

Company

  • About Us
  • Contact
  • Careers
  • Privacy Policy
  • Terms of Service
  • AI Voice Assistant
  • WhatsApp Voice AI
  • WhatsApp Bot Builder
  • AI Website Chatbot
  • AI-SDR
  • Number Masking
  • Shopify Apps
  • View All Products
  • For E-commerce
  • For Healthcare
  • For Real Estate
  • For Restaurants
  • For Appointments
  • View All Use Cases
  • AI Chatbot Development
  • Voice AI Development
  • Shopify Development
  • SaaS Development
  • WhatsApp API Integration
  • View All Services
  • Documentation
  • Voice Agent Docs
  • API Reference
  • Number Masking API Docs
  • Blog
  • Changelog
  • Book a Demo
  • About Us
  • Contact
  • Careers
  • Privacy Policy
  • Terms of Service

Popular Free Tools

Compress PDFMerge PDFPDF to WordGST CalculatorEMI CalculatorSIP CalculatorJSON FormatterBase64 EncoderImage CompressorQR Code GeneratorVoice AI ROI CalculatorAmazon FBA CalculatorAI Email WriterVideo to GIFPrivacy Policy GeneratorCRM ROI CalculatorMeeting Cost Calculator
Categories:PDF ToolsDeveloper ToolsFinance CalculatorsImage ToolsVideo ToolsAI Writing ToolsAudio ToolsWhatsApp ToolsDocument GeneratorsVoice AI ToolsE-commerce ToolsView All Tools

© 2026 Edesy Technology Labs Pvt Ltd

SSL Secured
99.9% Uptime