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

# Vendor templates

> Create YAML templates so invoices from known vendors extract at Tier 1 with zero LLM cost.

Vendor templates extract structured fields from known vendor layouts using OCR text and regular expressions. When a template matches enough required fields, SnapIt keeps the invoice on Tier 1. Tier 1 uses no LLM tokens, so extraction is free.

Template confidence is calculated as:

```
matched_required_fields / total_required_fields × 100
```

SnapIt accepts the Tier 1 result when confidence reaches the threshold (default 85). Below the threshold, the invoice escalates to the LLM tiers.

## Create and edit templates

<Steps>
  <Step title="Create the vendor">
    Add the supplier under **Manage → Vendors** if it does not exist yet.
  </Step>

  <Step title="Open the Templates page">
    Go to **Manage → Templates**. The vendor list shows which vendors already have a saved template and which are missing one.
  </Step>

  <Step title="Generate or edit the YAML">
    Select a vendor and edit the YAML in the editor panel. Click **Generate Template** to populate the editor with all supported fields and common regex patterns for that vendor.
  </Step>

  <Step title="Save">
    Click **Save**. New uploads for that vendor can take the Tier 1 path as soon as confidence clears the threshold.
  </Step>
</Steps>

## Template structure

A template has two sections: `fields` for header values and `tables.line_items` for line-item rows.

```yaml theme={null}
vendor_name: "Acme Supplies"
currency: "MYR"
fields:
  invoice_number:
    regex: 'Invoice\s*No[:\s]+([A-Z0-9/-]+)'
    required: true
  invoice_date:
    regex: '(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2,4})'
    required: true
  total_amount:
    regex: 'TOTAL\s*:?\s*(?:MYR|RM)?\s*([\d,]+\.\d{2})'
    required: true
  tax_amount:
    regex: 'GST\s*/\s*SST\s*:?\s*([\d,]+\.\d{2})'
    required: false
tables:
  line_items:
    header_keywords: ["Description", "Item", "Product"]
    columns:
      - name: description
        index: 0
      - name: quantity
        index: 1
        type: number
      - name: unit_price
        index: 2
        type: number
      - name: line_total
        index: 3
        type: number
```

### Header fields

Each entry under `fields` names a capture and provides a regex. The first capture group becomes the field value.

* `required: true` fields count toward the confidence score. Mark the fields that reliably appear on every invoice from that vendor as required.
* `required: false` fields are extracted when present but never lower confidence.

### Line-item rules

`tables.line_items` maps OCR table columns to invoice line fields:

* `header_keywords` identifies the line-item table. The extractor looks for a table whose header row contains one of these keywords.
* `columns` maps each column by zero-based `index` to a field name. Set `type: number` for quantity and amount columns so values are parsed numerically.

## Manage templates through the API

Workspace users can also manage templates with a session JWT. These endpoints use the Bearer JWT from login, not a programmatic API key.

<CodeGroup>
  ```bash Save a template theme={null}
  curl -X PUT https://api.trysnapit.com/api/vendors/<vendor-id>/template \
    -H "Authorization: Bearer <jwt>" \
    -H "Content-Type: text/plain" \
    --data-binary @my-template.yml
  ```

  ```bash Get a vendor's template theme={null}
  curl https://api.trysnapit.com/api/vendors/<vendor-id>/template \
    -H "Authorization: Bearer <jwt>"
  ```

  ```bash List vendor IDs with templates theme={null}
  curl https://api.trysnapit.com/api/vendors/template-ids \
    -H "Authorization: Bearer <jwt>"
  ```
</CodeGroup>

## Improve accuracy and cost over time

Every invoice stores the tier and provider that produced its data, so you can audit where your extraction spend goes.

* Watch the **Tier Distribution** chart on the Dashboard. A high share of Tier 2 or Tier 3 invoices means those vendors are missing templates or their templates match too few required fields.
* After improving a template, reprocess a past invoice from that vendor (or re-upload a sample) and confirm it now resolves at Tier 1.
* Anchor regexes to stable labels on the invoice, such as `Invoice No` or `TOTAL`, rather than to values that change between invoices.
* Keep `required: true` limited to fields that appear on every invoice from the vendor. A required field that only appears sometimes drags confidence below the threshold and forces LLM escalation.
