--- name: configure-sip-codecs title: "Configure SIP Codecs" description: "Configure audio codecs for SIP trunks." language: python framework: flask telnyx_products: [SIP Trunking] --- # Production-ready Flask application for SIP codec configuration via Telnyx. Voice application. Built with Telnyx Migration, Number Porting, SIP Trunking. ## Telnyx API Endpoints Used - **Create SIP Connection**: `POST /v2/sip_connections` — [API reference](https://developers.telnyx.com/api/sip-connections/create-sip-connection) - **Retrieve SIP Connection**: `GET /v2/sip_connections/{id}` — [API reference](https://developers.telnyx.com/api/sip-connections/get-sip-connection) - **List SIP Connections**: `GET /v2/sip_connections` — [API reference](https://developers.telnyx.com/api/sip-connections/list-sip-connections) ## Architecture ``` Your PBX / SBC │ ▼ ┌──────────────────┐ │ Telnyx SIP Trunk │ │ (IP / FQDN auth) │ └────────┬─────────┘ │ ▼ PSTN / Telnyx Network ``` ## 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) | | `SIP_USERNAME` | `string` | `your_value` | **yes** | Sip username | — | | `SIP_PASSWORD` | `string` | `your_value` | **yes** | Sip password | — | | `SIP_ENDPOINT` | `string` | `your_value` | **yes** | Sip endpoint | — | | `FLASK_DEBUG` | `string` | `false` | no | Flask debug | — | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/configure-sip-codecs-python cp .env.example .env # ← fill in your credentials pip install -r requirements.txt python app.py # starts on http://localhost:5000 ``` ## API Reference ### `GET /sip/connections` HTTP endpoint to list all SIP connections. ```bash curl http://localhost:5000/sip/connections ``` **Response:** ```json { "connections": [ { "id": "1494404757140276705", "name": "Production SIP", "status": "active", "ip": "192.168.1.100" } ] } ``` ### `POST /sip/connections` HTTP endpoint to create a new SIP connection with codec configuration. ```bash curl -X POST http://localhost:5000/sip/connections \ -H "Content-Type: application/json" \ -d '{}' ``` **Response:** ```json { "connections": [ { "id": "1494404757140276705", "name": "Production SIP", "status": "active", "ip": "192.168.1.100" } ] } ``` ### `GET /sip/connections/` HTTP endpoint to retrieve codec configuration for a specific SIP connection. ```bash curl http://localhost:5000/sip/connections/example-id ``` **Response:** ```json { "connections": [ { "id": "1494404757140276705", "name": "Production SIP", "status": "active", "ip": "192.168.1.100" } ] } ``` ## 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 - [Inbound SIP Routing (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/inbound-sip-routing-python/README.md) - [Setup SIP Trunk (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/setup-sip-trunk-python/README.md) - [SIP Failover Routing (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/sip-failover-routing-python/README.md) - [SIP Load Balancer Health Check (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/sip-load-balancer-health-check-python/README.md) - [SIP Trunking Failover Monitor (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/sip-trunking-failover-monitor-python/README.md) ## Resources - [Telnyx Developer Docs](https://developers.telnyx.com) - [Telnyx Portal](https://portal.telnyx.com) ## Why Telnyx Telnyx is an **AI Communications Infrastructure** platform — voice, messaging, SIP, AI, and IoT on one private, global network.