cookbook · Extractor · Expenses and scanned documents
Turn a receipt photo into an expense entry
Expense apps ask people to photograph a receipt and then type it in anyway. This recipe sends the photo itself to /v1/extract, with no separate OCR step, and gets back the items, quantities, prices, total, how it was paid and the change. The shop name is blurred in this photo, so merchant comes back null rather than guessed. Up to six images can go in one request, for multi-page documents.

Request
POST /v1/extract, documented in the Extractor reference.
curl https://meragpt.com/v1/extract \
-H "Authorization: Bearer $MERAGPT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "extractor-1",
"images": [
"https://meragpt.com/blog/extractor-1/receipt-photo.jpg"
],
"schema": {
"type": "object",
"properties": {
"merchant": {
"type": "string"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"price": {
"type": "string",
"description": "Line price as printed."
}
}
}
},
"total": {
"type": "string",
"description": "Total paid, as printed."
},
"paid_by": {
"type": "string",
"enum": [
"cash",
"card",
"other"
]
},
"change": {
"type": "string",
"description": "Change given, as printed."
}
}
}
}'What came back
The real output from running this recipe, unedited.
{
"merchant": null,
"items": [
{
"name": "[MD] SOFT MORNING ROLL",
"quantity": 1,
"price": "18,000"
},
{
"name": "REAL HONEY PANCAKE BREAD",
"quantity": 2,
"price": "20,000"
},
{
"name": "CUSTARD BRIOCHE",
"quantity": 4,
"price": "36,000"
}
],
"total": "74,000",
"paid_by": "cash",
"change": "26,000"
}Python
import os, requests
API = "https://meragpt.com/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['MERAGPT_API_KEY']}"}
import base64
# a URL works, or send the photo itself as a data URL
photo = base64.b64encode(open("receipt.jpg", "rb").read()).decode()
schema = {
"type": "object",
"properties": {
"merchant": {
"type": "string"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"price": {
"type": "string",
"description": "Line price as printed."
}
}
}
},
"total": {
"type": "string",
"description": "Total paid, as printed."
},
"paid_by": {
"type": "string",
"enum": [
"cash",
"card",
"other"
]
},
"change": {
"type": "string",
"description": "Change given, as printed."
}
}
}
r = requests.post(f"{API}/extract", headers=HEADERS, json={
"model": "extractor-1",
"images": [f"data:image/jpeg;base64,{photo}"],
"schema": schema,
}).json()
expense = r["data"]
to_number = lambda s: float(s.replace(",", "")) if s else None # this locale writes 74,000
print(expense["merchant"] or "merchant unreadable: ask the user", to_number(expense["total"]))
TypeScript
const API = "https://meragpt.com/v1";
const headers = {
Authorization: `Bearer ${process.env.MERAGPT_API_KEY}`,
"Content-Type": "application/json",
};
import { readFileSync } from "node:fs";
const photo = readFileSync("receipt.jpg").toString("base64");
const schema = {
"type": "object",
"properties": {
"merchant": {
"type": "string"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"price": {
"type": "string",
"description": "Line price as printed."
}
}
}
},
"total": {
"type": "string",
"description": "Total paid, as printed."
},
"paid_by": {
"type": "string",
"enum": [
"cash",
"card",
"other"
]
},
"change": {
"type": "string",
"description": "Change given, as printed."
}
}
};
const r = await fetch(`${API}/extract`, {
method: "POST",
headers,
body: JSON.stringify({ model: "extractor-1", images: [`data:image/jpeg;base64,${photo}`], schema }),
}).then((res) => res.json());
console.log(r.data.merchant ?? "merchant unreadable", r.data.total);
When to trust it
Every item, quantity and price was read correctly, and so were the total, the payment method and the change; the null merchant is right, since the name cannot be read. Prices are kept as printed ('18,000'), because receipts mix separators and currencies; convert them in code once you know the locale. Photos run on the GPU; in the free playground they take longer.
Try it without code in the playground, or see more Extractor recipes: read an invoice into your ap system, pull the key terms out of a contract, turn a support email into a ticket, get action items out of meeting notes, extract memories from a conversation.