> For the complete documentation index, see [llms.txt](https://typless.gitbook.io/typlessapi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://typless.gitbook.io/typlessapi/automation-use-cases/invoice-with-line-items.md).

# Invoice with line items

### Overview

This guide covers how to extract metadata and line items from multiple supplier invoices with examples in **Python** and **Node**. For this example, we will use the **Pretrained model**.

You will extract the following metadata fields:

* Name of the supplier
* Name of the receiver
* Invoice number
* Purchase order number
* Issue date
* Pay due date
* Total amount

You will extract the following line item fields:

* Product number
* Product description
* Quantity
* Price

**This guide shows you how to**

1. [Create invoice-line-items document type](#id-1.-create-a-new-document-type)
2. [Add multiple suppliers](#id-2.-add-suppliers)
3. [Execute training](#id-3.-execute-training)
4. [Extract data from documents](#id-4.-extract-data-from-documents)
5. [Continuously improve models after extraction](#id-5.-continuously-improve-models)

## Getting your API Key

The *Authorization* header for your API key is: `Token YOUR-API-KEY` ([Login](https://app.typless.com/login/?redirect=https://docs.typless.com/) if you do not see one).\
You can also obtain the **API key** by visiting the [Settings page](https://app.typless.com/settings/profile).

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FePXqOCeLb4FOuZhmYG8X%2Fuploads%2FXwpyvlMgoIW7PXZKkPwE%2FREC-20260515112641.mp4?alt=media&token=810fbc33-d3d7-4687-b49c-62abae6c7029>" %}
Getting your API key
{% endembed %}

## 1. Create a new document type

Before you start extracting data, you need to define a document type. Navigate to the [Dashboard page](https://app.typless.com) and click on the **New document type** button in the top right corner of the table. Next, select the **Pretrained model**, followed by **Use a Predefined Template**. Next, click on **Line item invoice** card. The wizard will already pre-fill all the needed [extraction fields](/typlessapi/typless/extraction-fields.md) along with the [document type configuration](/typlessapi/typless/document-type.md).\
Click on the **Create document type**.

This will create a new **document type** with name **line-item-invoice** with the following fields:

* **`supplier_name`**
* **`supplier_vat_id`**
* **`buyer_name`**
* **`buyer_vat_id`**
* **`invoice_number`**
* **`payment_reference`**
* **`invoice_date`**
* **`payment_due_date`**
* **`service_date`**
* **`total_amount`**
* **`net_amount`**
* **`vat_amount`**
* **`IBAN`**
* **`payment_purpose_code`**
* **`purchasing_order`**
* **`currency`**

and line items fields:

* **`item_code`**
* **`item_description`**
* **`unit`**
* **`net_amount`**
* **`vat_amount`**
* **`gross_amount`**
* **`discount_percentage`**
* **`discount`**
* **`unit_price`**
* **`quantity`**
* **`vat_rate`**

## 2. Extract data from documents

{% hint style="success" %}
**👍&#x20;**<mark style="color:green;">**For the Pretrained model, we do not need to train the suppliers - we can start extracting data right away.**</mark>
{% endhint %}

&#x20;Here are two examples of invoices that you can use for data extraction:

* [Amazing Company 2 - download](https://typless-public.s3-eu-west-1.amazonaws.com/use_cases/line-item-invoice/line_items_supplier_1_example_2.pdf)
* [Good Services 2 - download](https://typless-public.s3-eu-west-1.amazonaws.com/use_cases/line-item-invoice/line_items_supplier_2_example_2.pdf)

Download them and extract the data using the code:

<details>

<summary><strong>1 Open file as base64 string</strong> <em><mark style="color:green;">(Lines 6-7)</mark></em></summary>

Make sure that you are pointing to the correct path.

</details>

<details>

<summary><strong>2 Specify payload</strong> <em><mark style="color:green;">(Lines 8-12)</mark></em></summary>

</details>

<details>

<summary><strong>3 Specify headers</strong> <em><mark style="color:green;">(Lines 16-20)</mark></em></summary>

</details>

<details>

<summary><strong>4 Make POST request</strong> <em><mark style="color:green;">(Lines 22-24)</mark></em></summary>

</details>

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
import requests
import base64

file_name = 'amazing_company_2.pdf'
with open(file_name, 'rb') as file:
    base64_data = base64.b64encode(file.read()).decode('utf-8')

payload = {
    "file": base64_data,
    "file_name": file_name,
    "document_type_name": "line-item-invoice"
}

url = "https://developers.typless.com/api/extract-data"

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": "<<apikey>>"
}

response = requests.request("POST", url, json=payload, headers=headers)

for field in response.json()['extracted_fields']:
    print(f'{field["name"]}: {field["values"][0]["value"]}')
```

{% endcode %}
{% endtab %}

{% tab title="Node" %}
{% code lineNumbers="true" %}

```javascript
const fetch = require('node-fetch');
const fs = require('fs');

const fileName = 'amazing_company_2.pdf';
const base64File = fs.readFileSync(fileName, {encoding: 'base64'});

const url = 'https://developers.typless.com/api/extract-data';

const payload = {
  file: base64File,
  file_name: fileName,
  document_type_name: "line-item-invoice"
}

const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
  'Authorization': '<<apikey>>'
}

let options = {
  method: 'POST',
  headers: headers,
  body: JSON.stringify(payload)
};

fetch(url, options)
  .then(res => res.json())
  .then(json => {
      json.extracted_fields.forEach(field => console.log(`${field.name}: ${field.values[0].value}`))
      json.line_items.forEach(
        line_item => {
          console.log('Line item')
          line_item.forEach(field => console.log(`${field.name}: ${field.values[0].value}`))
        }
      )
    }
  )
  .catch(err => console.error('error:' + err));
```

{% endcode %}
{% endtab %}
{% endtabs %}

**Response:**

{% tabs %}
{% tab title="JSON" %}
{% code lineNumbers="true" %}

```json
// Example extraction response - the provided recipe will not produce equal results
{
    "file_name": "invoice_2.pdf",
    "object_id": "1cb25cc8-c9fa-4149-9a83-b4ed6a2173b9",
    "extracted_fields": [
        {
            "name": "supplier",
            "values": [
                {
                    "x": -1,
                    "y": -1,
                    "width": -1,
                    "height": -1,
                    "value": "ScaleGrid",
                    "confidence_score": "0.968",
                    "page_number": -1
                }
            ],
            "data_type": "AUTHOR"
        },
        {
            "name": "invoice_number",
            "values": [
                {
                    "x": 1989,
                    "y": 545,
                    "width": 323,
                    "height": 54,
                    "value": "20190500005890",
                    "confidence_score": "0.250",
                    "page_number": 0
                },
                {
                    "x": 167,
                    "y": 574,
                    "width": 391,
                    "height": 54,
                    "value": "GB123456789",
                    "confidence_score": "0.250",
                    "page_number": 0
                }
            ],
            "data_type": "STRING"
        },
        {
            "name": "issue_date",
            "values": [
                {
                    "x": 2072,
                    "y": 628,
                    "width": 240,
                    "height": 54,
                    "value": "2019-06-05",
                    "confidence_score": "0.358",
                    "page_number": 0
                }
            ],
            "data_type": "DATE"
        },
        {
            "name": "total_amount",
            "values": [
                {
                    "x": 2146,
                    "y": 1196,
                    "width": 126,
                    "height": 54,
                    "value": "47.5300",
                    "confidence_score": "0.990",
                    "page_number": 0
                }
            ],
            "data_type": "NUMBER"
        }
    ],
    "line_items": [
        [
            {
                "name": "Description",
                "values": [
                    {
                        "x": 208,
                        "y": 1196,
                        "width": 1022,
                        "height": 50,
                        "value": "5/2019-MongoBackend-MgmtStandalone-Small-744 hours",
                        "confidence_score": "0.661",
                        "page_number": 0
                    }
                ],
                "data_type": "STRING"
            },
            {
                "name": "Price",
                "values": [
                    {
                        "x": 2146,
                        "y": 1196,
                        "width": 126,
                        "height": 54,
                        "value": "47.5300",
                        "confidence_score": "0.582",
                        "page_number": 0
                    }
                ],
                "data_type": "NUMBER"
            },
            {
                "name": "Quantity",
                "values": [
                    {
                        "x": 1979,
                        "y": 1196,
                        "width": 23,
                        "height": 54,
                        "value": "1",
                        "confidence_score": "0.647",
                        "page_number": 0
                    }
                ],
                "data_type": "NUMBER"
            }
        ]
    ],
    "customer": null
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Need a more in-depth explanation of the response?**\
You can read about it [**here**](/typlessapi/typless/data-extraction.md#understanding-response).
{% endhint %}

## Running Typless live

The only thing that you need to do to automate your manual data entry is to integrate those simple API calls into your system.

{% hint style="success" %}
**Have any questions or need some help?** Contact us via chat or email **<support@typless.com>**
{% endhint %}

![Typless usage is very easy and straightforward!](https://files.readme.io/ad56c3d-typless_1.PNG)
