# Build a Fax-to-JSON Pipeline in Python

> Source: <https://dev.to/sonam_50a41a4ced7e6b4f3fa/build-a-fax-to-json-pipeline-in-python-4hc4>
> Published: 2026-07-08 01:47:48+00:00

Fax is still part of a lot of real business workflows.

Healthcare, 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.

This Python example shows how to receive a Telnyx fax event and turn document text into structured JSON with Telnyx AI Inference:

[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)

The app is a small Flask API with these routes:

```
POST /webhooks/fax   # receive Telnyx fax events
POST /extract        # extract structured data from document text
GET  /faxes          # list queued fax metadata
GET  /extracted      # list recent extraction results
GET  /health         # health check
```

The extraction route supports:

The AI call uses:

```
POST /v2/ai/chat/completions
git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/fax-to-structured-data-pipeline-python
cp .env.example .env
pip install -r requirements.txt
python app.py
```

Set these values in `.env`

:

```
TELNYX_API_KEY=your_telnyx_api_key
TELNYX_PUBLIC_KEY=your_telnyx_public_key
AI_MODEL=moonshotai/Kimi-K2.6
HOST=127.0.0.1
```

You do not need to send a live fax to test the extraction path. Send document text directly:

```
curl -X POST http://localhost:5000/extract \
  -H "Content-Type: application/json" \
  -d '{
    "type": "invoice",
    "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."
  }' | python3 -m json.tool
```

The app asks the model for invoice-shaped JSON:

```
{
  "vendor": "Acme Medical Supplies",
  "invoice_number": "INV-1042",
  "date": "2026-07-01",
  "due_date": "2026-07-31",
  "line_items": [
    {
      "description": "Nitrile gloves",
      "quantity": 10,
      "unit_price": 12.5,
      "total": 125
    }
  ],
  "subtotal": 225,
  "tax": 18,
  "total": 243,
  "payment_terms": "Net 30"
}
```

Use `type: "auto"`

when you want the model to infer the document type:

```
curl -X POST http://localhost:5000/extract \
  -H "Content-Type: application/json" \
  -d '{
    "type": "auto",
    "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."
  }' | python3 -m json.tool
```

The live fax route is:

```
POST /webhooks/fax
```

The app verifies the Telnyx webhook signature before trusting the event. When it receives `fax.received`

, it queues metadata like:

For local webhook testing, expose your app:

```
ngrok http 5000
```

Then set your Telnyx Fax Application webhook URL to:

```
https://<id>.ngrok.io/webhooks/fax
```

The demo keeps state in memory. For production, I would add:

`media_url`

The 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.
