--- name: sms-two-factor-auth title: "SMS Two Factor Auth" description: "Implement SMS-based two-factor authentication." language: python framework: flask telnyx_products: [Cloud Storage, SMS/MMS, Verify] --- # Production-ready OTP 2FA system with Flask and Telnyx SMS. SMS application. Built with Telnyx Cloud Storage, Migration, Number Porting, SMS/MMS. ## Telnyx API Endpoints Used - **Send Message (OTP)**: `POST /v2/messages` — [API reference](https://developers.telnyx.com/api/messaging/send-message) ## Architecture ``` API Request │ ▼ ┌──────────────────┐ │ Telnyx Messaging │ │ • Summarization │ └────────┬─────────┘ │ └──► 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) | | `OTP_EXPIRY_SECONDS` | `string` | `300` | no | Otp expiry seconds | — | | `TELNYX_PHONE_NUMBER` | `string` | `your_value` | **yes** | Telnyx phone number | — | | `FLASK_DEBUG` | `string` | `false` | no | Flask debug | — | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/sms-two-factor-auth-python cp .env.example .env # ← fill in your credentials pip install -r requirements.txt python app.py # starts on http://localhost:5000 ``` ## API Reference ### `POST /auth/request-otp` Request an OTP to be sent to the provided phone number. ```bash curl -X POST http://localhost:5000/auth/request-otp \ -H "Content-Type: application/json" \ -d '{}' ``` **Response:** ```json { "id": "item-1750280400", "status": "created", "created_at": "2026-07-15T14:30:00Z" } ``` ### `POST /auth/verify-otp` Verify the OTP code provided by the user. ```bash curl -X POST http://localhost:5000/auth/verify-otp \ -H "Content-Type: application/json" \ -d '{ "phone": "+12125551234", "channel": "sms" }' ``` **Response:** ```json { "verification_id": "ver-abc123", "status": "pending", "channel": "sms", "phone": "+12125551234" } ``` ### `GET /auth/otp-status` Get OTP status for a phone number (for testing/debugging only). ```bash curl http://localhost:5000/auth/otp-status ``` **Response:** ```json { "items": [ { "id": "item-001", "status": "active", "created_at": "2026-07-15T14:30:00Z" } ] } ``` ## 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 - [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.