Complete API reference for receipt image processing
POST /api/receipt/processUpload a receipt image and receive structured JSON data with merchant information, line items, totals, taxes, and payment details. The API automatically validates that the uploaded image contains a valid receipt.
⭐ Enhanced with Gemini 3.0 Flash AI
When GEMINI_API_KEY is configured, the API uses advanced AI reasoning to determine if an image is actually a receipt, providing significantly better accuracy than pattern matching alone. The AI understands context and is lenient with OCR errors.
The AI validation recognizes receipts from:
💡 Tip: For best results, always configure GEMINI_API_KEY. The AI validation is dramatically more accurate and handles edge cases much better than pattern matching. See RECEIPT_OCR_SETUP.md for setup instructions.
multipart/form-data| Field | Type | Description |
|---|---|---|
| image | File | Receipt image file |
Maximum: 10 MB
// JavaScript/TypeScript Example
const formData = new FormData();
formData.append('image', fileInput.files[0]);
const response = await fetch('/api/receipt/process', {
method: 'POST',
body: formData,
});
const receipt = await response.json();# cURL Example curl -X POST https://your-domain.com/api/receipt/process \ -F "image=@receipt.jpg"
Returns a ParsedReceipt object with the following structure:
{
// Merchant Information
"merchantName": "Fresh Market Grocery",
"merchantAddress": "123 Main Street, Springfield, IL 62701",
"merchantPhone": "(555) 123-4567",
"merchantTaxId": "12-3456789",
// Transaction Details
"transactionId": "TXN-1234567890-ABCDEF123",
"date": "2024-01-15",
"timestamp": "2024-01-15T14:30:00.000Z",
// Line Items
"items": [
{
"name": "Organic Bananas",
"quantity": 1.5,
"unitPrice": 0.69,
"totalPrice": 1.04,
"category": "Produce",
"sku": "PROD-001",
"discounted": false
},
{
"name": "Whole Milk (1 gal)",
"quantity": 1,
"unitPrice": 4.99,
"totalPrice": 4.99,
"category": "Dairy"
}
],
// Financial Summary
"subtotal": 23.45,
"discount": 2.35,
"taxes": [
{
"name": "Sales Tax",
"rate": 8.25,
"amount": 1.74
}
],
"tip": 0,
"total": 22.84,
// Payment Information
"payments": [
{
"method": "Credit Card",
"cardLast4": "4532",
"amount": 22.84
}
],
"amountTendered": null,
"changeGiven": null,
// Additional Details
"currency": "USD",
"cashierName": "John D.",
"registerNumber": "REG-1",
// Processing Metadata
"metadata": {
"processedAt": "2024-01-15T14:35:00.000Z",
"confidence": 0.92,
"imageFormat": "JPEG",
"imageSize": 2457600,
"isMockData": false
}
}Invalid request (missing file, wrong format, file too large)
{
"error": "Invalid file type",
"status": 400,
"details": "Supported formats: JPEG, PNG, WebP, HEIC. Received: image/gif"
}Image validation failed - not recognized as a valid receipt
{
"error": "Invalid receipt image",
"status": 400,
"details": "Not a valid receipt: insufficient receipt indicators found"
}Server error during processing
{
"error": "Failed to process receipt image",
"status": 500,
"details": "OCR service unavailable"
}Import the types from the API module for full type safety:
import type {
ParsedReceipt,
ReceiptItem,
ReceiptTax,
ReceiptPayment,
ReceiptProcessError
} from '@/types/receipt/types'
// Use in your code
const processReceipt = async (file: File): Promise<ParsedReceipt> => {
const formData = new FormData()
formData.append('image', file)
const response = await fetch('/api/receipt/process', {
method: 'POST',
body: formData,
})
if (!response.ok) {
const error: ReceiptProcessError = await response.json()
throw new Error(error.error)
}
return await response.json()
}In production, implement rate limiting to prevent abuse (e.g., 100 requests per hour per IP).
Add API key authentication or JWT tokens to secure the endpoint for production use.
Currently using Tesseract.js for real OCR processing. For even better accuracy:
For production-grade accuracy, consider adding LLM post-processing:
Example: Send OCR output to LLM with prompt: "Fix errors in this receipt text and structure as JSON. Validate merchant name against known businesses."
Consider storing processed receipts in a database for auditing and user history.
For large volumes, use a queue system (e.g., Bull, AWS SQS) to process receipts asynchronously.