Split
Node.js SDK reference for the Split resource — split a file with several documents into typed segments, wait for the result and crop each segment with docutray/split.
Split
Resource for splitting a file that holds several documents (a scanned bundle, an expense report with its receipts, a photo of several receipts) into typed segments: one per document, with its page range, its document type among your candidates (or otro) and, in beta, its regions on each page. Access via client.split. Split is asynchronous only and does not extract data: crop each segment with cropSegment() and convert it. Available from docutray 0.3.0.
See the split guide for the segment format, limits and billing.
Methods
create(params, options?)
Starts a split by file, url or base64. documentTypeCodeOptions takes 1 to 20 candidate codes, without otro (it is implicit); repeats are removed and the list is checked before any request. documentMetadata is returned as-is in the status and the SPLIT_* webhooks; with file (multipart) its values must be strings, numbers, booleans or null.
import { readFileSync } from 'node:fs';
const split = await client.split.create({
file: readFileSync('bundle.pdf'),
filename: 'bundle.pdf',
documentTypeCodeOptions: ['factura', 'boleta'],
documentMetadata: { batch: '2026-12-01' },
});
const result = await split.wait();
if (result.status === 'SUCCESS') {
for (const segment of result.segments ?? []) {
console.log(segment.index, segment.document_type.code, segment.pages);
}
} else {
console.error(result.error);
}Parameters: SplitParams
Returns: Promise<SplitEnqueued & { wait(options?): Promise<SplitStatus> }>
wait() polls until SUCCESS or FAILED and resolves with that final status on both (check status). It waits up to 30 minutes by default (SPLIT_POLL_TIMEOUT): a 100-page split can take over 25 minutes. A timeout does not cancel the split: resume with client.split.wait(splitId), never with a second create(), which starts and bills another split.
getStatus(splitId, options?)
Retrieves the current status of a split and, on SUCCESS, its segments.
Returns: Promise<SplitStatus>
wait(splitId, pollOptions?, options?)
Polls a split started elsewhere (for example from a SPLIT_COMPLETED webhook handler) until it reaches SUCCESS or FAILED.
const final = await client.split.wait(splitId, { pollInterval: 2000 });Raw Responses
client.split.withRawResponse.create() and .getStatus() return the raw HTTP response:
const raw = await client.split.withRawResponse.getStatus(splitId);
console.log(raw.statusCode);
const status = await raw.parse();Crop each segment
The docutray/split entry point turns a segment into its own file, ready for convert. It needs pdf-lib and pdfjs-dist 5, optional peer dependencies of the SDK:
npm install pdf-lib pdfjs-dist@^5.4import { readFileSync } from 'node:fs';
import { cropSegment } from 'docutray/split';
const file = readFileSync('bundle.pdf');
for (const segment of result.segments ?? []) {
if (segment.document_type.code === 'otro') continue; // nothing to extract
const crop = await cropSegment(file, segment, { filename: 'bundle.pdf' });
if (crop.method === 'whole-file') continue; // a TIFF region that could not be cropped (crop.warning)
const conversion = await client.convert.runAsync({
file: crop.content,
filename: crop.filename, // segment-<index>-<code>.pdf or .png
documentTypeCode: segment.document_type.code,
documentMetadata: { split_id: result.split_id, segment_index: segment.index },
});
console.log(segment.index, (await conversion.wait()).status);
}- Without regions, or when every region is a whole page, a PDF segment becomes a new PDF with its pages, copied without rendering; an image is returned as-is.
- Otherwise each region is cropped with a 2 % margin per side (
marginoption) after rendering PDF pages at 200 dpi (dpioption) or applying an image's EXIF orientation. One region gives a PNG; several pieces give a PDF with one page per piece. - A PDF with only an owner password is rendered (pdf-lib cannot copy it). A TIFF with partial regions comes back whole with
method: 'whole-file'and awarning. expandBox()andsegmentPieces()are exported for your own cropping code.- Without the optional dependencies,
docutray/splitstill imports andcropSegment()rejects with aDocuTrayErrorthat says what to install. pdfjs-dist 4 is rejected at runtime with the version it found.