Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ yarn-error.*
*.tsbuildinfo

app-example
.env
53 changes: 53 additions & 0 deletions backend/gemini-extractor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { GoogleGenAI } from "@google/genai";
import fs from "fs";
import dotenv from "dotenv";

dotenv.config();

const receiptExtractorPrompt = `
You are a receipt extractor. You are given a receipt image and you need to extract the following information:
- The name of the item and the price of the item in the receipt
extract all of those in the following format:
{
"item": "item name",
"upc": "item upc code",
"price": "item price"
}
- if there is no price, then the price should be 0
- if there is no item, then the item should be "No item"
- if there is no upc, then the upc should be "null"

IMPORTANT: Return ONLY the raw JSON array without any markdown formatting, code blocks, or additional text.
Do not include \`\`\`json or \`\`\` markers. Just return the pure JSON array.
`

// The client gets the API key from the environment variable `GEMINI_API_KEY`.
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY,
});

const base64ImageFile = fs.readFileSync("./receipt.png", {
encoding: "base64",
});

const contents = [
{
inlineData: {
mimeType: "image/png",
data: base64ImageFile,
}
},
{
text: receiptExtractorPrompt
}
]

async function receiptExtractor() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: contents,
});
console.log(response.text);
}

await receiptExtractor();
33 changes: 33 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import sharp from "sharp";
import Tesseract from "tesseract.js";
import { receiptExtractor } from "./gemini-extractor.js";

dotenv.config();

const app = express();
const port = 3000;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());


app.post("/api/v1/receipts", async (req, res) => {
try {
const file = req.files?.file;
if (!file) return res.status(400).json({ error: "no_file" });

const result = await receiptExtractor(file);
res.json(result);
} catch (e) {
console.error(e);
res.status(500).json({ error: "extraction_failed" });
}
});

app.listen(port, () => {
console.log(`Server is running on port http://localhost:${port}`);
});
Loading