Prerequisites
Before installing Invoice OCR, ensure you have:Required Software
- Node.js: Version 20 or higher
- Package Manager: npm (included with Node.js), yarn, pnpm, or bun
- Git: For cloning the repository
Check your Node.js version with
node --version. Download the latest LTS version from nodejs.org if needed.Required Services
- OpenRouter Account: Sign up at openrouter.ai to get an API key
- API Credits: Fund your OpenRouter account for model usage (Gemini 2.5 Flash costs ~$0.30 per 1M input tokens)
Installation Steps
Install dependencies
Install all required Node.js packages:This installs:
- Next.js 15.5.2 - React framework with App Router
- React 19 - UI library
- Tailwind CSS v4 - Utility-first styling
- shadcn/ui - Accessible component library
- Vitest - Test runner with coverage support
Configure environment variables
Create your local configuration file:Edit
.env.local with your OpenRouter credentials:.env.local
Verify installation
Start the development server:You should see:Visit http://localhost:3000 to confirm the application is running.
Output
Environment Configuration
Required Variables
OPENROUTER_API_KEY
Your OpenRouter API key for authentication.- Get it: openrouter.ai/keys
- Format:
sk-or-v1-...(starts withsk-or-v1-) - Security: Keep this secret, never commit to git
Optional Variables
OPENROUTER_MODEL
Override the default vision model.- Default:
google/gemini-2.0-flash(used when OPENROUTER_MODEL is not set) - Example:
openai/gpt-4o-mini - Options: Any vision-capable model on OpenRouter
| Model | Speed | Accuracy | Cost | Best For |
|---|---|---|---|---|
google/gemini-2.5-flash | Fast | High | Low | General use (recommended) |
openai/gpt-4o-mini | Fast | High | Medium | OpenAI preference |
google/gemini-2.5-pro | Slow | Highest | High | Complex multi-page invoices |
openai/o3-mini | Medium | High | Medium | Reasoning-heavy extraction |
OPENROUTER_PDF_ENGINE
PDF parsing engine for OpenRouter’s file-parser plugin.- Default:
pdf-text - Options:
pdf-text,mistral-ocr,native
- pdf-text: Fast text extraction, best for digital PDFs with embedded text
- mistral-ocr: OCR-based extraction for scanned PDFs or images
- native: Native PDF rendering, slower but more accurate for complex layouts
OPENROUTER_SITE_URL and OPENROUTER_APP_NAME
Optional headers for OpenRouter usage tracking.Project Structure
Understanding the codebase layout:Key Files
app/api/ocr-structured-v4/route.ts: Main OCR endpoint with reconciliation logic (line 1-407)lib/invoice_v4.ts: Reconciliation engine that validates totals and tax calculationscomponents/ocr-uploader.tsx: React component for file upload and extraction UI
Available Scripts
Development
Build
.next/ directory.
Production Server
npm run build first.
Linting
Testing
lib/__tests__/.
Verifying Your Setup
Test the Web Interface
Navigate to the app
Upload a test invoice
Use any invoice image or PDF for testing. If you don’t have one:
- Search “sample invoice India GST” for free examples
- Use a personal receipt or invoice (data is not stored)
Test the API Directly
Verify the API is working:Troubleshooting
Port 3000 Already in Use
Module Not Found Errors
node_modules and reinstall:
OpenRouter API Errors
- Verify your API key is correct in
.env.local - Check you have credits in your OpenRouter account
- Restart the dev server after changing
.env.local
TypeScript Errors
Deployment Considerations
Environment Variables in Production
When deploying to production (Vercel, Netlify, etc.):- Set environment variables in your hosting platform’s dashboard
- Use production-grade API keys (not development keys)
- Set
OPENROUTER_SITE_URLto your production domain - Consider rate limiting and API key rotation
Security Best Practices
- Never expose
OPENROUTER_API_KEYin client-side code - All API routes run server-side in Next.js API routes
- Validate and sanitize all user inputs
- Implement request rate limiting for production
- Monitor OpenRouter usage and set budget alerts
Performance Optimization
- Use
google/gemini-2.5-flashfor best cost/performance ratio - Implement caching for repeated invoice processing
- Consider batching for high-volume scenarios
- Monitor reconciliation error rates and adjust prompts
Next Steps
Quick Start Guide
Process your first invoice and understand the results
API Documentation
Explore API endpoints, schemas, and integration examples
