What the agent does
This is for a company that sells accounts payable software to trade and construction businesses: electrical contractors, builders, landscaping firms. Their suppliers email PDF invoices, and the office manager saves them into a OneDrive folder until someone keys each one in. The software company saves one agent as a template that names onedrive, which reaches Microsoft Graph files with Files.ReadWrite.All, and makes one agent from it per customer business, storing the agent id against that customer. The customer's office manager connects their own OneDrive through the authorize_url from POST /o/{org}/agents/{agent}/connections {"type": "app", "name": "onedrive"}, opened in a popup inside the AP software.
Each run looks at one folder. The agent lists it by path with Microsoft Graph's GET /drives/{drive-id}/root:/{path-relative-to-root}:/children, on the signed-in user's drive and with the folder's path from its input, following @odata.nextLink past the first page, and keeps PDF files that are not in the list of item ids the software already processed. It downloads each one with GET /me/drive/items/{item-id}/content and reads its text in the sandbox.
From each invoice it extracts what a bill needs: the supplier's name and address, the invoice number and date, the due date or payment terms, the customer's purchase order or job number if the supplier wrote one, each line with its description, quantity, unit price and amount, the subtotal, the tax and the total. Trade suppliers often put a job or site name on the invoice, which is what lets the contractor charge materials to the right job, so the agent copies it exactly as written.
Then it checks the arithmetic the way an AP clerk would: lines times prices against line amounts, lines against the subtotal, subtotal plus tax against the total. An invoice that does not add up, that has no invoice number, or whose pages carry no text (a scan with no text layer) is returned flagged with the reason, not with numbers it guessed. The agent moves, renames and deletes nothing in OneDrive.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "<agent id>",
"input": "List the files in the OneDrive folder 'Accounts/Supplier invoices/Inbox'. Skip any item whose id is in this list: 01ABCDEF2GHIJKL, 01MNOPQR3STUVWX. For each other PDF, download it and extract: supplier name and address, invoice number, invoice date, due date or terms, purchase order or job number as written, line items (description, quantity, unit price, amount), subtotal, tax, total. Check that each line's quantity times unit price equals its amount, that the lines sum to the subtotal, and that subtotal plus tax equals the total. Return only JSON, one object per file with the OneDrive item id and file name; set flagged to true with a reason if the arithmetic does not match, the invoice number is missing, or the PDF has no readable text. Do not move, rename, edit or delete any file."
}- Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
- Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
- Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.
What comes back
The answer is a JSON list, one object per new PDF, keyed by the OneDrive item id so the software can record what it has processed. It arrives in the session's result and in the run.completed webhook's data.result up to 16 KiB; a busy week's batch is read in full from session_url. The software turns each unflagged object into a draft bill in its own system, matched to the supplier and the job, and puts flagged ones in a review queue with the reason.
Nothing is paid on the agent's reading. The office manager approves each draft bill, and checks against the PDF any invoice where the supplier, the job or the total looks unfamiliar, since the same tidy layout comes from honest suppliers and from fake invoices alike. The transcript records every file the agent downloaded and the text it read, so a wrong figure can be traced to the page it came from.
Run it on every new invoices, per customer
The AP software's scheduler sends one POST /o/{org}/sessions per connected customer a few times a day, with the folder path and the ids it has already processed in input. A request_id that is a UUID v5 of the customer's id and the time slot means a retried job returns the run it already started.
A customer whose OneDrive connection lapsed shows needs_reauth on GET /o/{org}/agents/{agent}/connections; the software skips their slot and asks the office manager to reconnect the next time they sign in.
The use case this job follows, with its automation and trigger: Run an agent per customer task.
Why run it on Rush?
Reading supplier invoices is the part of AP that resists plain code: every supplier's layout differs, and the job number can be anywhere on the page. The agent reads each PDF the way a clerk would and shows its arithmetic, so the software can trust the clean ones and route the rest to a person.
Each customer's OneDrive belongs to their own agent, so one contractor's invoices never reach another's run or the software company's own accounts, and the software company stores agent ids instead of Microsoft tokens. Runs for many customers go in parallel, each in its own computer with its own record and cost, and GET /o/{org}/sessions?agent=<agent id> lists one customer's runs with their total_cost_usd.
Frequently asked questions
The connection can write files. Will the agent change anything?
The OneDrive app asks Microsoft for Files.ReadWrite.All, so it could. The prompt forbids moving, renaming, editing or deleting, and the transcript shows every Graph call a run made. Moving processed files into another folder is easy to add to the prompt if a customer wants it.
What about scanned paper invoices?
A PDF whose pages have no text layer is returned flagged as unreadable rather than with guessed numbers. The office manager keys it in or asks the supplier for a digital copy.
Does the customer need a Microsoft developer account?
No. They sign in on Microsoft's own page, which names Rush as the app asking, and Rush stores and refreshes the token for their agent. The software company registers no app with Microsoft either.
Can it match invoices to purchase orders?
It extracts the purchase order or job number exactly as the supplier wrote it. Matching that to an open order happens in the AP software, which holds the orders.
More OneDrive jobs
More jobs like this: Run an agent per customer task
- Find Personal-Training Clients With No Next Session Booked in Each Trainer's Google Calendar (Google Calendar)
- Which Donors Gave Last Year but Not This Year? A Fundraising App's Monthly Airtable Check for Each Nonprofit (Airtable)
- Where Are the Gaps in My Book Next Week? A Salon App Finds Each Stylist's Open Slots From Their Google Calendar (Google Calendar)