--- name: webrtc-browser-calling title: "WebRTC Browser Calling" description: "Enable browser-based calling with WebRTC." language: python framework: flask telnyx_products: [Voice, WebRTC] --- # Production-ready WebRTC calling application with Telnyx Voice API and FastAPI. Voice application. Built with Telnyx Migration, Number Porting, Voice, WebRTC. ## Telnyx API Endpoints Used - **Call Control: Answer**: `POST /v2/calls/{id}/actions/answer` — [API reference](https://developers.telnyx.com/api/call-control/answer-call) ## Telnyx Webhook Events This app handles these webhook events ([Call Control docs](https://developers.telnyx.com/docs/api/v2/call-control)): - `call.answered` — Call connected — app begins interaction - `call.dtmf.received` — DTMF tone detected during call - `call.hangup` — Call ended — app cleans up session, triggers post-call processing - `call.initiated` — New inbound or outbound call detected ## Architecture ``` Inbound Phone Call │ ▼ ┌──────────────────┐ │ Call Control │ └────────┬─────────┘ │ ├──► DTMF │ ▼ Call completed ``` ## How It Works 1. Receives incoming call via Telnyx Call Control webhook 2. Transfers call to human agent when needed ## Why Telnyx Telnyx is an **AI Communications Infrastructure** platform — voice, messaging, SIP, AI, and IoT on one private, global network. - **Single-vendor voice stack** — call control, STT, TTS, and recording from one API. No multi-vendor coordination. ## Environment Variables Copy `.env.example` to `.env` and fill in: | Variable | Type | Example | Required | Description | Where to get it | |----------|------|---------|----------|-------------|-----------------| | `TELNYX_API_KEY` | `string` | `KEY0123456789ABCDEF` | **yes** | Telnyx API v2 key | [Portal](https://portal.telnyx.com/api-keys) | | `TELNYX_PHONE_NUMBER` | `string` | `your_value` | **yes** | Telnyx phone number | — | | `TELNYX_CONNECTION_ID` | `string` | `your_value` | **yes** | Telnyx connection id | — | | `WEBHOOK_URL` | `string` | `https://your-server.example.com` | no | Public URL for receiving webhooks | — | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/webrtc-browser-calling-python cp .env.example .env # ← fill in your credentials pip install -r requirements.txt python app.py # starts on http://localhost:5000 ``` ### Webhook Configuration 1. Expose your local server: ```bash ngrok http 5000 ``` 2. Copy the HTTPS URL and configure in [Telnyx Portal](https://portal.telnyx.com): - **Call Control Application** → Webhook URL → `https://.ngrok.io/webhooks/voice` ## Testing **Get WebRTC credentials:** ```bash curl http://localhost:5000/api/credentials ``` **Response:** ```json {"sip_username": "user@sip.telnyx.com", "credential_id": "", "status": "ok"} ``` **Health check:** ```bash curl http://localhost:5000/health ``` ## Troubleshooting | Issue | Cause | Fix | |-------|-------|-----| | `401 Unauthorized` | Invalid or missing API key | Verify `TELNYX_API_KEY` in `.env` matches your key in the [Portal](https://portal.telnyx.com/api-keys) | | Webhook not received | Local server not publicly reachable | Expose it with a tunnel (e.g. ngrok) and set the webhook URL in the [Telnyx Portal](https://portal.telnyx.com) | | `422 Unprocessable Entity` | Missing or malformed request fields | Check the request body against the API Reference above | ## Related Examples - [Branded Caller Id Manager (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/branded-caller-id-manager-python/README.md) - [Build Conference Calling (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/build-conference-calling-python/README.md) - [Build IVR Phone Menu (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/build-ivr-phone-menu-python/README.md) - [Bulk Number Validation Cleaner (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/bulk-number-validation-cleaner-python/README.md) - [Call Analytics Dashboard Api (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/call-analytics-dashboard-api-python/README.md) ## Resources - [Call Control Guide](https://developers.telnyx.com/docs/voice/call-control) - [Telnyx Developer Docs](https://developers.telnyx.com) - [Telnyx Portal](https://portal.telnyx.com) ## API Reference ### `GET /health` Health check endpoint. ```bash curl http://localhost:5000/health ``` ```json {"status": "ok"} ```