Node.js Integration
DocuParse API Node.js Integration
Use DocuParse API in Node.js to extract structured JSON from receipts, invoices, and PDFs. No SDK required — use built-in fetch (Node 18+) or node-fetch with FormData to upload files and receive named fields.
Prerequisites
Node.js 18+ (built-in fetch) or Node.js 16+ with node-fetch
A DocuParse API key (sign up for a free account to generate one)
For Node 16: npm install node-fetch form-data
API key security: Store your API key in process.env.DOCUPARSE_API_KEY, not in source code or version control.
Node.js with node-fetch (v16+)
Upload a receipt or invoice using form-data and node-fetch:
import fs from 'fs';
import FormData from 'form-data';
import fetch from 'node-fetch'; // Node 18+ has built-in fetch
const API_KEY = process.env.DOCUPARSE_API_KEY; // store in env, not source
const API_URL = 'https://docuparseapi.com/api/v1/extract';
async function extractDocument(filePath) {
const form = new FormData();
form.append('file', fs.createReadStream(filePath));
const response = await fetch(API_URL, {
method: 'POST',
headers: {
Authorization: `Bearer ${API_KEY}`,
...form.getHeaders(),
},
body: form,
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return response.json();
}
// Usage
const result = await extractDocument('receipt.pdf');
console.log(result.merchant); // "Office Depot"
console.log(result.total); // "45.50"
console.log(result.date); // "2026-04-26"Node.js 18+ (built-in fetch)
Node 18+ has native fetch and Blob. No extra packages needed:
// Node.js 18+ with built-in fetch and File
import { readFile } from 'fs/promises';
async function extractDocument(filePath) {
const fileBuffer = await readFile(filePath);
const fileName = filePath.split('/').pop();
const form = new FormData();
form.append('file', new Blob([fileBuffer]), fileName);
const response = await fetch('https://docuparseapi.com/api/v1/extract', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.DOCUPARSE_API_KEY}` },
body: form,
});
if (!response.ok) {
const error = await response.json().catch(() => ({}));
throw new Error(`API error ${response.status}: ${error.message ?? ''}`);
}
return response.json();
}Response structure
The API returns a JSON object with named fields. Fields not found in the document return null.
{
"success": true,
"document_type": "receipt",
"merchant": "Office Depot",
"date": "2026-04-26",
"total": "45.50",
"subtotal": "42.00",
"tax": "3.50",
"currency": "USD",
"receipt_id": "R-10492",
"payment_method": "Card",
"line_items": [
{ "description": "Notebook", "quantity": 2, "amount": "20.00" }
]
}Error handling
Handle common HTTP errors from the API:
async function extractDocumentSafe(filePath) {
try {
const result = await extractDocument(filePath);
if (!result.success) {
throw new Error('Extraction was not successful');
}
return result;
} catch (error) {
if (error.message.includes('401')) {
throw new Error('Invalid API key — check DOCUPARSE_API_KEY');
}
if (error.message.includes('422')) {
throw new Error('Unsupported file type or empty file');
}
if (error.message.includes('429')) {
throw new Error('Rate limit exceeded — retry after delay');
}
throw error;
}
}401Invalid API key422Bad file format429Rate limit500Server errorStart with Node.js today
Start with 20 documents/month and full API access. No credit card required.