--- name: fax-to-ai-document-processor title: "Fax to AI Document Processor" description: "Fax to AI Document Processor — receive fax, AI extracts data, forwards structured summary." language: python framework: flask telnyx_products: [SMS/MMS, AI Inference] --- # Fax to AI Document Processor Fax to AI Document Processor — receive fax, AI extracts data, forwards structured summary. ## Telnyx API Endpoints Used - **Send Message**: `POST /v2/messages` — [API reference](https://developers.telnyx.com/api/messaging/send-message) - **AI Inference**: `POST /v2/ai/chat/completions` — [API reference](https://developers.telnyx.com/api/inference/chat-completions) ## Telnyx Webhook Events This app handles these webhook events: - `fax.received` — Inbound fax received — media URL available ## External Service Integrations - **Email / SMTP** — Email notifications and alerts ## Architecture ``` API Request │ ▼ ┌──────────────────┐ │ Parse message │ └────────┬─────────┘ │ ▼ ┌──────────────────┐ │ AI Inference │ │ • Classification / triage│ │ • Summarization │ └────────┬─────────┘ │ ◄──── conversation loop │ └──► Email ``` ## 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) | | `AI_MODEL` | `string` | `moonshotai/Kimi-K2.6` | no | Telnyx AI Inference model name | [Portal](https://developers.telnyx.com/docs/inference/models) | | `FAX_NUMBER` | `string` | `your_value` | **yes** | Fax number | — | | `FORWARD_EMAIL` | `string` | `your_value` | **yes** | Forward email | — | | `PORT` | `integer` | `5000` | no | HTTP server port | — | ## Setup ```bash git clone https://github.com/team-telnyx/telnyx-code-examples.git cd telnyx-code-examples/fax-to-ai-document-processor-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): - **Fax Application** → Webhook URL → `https://.ngrok.io/webhooks/fax` ## API Reference ### `GET /faxes` Returns faxes ```bash curl http://localhost:5000/faxes ``` **Response:** ```json { "items": [ { "id": "item-001", "status": "active", "created_at": "2026-07-15T14:30:00Z" } ] } ``` ### `GET /health` Returns health ```bash curl http://localhost:5000/health ``` **Response:** ```json { "status": "ok", "uptime_seconds": 3842, "active_sessions": 2, "version": "1.0.0" } ``` ## Webhook Endpoints ### `POST /webhooks/fax` Receives [Telnyx Fax](https://developers.telnyx.com/docs/fax) webhook events. **Example payload:** ```json { "data": { "event_type": "fax.received", "id": "b7c8d9e0-1234-5678-9abc-def012345678", "occurred_at": "2026-07-15T14:30:00.000Z", "payload": { "fax_id": "b7c8d9e0-1234-5678-9abc-def012345678", "direction": "inbound", "from": "+12125551234", "to": "+13105559876", "status": "received", "media_url": "https://api.telnyx.com/v2/faxes/b7c8d9e0/media", "page_count": 3, "quality": "fine" }, "record_type": "event" } } ``` ## 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 - [AI After Hours Emergency Triage (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-after-hours-emergency-triage-python/README.md) - [AI Assistant Knowledge Base (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-assistant-knowledge-base-python/README.md) - [AI Assistant Multi Tool (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-assistant-multi-tool-python/README.md) - [AI Assistant Phone Setup (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-assistant-phone-setup-python/README.md) - [AI Audiobook Narrator (Python)](https://raw.githubusercontent.com/team-telnyx/telnyx-code-examples/main/ai-audiobook-narrator-python/README.md) ## Resources - [Fax Guide](https://developers.telnyx.com/docs/fax) - [AI Inference Guide](https://developers.telnyx.com/docs/inference) - [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.