--- name: monitor-iot-data-usage title: "Monitor IoT Data Usage" description: "Production-ready Flask application for monitoring SIM card data usage via Telnyx IoT API." language: python framework: flask telnyx_products: [IoT/SIM] --- # Production-ready Flask application for monitoring SIM card Production-ready Flask application for monitoring SIM card data usage via Telnyx IoT API. ## Telnyx API Endpoints Used - **SIM Cards**: `GET /v2/sim_cards` — [API reference](https://developers.telnyx.com/api/sim-cards/list-sim-cards) ## Telnyx Webhook Events This app handles these webhook events: - `sim_card.data_limit.reached` — SIM card data usage limit reached - `sim_card.status.changed` — SIM card status changed (active, suspended, etc.) ## Architecture ``` ┌──────────────────┐ │ IoT Device Event │ │ (SIM data / │ │ sensor reading) │ └────────┬─────────┘ │ ▼ ┌──────────────────┐ │ Threshold Check │ └────────┬─────────┘ │ ▼ 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) | | `DATA_LIMIT_THRESHOLD_MB` | `string` | `500` | no | Data limit threshold mb | — | | `FLASK_DEBUG` | `string` | `false` | no | Flask debug | — | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/monitor-iot-data-usage-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 /health` Health check endpoint for monitoring infrastructure. ```bash curl http://localhost:5000/health ``` **Response:** ```json { "status": "ok", "uptime_seconds": 3842, "active_sessions": 2, "version": "1.0.0" } ``` ### `GET /sim-cards` List all SIM cards in the account. ```bash curl http://localhost:5000/sim-cards ``` **Response:** ```json { "items": [ { "id": "item-001", "status": "active", "created_at": "2026-07-15T14:30:00Z" } ] } ``` ### `GET /sim-cards/` Retrieve details for a specific SIM card. ```bash curl http://localhost:5000/sim-cards/example-id ``` **Response:** ```json { "items": [ { "id": "item-001", "status": "active", "created_at": "2026-07-15T14:30:00Z" } ] } ``` ### `GET /sim-cards//usage` Get data usage metrics for a specific SIM card. ```bash curl http://localhost:5000/sim-cards/example-id/usage ``` **Response:** ```json { "items": [ { "id": "item-001", "status": "active", "created_at": "2026-07-15T14:30:00Z" } ] } ``` ### `GET /sim-cards//health` Get comprehensive health status for a SIM card. ```bash curl http://localhost:5000/sim-cards/example-id/health ``` **Response:** ```json { "items": [ { "id": "item-001", "status": "active", "created_at": "2026-07-15T14:30:00Z" } ] } ``` ### `POST /sim-cards//activate` Activate a SIM card. ```bash curl -X POST http://localhost:5000/sim-cards/example-id/activate \ -H "Content-Type: application/json" \ -d '{}' ``` **Response:** ```json { "id": "item-1750280400", "status": "created", "created_at": "2026-07-15T14:30:00Z" } ``` ## Webhook Endpoints ### `POST /webhooks/sim-events` Receives Telnyx webhook events for `/webhooks/sim-events`. ## 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 - [Activate Sim Card (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/activate-sim-card-python/README.md) - [IoT Fleet Alert Escalation (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/iot-fleet-alert-escalation-python/README.md) - [IoT Panic Button Voice Alert (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/iot-panic-button-voice-alert-python/README.md) - [IoT Smart Building Voice Control (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/iot-smart-building-voice-control-python/README.md) - [Provision Esim (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/provision-esim-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.