--- name: verify-phone-number-otp-flow title: "Verify Phone Number OTP Flow" description: "Verify Phone Number OTP Flow — Telnyx Verify API with SMS primary and voice call fallback." language: python framework: flask telnyx_products: [Verify] channel: [voice] --- # Verify Phone Number OTP Flow Verify Phone Number OTP Flow — Telnyx Verify API with SMS primary and voice call fallback. ## Telnyx API Endpoints Used - **Create Verification**: `POST /v2/verifications` — [API reference](https://developers.telnyx.com/api/verify/create-verification) ## Architecture ``` User requests verification │ ▼ ┌──────────────────┐ │ Generate OTP │ └────────┬─────────┘ │ ├──► SMS code │ ▼ ┌──────────────────┐ │ Verify code │ ── user submits OTP └────────┬─────────┘ │ ▼ ✓ Verified / ✗ Denied ``` ## 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) | | `VERIFY_PROFILE_ID` | `string` | `your_value` | **yes** | Verify profile id | — | | `PORT` | `integer` | `5000` | no | HTTP server port | — | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/verify-phone-number-otp-flow-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` ## API Reference ### `POST /verify/start` Triggers start ```bash curl -X POST http://localhost:5000/verify/start \ -H "Content-Type: application/json" \ -d '{ "phone": "+12125551234", "channel": "sms" }' ``` **Response:** ```json { "verification_id": "ver-abc123", "status": "pending", "channel": "sms", "phone": "+12125551234" } ``` ### `POST /verify/voice-fallback` Triggers voice-fallback ```bash curl -X POST http://localhost:5000/verify/voice-fallback \ -H "Content-Type: application/json" \ -d '{ "phone": "+12125551234", "channel": "sms" }' ``` **Response:** ```json { "verification_id": "ver-abc123", "status": "pending", "channel": "sms", "phone": "+12125551234" } ``` ### `POST /verify/check` Triggers check ```bash curl -X POST http://localhost:5000/verify/check \ -H "Content-Type: application/json" \ -d '{ "phone": "+12125551234", "channel": "sms" }' ``` **Response:** ```json { "verification_id": "ver-abc123", "status": "pending", "channel": "sms", "phone": "+12125551234" } ``` ### `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 - [Abandoned Cart Recovery (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/abandoned-cart-recovery-python/README.md) - [Accounting Tax Season Line (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/accounting-tax-season-line-python/README.md) - [After Hours Nurse Triage (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/after-hours-nurse-triage-python/README.md) - [AI Appointment Booking SMS Flow (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-appointment-booking-sms-flow-python/README.md) - [AI Appointment Reminder SMS Voice (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-appointment-reminder-sms-voice-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) ## Why Telnyx Telnyx is an **AI Communications Infrastructure** platform — voice, messaging, SIP, AI, and IoT on one private, global network.