Inkspan › Guides › How to extract text from a PDF in Node.js
The /v1/extract/text endpoint returns the text content of a PDF as JSON, split per page. Each entry carries its page number and the text found on it, which keeps positional context you would lose by concatenating the whole document into one string.
This reads the text layer rather than performing OCR. Born-digital PDFs — anything produced by software, which covers most invoices, statements, and exports — extract cleanly and quickly.
import fs from 'node:fs';
const fd = new FormData();
fd.append('file', new Blob([fs.readFileSync('input.pdf')]), 'input.pdf');
const res = await fetch('https://api.inkspan.dev/v1/extract/text', {
method: 'POST',
headers: { 'X-API-Key': process.env.INKSPAN_KEY },
body: fd,
});
if (!res.ok) throw new Error(await res.text());
console.log(await res.json());
| Name | Type | Required | Description |
|---|---|---|---|
file | file | yes | The PDF to read text from. |
Returns application/json:
{
"page_count": 2,
"pages": [
{
"page": 1,
"text": "Invoice INV-1042\nTotal: $61.00"
},
{
"page": 2,
"text": "Terms and conditions\u2026"
}
]
}
| Status | Meaning | Fix |
|---|---|---|
401 | Invalid or missing API key | Send your key in the X-API-Key header. |
402 | Monthly quota exceeded | The response reports used/quota. Upgrade at inkspan.dev/#pricing. |
422 | Missing or invalid parameter | Check required fields in the table above — the body names the offending field. |
Failed requests do not count against your quota; only successful ones are metered.
No. Extraction reads the embedded text layer; a scan without one returns empty pages. Rasterise with /v1/pdf/to-images and run OCR or a vision model if you need that.
Extract and check whether the text is empty. Empty pages with a non-trivial page count almost always mean a scan.
Text comes back per page in the document's internal order. For tabular data use /v1/extract/tables, which reconstructs row and column structure.
Related Extract guides: extract tables from a PDF