All projects
FastAPI · LLM vision · Discord · Google Sheets

Invoices and Receipts, Straight Into a Sheet

A FastAPI service for document images: invoices, receipts, IDs, bills of lading. It prepares the image, asks the model for the exact JSON you define and repairs broken output on its own. Discord uploads are saved as a row in Google Sheets; API calls get the JSON back for n8n, Make or a CRM.

  • Invoice to a Sheet row in 6.7 s
  • FastAPI
  • Gemini
  • Discord.py
  • Google Sheets API
  • Docker
How it connects
  1. Discord or any app photo upload or API call
  2. Extraction API FastAPI · your JSON schema
  3. Image prep resize + grayscale
  4. LLM Gemini today
  5. Sheet or your app a Sheets row, or JSON back
Your tools The backend I build Where the data lands
893 Tokens for that invoice on Gemini 2.5 Flash-Lite
Auto-repair Broken JSON from the model is fixed before saving
The case

What Happened

  1. 01

    The test

    A fictional plumbing invoice (3 line items, VAT, a bill-to address) sent through an n8n form to the API with a custom JSON schema.

  2. 02

    The result

    A row in the Google Sheet 6.7 seconds later: invoice number, date, supplier, bill-to, every line item with quantity and amount, subtotal, VAT and the £340.20 total. All correct, 893 tokens. A wrong API key and a link to a private address were both refused, with a ping on Discord.

  3. 03

    What it's for

    The same call works from n8n, Make or a CRM: send the image, get the fields back, write them where the client needs them.

Reliability

Built to Keep Running, Not Just to Demo

The same rules I check in every health check, and how this system handles them.

Errors

If the model returns broken JSON, the service sends it back for a repair pass before anything is saved.

Retries

Rate limits are retried with exponential backoff and random jitter. File downloads retry 3 times.

Alerts

Results that need a human check are marked ⚠ in Discord, with the unclear fields flagged.

Keys

API key on every batch call. Image links that point to private networks are refused.

Logs

JSON logs with a trace ID and the token use of every request.

When Traffic Grows

  • A limit on how many documents are processed at once. A batch runs its documents in parallel up to that limit.
  • Images are resized before the model sees them, which keeps the token cost per document down.
  • The model sits behind one interface. Gemini is wired in today; OpenAI or a local model means one new adapter.

Keys and Client Data

  • Logs keep the image hash and size, never the document itself.
  • Runs as a non-root user in Docker, with a 10 MB request limit.
  • Discord results go to the client's own Google Sheet through a service account that can only use Sheets.