## Invoice Processing Tutorial

This tutorial walks through building an agent that processes PDF invoices, extracts structured data, and sends the results to Slack. The agent takes an uploaded invoice PDF, extracts key information like vendor details and line items, and posts a formatted summary to a Slack channel.

## Interface

## Blueprint

## Slack message

This pattern is useful for automating accounts payable workflows, expense processing, and any scenario where you need to extract structured data from documents and route it to business systems. Once you complete this tutorial, you can continue enhancing the agent by adding [tool calling](https://dev.writer.com/agent-builder/tool-calling-tutorial) with behaviors like validating invoices, flagging potential issues, and comparing invoices against historical data.

## Prerequisites

Before building this agent, you need to set up a Slack webhook to send the invoice data to a Slack channel. Follow the instructions in the [Slack documentation](https://api.slack.com/messaging/webhooks) to set up a webhook. The general steps are:

1. Go to [Slack API](https://api.slack.com/apps) and create a new app
2. Select “From scratch” and provide a name for your app
3. Choose the Slack workspace where you want to build the agent
4. Navigate to “Incoming Webhooks” in the left sidebar of the new app and activate webhooks
5. Click “Add New Webhook to Workspace” and select the channel where invoices should be posted. You might need administrator permissions to add a webhook to a channel, depending on your Slack workspace settings.
6. Copy the webhook URL - you’ll need this for the HTTP request block. The webhook URL will look like: `https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX`

## Agent Overview

This agent processes invoices through the following steps:

1. User uploads a PDF invoice file  
2. File is uploaded to Writer Cloud storage  
3. PDF content is extracted and parsed  
4. AI extracts structured invoice data such as vendor, amounts, line items.  
5. Formatted invoice summary is sent to Slack

## Build the UI

The agent’s UI contains a file input for uploading invoices and a submit button.

1. **Add a file input**  
   Drag a **File Input** block to the canvas. In the block’s configuration menu, update the following:  
   - **Label**: `Invoice`  
   - **Allowed file types**: `.pdf`  
   - **Link variable** under **Binding**: `invoice`  
   This allows users to upload PDF files and stores the file in the `invoice` state variable.

2. **Add a button to submit the request**  
   Drag a **Button** block to the canvas. In the block’s configuration menu, update the following:  
   - **Label**: `Submit`

## Build the Blueprint

The logic of the blueprint is as follows:

1. The **UI Trigger** block starts the agent when the user clicks the submit button
2. The **Add files to Writer Cloud** block uploads the invoice file to cloud storage
3. The **Parse PDF** block extracts text content from the uploaded file
4. The **Structured Output** block processes the PDF text and extracts invoice data as JSON
5. The **HTTP Request** block sends the structured data to Slack

The finished blueprint contains the following blocks:

1. **Add a UI Trigger block**  
   Drag a **UI Trigger** block to the canvas. In the block’s configuration menu, update the following:  
   - **Component Id**: select the **Submit** button from the dropdown  
   - **Trigger**: `wf-click`  
   This triggers the blueprint when the user clicks the Process Invoice button.

2. **Add an Add files to Writer Cloud block**  
   Drag an **Add files to Writer Cloud** block to the canvas. In the block’s configuration menu, update the following:  
   - **Files**: `@{invoice}`  
   This uploads the invoice file from the `invoice` state variable to Writer Cloud storage.

3. **Add a Parse PDF block**  
   Drag a **Parse PDF tool** block to the canvas. In the block’s configuration menu, update the following:  
   - **File**: `@{result.0.id}`  
   This extracts the text content from the uploaded PDF file using the file information returned from the Add files to Writer Cloud block.

4. **Add a Structured Output block**  
   Drag a **Structured Output** block to the canvas. In the block’s configuration menu, update the following:  
   - **Prompt**:
   
   ```
   Extract the following information from this invoice:
   
   - Invoice number
   - Vendor name and address
   - Invoice date and due date
   - Bill-to company and address
   - Total amount, subtotal, and tax amount
   - Currency
   - All line items with descriptions, quantities, unit prices, and totals
   - Payment terms
   - Purchase order number (if present)
   
   If any information is not clearly stated in the invoice, use null for that field.
   ```
   - **Input**: `@{result}`  
   - **Model**: `Palmyra X5`

5. **Add an HTTP Request block to send to Slack**  
   Drag an **HTTP Request** block to the canvas. In the block’s configuration menu, update the following:  
   - **Method**: `POST`
   - **URL**: Your Slack webhook URL
   - **Body**:
   
   ```
   {
     "text": "New Invoice Processed",
     "blocks": [
       {
         "type": "header",
         "text": {
           "type": "plain_text",
           "text": "📄 Invoice Processed"
         }
       },
       {
         "type": "section",
         "fields": [
           {
             "type": "mrkdwn",
             "text": "*Invoice #:* @{result.invoiceNumber}"
           },
           {
             "type": "mrkdwn",
             "text": "*Vendor:* @{result.vendorName}"
           },
           {
             "type": "mrkdwn",
             "text": "*Date:* @{result.invoiceDate}"
           },
           {
             "type": "mrkdwn",
             "text": "*Due Date:* @{result.dueDate}"
           },
           {
             "type": "mrkdwn",
             "text": "*Amount:* @{result.currency} @{result.totalAmount}"
           },
           {
             "type": "mrkdwn",
             "text": "*Bill To:* @{result.billToCompany}"
           }
         ]
       }
     ]
   }
   ```
   This sends a formatted message to Slack with the key invoice details. The message uses Slack’s block kit format to create a structured, readable invoice summary.

## Preview the Agent

Navigate to the **Preview** tab to test the agent. Upload a PDF invoice and click the Process Invoice button. The agent will upload the file, extract the invoice data, and send a summary to your configured Slack channel. You can see the agent’s progress in the **Logs** tab, including the extracted JSON data and the Slack API response. You should see a message in the Slack channel with the invoice data.

## Add Tool Calling for Invoice Validation

You can enhance this agent by adding tool calling to validate invoices and flag potential issues. Here are some ways to extend the functionality:

- Check for missing required fields like tax ID or payment terms
- Compare against previous invoices from the same vendor to detect price discrepancies
- Validate amounts against purchase orders and company spending policies
- Flag duplicate invoice numbers or unusual payment terms
- Verify vendor details against your approved vendor list

To add these capabilities:

1. Add a tool calling block after the structured output to analyze the extracted data  
2. Configure validation rules and checks in your custom Python code  
3. Update the Slack message to include any validation warnings or approvals  
4. Optionally trigger different workflows based on the validation results

Check out the [tool calling tutorial](https://dev.writer.com/agent-builder/tool-calling-tutorial) to learn how to add these validation capabilities to your invoice processing agent.
