--- name: call-analytics-dashboard-api title: "Call Analytics Dashboard API" description: "Pull call detail records from the Telnyx API and expose call usage analytics through a dashboard API." language: python framework: flask telnyx_products: [SMS/MMS] channel: [sms] --- # Call Analytics Dashboard API — pull CDRs and build usage analytics. SMS application. Built with Telnyx CDR, Migration, Number Porting, SMS/MMS. ## Telnyx API Endpoints Used - **Send Message**: `POST /v2/messages` — [API reference](https://developers.telnyx.com/api/messaging/send-message) - **List Phone Numbers**: `GET /v2/phone_numbers` — [API reference](https://developers.telnyx.com/api/numbers/list-phone-numbers) ## Telnyx Webhook Events This app handles these webhook events ([Messaging docs](https://developers.telnyx.com/docs/api/v2/messaging)): - `message.received` — Inbound SMS/MMS received ## Architecture ``` Inbound SMS/MMS │ ▼ ┌──────────────────┐ │ Telnyx Messaging │ └────────┬─────────┘ │ └──► JSON response ``` ## 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) | | `PORT` | `integer` | `5000` | no | HTTP server port | — | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/call-analytics-dashboard-api-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` - **Messaging Profile** → Inbound Webhook URL → `https://.ngrok.io/webhooks/sms` ## API Reference ### `GET /analytics/calls` Returns calls ```bash curl http://localhost:5000/analytics/calls ``` **Response:** ```json { "calls": [ { "call_id": "v3:uMi2qMWHT-mLFGkEm4t9tA", "from": "+18005551234", "to": "+12125559876", "duration_seconds": 145, "status": "completed" } ] } ``` ### `GET /analytics/numbers` Returns numbers ```bash curl http://localhost:5000/analytics/numbers ``` **Response:** ```json { "numbers": [ { "phone_number": "+18005551234", "status": "active", "type": "local", "region": "US-CA" } ] } ``` ### `GET /analytics/messaging` Returns messaging ```bash curl http://localhost:5000/analytics/messaging ``` **Response:** ```json { "period": "2026-07-15", "total_calls": 1247, "avg_duration_seconds": 186, "inbound": 823, "outbound": 424, "peak_hour": "14:00", "cost_usd": 42.18 } ``` ### `GET /health` Returns health ```bash curl http://localhost:5000/health ``` **Response:** ```json { "status": "ok", "uptime_seconds": 3842, "active_sessions": 2, "version": "1.0.0" } ``` ## 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 Forwarding (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/call-forwarding-python/README.md) ## Resources - [Call Control Guide](https://developers.telnyx.com/docs/voice/call-control) - [Messaging Guide](https://developers.telnyx.com/docs/messaging) - [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.