{"slug": "build-a-fax-to-json-pipeline-in-python", "title": "Build a Fax-to-JSON Pipeline in Python", "summary": "Telnyx released a Python example that builds a fax-to-JSON pipeline using Flask and Telnyx AI Inference. The app receives fax events via webhooks, extracts structured data from document text using an AI model, and returns JSON output. It supports document types like invoices and purchase orders, with optional automatic type inference.", "body_md": "Fax is still part of a lot of real business workflows.\n\nHealthcare, insurance, logistics, legal, finance, and back-office teams still receive forms, invoices, purchase orders, prescriptions, claims, and signed documents by fax. The problem is what happens after the fax arrives.\n\nThis Python example shows how to receive a Telnyx fax event and turn document text into structured JSON with Telnyx AI Inference:\n\n[https://github.com/team-telnyx/telnyx-code-examples/tree/main/fax-to-structured-data-pipeline-python](https://github.com/team-telnyx/telnyx-code-examples/tree/main/fax-to-structured-data-pipeline-python)\n\nThe app is a small Flask API with these routes:\n\n```\nPOST /webhooks/fax   # receive Telnyx fax events\nPOST /extract        # extract structured data from document text\nGET  /faxes          # list queued fax metadata\nGET  /extracted      # list recent extraction results\nGET  /health         # health check\n```\n\nThe extraction route supports:\n\nThe AI call uses:\n\n```\nPOST /v2/ai/chat/completions\ngit clone https://github.com/team-telnyx/telnyx-code-examples.git\ncd telnyx-code-examples/fax-to-structured-data-pipeline-python\ncp .env.example .env\npip install -r requirements.txt\npython app.py\n```\n\nSet these values in `.env`\n\n:\n\n```\nTELNYX_API_KEY=your_telnyx_api_key\nTELNYX_PUBLIC_KEY=your_telnyx_public_key\nAI_MODEL=moonshotai/Kimi-K2.6\nHOST=127.0.0.1\n```\n\nYou do not need to send a live fax to test the extraction path. Send document text directly:\n\n```\ncurl -X POST http://localhost:5000/extract \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"type\": \"invoice\",\n    \"text\": \"Invoice #INV-1042 from Acme Medical Supplies dated 2026-07-01. Due 2026-07-31. Bill to North Clinic. Item: Nitrile gloves, quantity 10, unit price 12.50, total 125.00. Item: Face masks, quantity 5, unit price 20.00, total 100.00. Subtotal 225.00. Tax 18.00. Total 243.00. Payment terms Net 30.\"\n  }' | python3 -m json.tool\n```\n\nThe app asks the model for invoice-shaped JSON:\n\n```\n{\n  \"vendor\": \"Acme Medical Supplies\",\n  \"invoice_number\": \"INV-1042\",\n  \"date\": \"2026-07-01\",\n  \"due_date\": \"2026-07-31\",\n  \"line_items\": [\n    {\n      \"description\": \"Nitrile gloves\",\n      \"quantity\": 10,\n      \"unit_price\": 12.5,\n      \"total\": 125\n    }\n  ],\n  \"subtotal\": 225,\n  \"tax\": 18,\n  \"total\": 243,\n  \"payment_terms\": \"Net 30\"\n}\n```\n\nUse `type: \"auto\"`\n\nwhen you want the model to infer the document type:\n\n```\ncurl -X POST http://localhost:5000/extract \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"type\": \"auto\",\n    \"text\": \"Purchase Order PO-7781. Vendor: Harbor Office Supply. Ship to: 500 Market St, San Francisco, CA. SKU CHAIR-22, ergonomic chair, quantity 12, unit price 199.00. Total 2388.00. Delivery requested 2026-07-20.\"\n  }' | python3 -m json.tool\n```\n\nThe live fax route is:\n\n```\nPOST /webhooks/fax\n```\n\nThe app verifies the Telnyx webhook signature before trusting the event. When it receives `fax.received`\n\n, it queues metadata like:\n\nFor local webhook testing, expose your app:\n\n```\nngrok http 5000\n```\n\nThen set your Telnyx Fax Application webhook URL to:\n\n```\nhttps://<id>.ngrok.io/webhooks/fax\n```\n\nThe demo keeps state in memory. For production, I would add:\n\n`media_url`\n\nThe example repo is also agent-readable. A coding agent can inspect the README, API reference, guide, environment file, and app code, then help you add OCR, storage, tests, queue workers, or stricter validation.", "url": "https://wpnews.pro/news/build-a-fax-to-json-pipeline-in-python", "canonical_source": "https://dev.to/sonam_50a41a4ced7e6b4f3fa/build-a-fax-to-json-pipeline-in-python-4hc4", "published_at": "2026-07-08 01:47:48+00:00", "updated_at": "2026-07-08 01:58:18.519279+00:00", "lang": "en", "topics": ["developer-tools", "artificial-intelligence", "natural-language-processing", "ai-tools"], "entities": ["Telnyx", "Flask", "Telnyx AI Inference", "moonshotai/Kimi-K2.6", "ngrok"], "alternates": {"html": "https://wpnews.pro/news/build-a-fax-to-json-pipeline-in-python", "markdown": "https://wpnews.pro/news/build-a-fax-to-json-pipeline-in-python.md", "text": "https://wpnews.pro/news/build-a-fax-to-json-pipeline-in-python.txt", "jsonld": "https://wpnews.pro/news/build-a-fax-to-json-pipeline-in-python.jsonld"}}