Ever wondered how you can build an AI receipt reader in under 10 minutes? In this tutorial, we'll explore how to extract structured data from PDF receipts using the Docuglean SDK. I'll guide you step-by-step through the entire setup process - it's surprisingly easy and only requires basic knowledge!
๐ Visit and star Docuglean SDK
1- Prerequisites:
In order to be able to use the Docuglean AI SDK you need a little bit of JavaScript knowledge, you can review some JavaScript here , other than that youโll need Node.js and NPM installed in your machine, to check if you have them already, in Windows, click the Windows key + R on your keyboard, and type cmd, then type the commands:
node -vnpm -v
If you are able to see the versions of node and npm, good! Youโre ready!, instead, you can simply go here and install the node.js windows installer (msi), after the setup is complete, check the versions again in cmd, and you will see that you are ready!
2- Installing the Docuglean SDK
Let's create your receipt reader project:
Create a project directory:
mkdir my-receipt-extractor cd my-receipt-extractor
Install Docuglean:
npm i docuglean
๐ Perfect! Your directory now has all the Docuglean AI features ready to use!
Getting Your API Key
You'll need an API key from one of these supported providers:
OpenAI: gpt-4.1-mini
, gpt-4.1
, gpt-4o-mini
, gpt-4o
, o1-mini
, o1
, o3
, o4-mini
Mistral: mistral-ocr-latest
โ ๏ธ Important: Keep your API key secure and never share it publicly!
More providers coming soon! Check the Docuglean repository for updates.
3- Creating a Zod Schema for receipts
The Zod schema acts as your blueprint - it tells Docuglean AI exactly what structure and data types to extract from receipts, ensuring consistent, predictable results every time.
Why schemas matter:
Predictable Output: Guarantees data comes back in your expected format
Type Safety: Ensures fields are the correct type (dates as strings, totals as numbers, etc.)
AI Guidance: Helps the AI understand exactly what information to extract
Here's a comprehensive Zod schema for receipts:
import { z } from 'zod';
// Define the structure for individual receipt items
const ReceiptItemSchema = z.object({
name: z.string().describe('The name of the item purchased.'),
price: z.number().describe('The price of this specific item.')
});
// Define the complete receipt structure
const ReceiptSchema = z.object({
date: z.string().describe('The date of the receipt in YYYY-MM-DD format.'),
total: z.number().describe('The grand total amount shown on the receipt.'),
currency: z.string().optional().describe('The currency symbol or code (e.g., "$", "EUR").'),
vendorName: z.string().optional().describe('The name of the store or business.'),
items: z.array(ReceiptItemSchema).describe('A list of all individual items purchased, with their names and prices.')
});
4- Writing the extraction script
This script is the heart of your application - it combines your receipt, API key, and Zod schema to tell Docuglean AI exactly what to do.
import { extract } from 'docuglean';
import { z } from 'zod';
import * as dotenv from 'dotenv';
dotenv.config(); // Load environment variables securely
// Import your Zod schema (from section 3)
const ReceiptItemSchema = z.object({
name: z.string().describe('The name of the item purchased.'),
price: z.number().describe('The price of this specific item.')
});
const ReceiptSchema = z.object({
date: z.string().describe('The date of the receipt in YYYY-MM-DD format.'),
total: z.number().describe('The grand total amount shown on the receipt.'),
currency: z.string().optional().describe('The currency symbol or code (e.g., "$", "EUR").'),
vendorName: z.string().optional().describe('The name of the store or business.'),
items: z.array(ReceiptItemSchema).describe('A list of all individual items purchased, with their names and prices.')
});
async function runReceiptExtraction() {
const apiKey = process.env.OPENAI_API_KEY; // Set this in your .env file
const receiptFilePath = './receipt_example.pdf'; // Ensure this file exists
if (!apiKey) {
console.error("โ Error: API key not found. Please set OPENAI_API_KEY in your .env file.");
return;
}
try {
console.log("๐ Starting receipt data extraction...");
const extractedData = await extract({
filePath: receiptFilePath,
apiKey: apiKey,
provider: 'openai', // or 'mistral'
responseFormat: ReceiptSchema,
prompt: 'Extract the date, total, currency, vendor name, and a list of items with their names and prices from this receipt.'
});
console.log("โ
Extraction successful!");
console.log("๐ Extracted Data:");
console.log(JSON.stringify(extractedData, null, 2));
} catch (error) {
console.error("โ An error occurred during extraction:", error);
}
}
runReceiptExtraction();
5- Understanding the output
Once your script runs successfully, Docuglean returns a JavaScript object containing all extracted information, perfectly matching your Zod schema structure.
Example Output:
{
"date": "2024-07-09",
"total": 55.75,
"currency": "USD",
"vendorName": "SuperMart",
"items": [
{
"name": "Organic Bananas",
"price": 3.49
},
{
"name": "Milk (1 Gallon)",
"price": 4.99
},
{
"name": "Avocado (Each)",
"price": 2.50
}
]
}
6- Let's wrap up!
Congratulations! You've successfully built an AI-powered receipt reader that automates tedious data entry and opens up new possibilities for document processing.
What's coming soon to Docuglean:
๐ฎ summarize() - Get quick TLDRs of long documents
๐ translate() - Built-in multilingual document support
๐ท๏ธ classify() - Automatically detect document types (receipt, invoice, ID, etc.)
๐ search(query) - AI-powered document search
๐ค More AI Models - Integrations with Meta's Llama, Together AI, and OpenRouter
Keep up with the latest updates by starring the Docuglean GitHub repository!
โญ Want More?
Check out the full Docuglean repository on GitHub and star the project to support future updates!
๐ Visit and star Docuglean SDK
Have questions/requests? Drop them the comments! ๐๏ธ