> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spn.wtf/llms.txt
> Use this file to discover all available pages before exploring further.

# WhatsApp & Order Drafts API

> Webhook and order draft management

## Order Drafts

Order drafts are WhatsApp-parsed orders pending review before conversion to real client orders.

All routes are prefixed with `/order-drafts`.

### `GET /order-drafts`

Get all pending order drafts.

**Response**

```json theme={null}
[
  {
    "id": 1,
    "created_at": "2025-01-15T10:30:00",
    "phone_number": "917595903437",
    "raw_message": "2 rings 5g each for Priya",
    "entries": [
      {
        "id": 1,
        "item_name": "Ring",
        "weight": 5.0,
        "quantity": 2,
        "client_name": "Priya"
      }
    ]
  }
]
```

***

### `GET /order-drafts/{draft_id}`

Get a specific order draft with all its entries.

### `PUT /order-drafts/{draft_id}`

Update metadata of an order draft.

### `DELETE /order-drafts/{draft_id}`

Delete an order draft and all its entries.

***

### `PUT /order-drafts/{draft_id}/entries/{entry_id}`

Update a specific entry within a draft.

**Request Body**

```json theme={null}
{
  "item_name": "Ring",
  "weight": 5.2,
  "quantity": 2,
  "client_id": 1,
  "slip_number": "SL-042"
}
```

***

### `POST /order-drafts/{draft_id}/recalculate_slip_numbers`

Recalculate and reassign slip numbers for all entries in a draft.

### `POST /order-drafts/{draft_id}/convert`

Convert a reviewed draft into actual client orders. Creates real `client_order` records and deletes the draft.

***

## WhatsApp Webhook

### `POST /gupshup_incoming_webhook`

Receives incoming WhatsApp messages from Gupshup.

**Flow:**

1. Customer sends an order via WhatsApp
2. Gupshup forwards the message to this endpoint
3. Sender's phone number is checked against `ALLOWED_PHONE_NUMBERS`
4. If allowed, message is sent to OpenAI for structured parsing
5. An `OrderDraft` is created with parsed entries
6. A WhatsApp confirmation reply is sent back
7. Staff review the draft at `/order-drafts` in the frontend
8. Once confirmed, draft is converted to real client orders

<Warning>
  Only phone numbers listed in `ALLOWED_PHONE_NUMBERS` env var are processed. All others are silently ignored.
</Warning>

***

## Khata Upload

Khata upload processes scanned/photographed ledger sheets using OCR.

### `POST /khata-upload`

Upload a khata image/PDF and get back parsed entries (v1).

**Request:** `multipart/form-data`

| Field  | Type | Description                    |
| ------ | ---- | ------------------------------ |
| `file` | file | Image or PDF of the khata page |

### `POST /khata-upload-v2`

Upload and process using the v2 parsing pipeline.

### `POST /khata-submit`

Submit reviewed khata entries into the ledger system.

### `POST /khata/submit`

Alternative submission endpoint (v2 flow).
