KERNIT Documentation
Node integration
This Node.js example runs the full KERNIT workflow from a local DOCX to a linked DOCX using built-in fetch, FormData, Blob, and a KERNIT API key.
Install and Run#
export KERNIT_API_KEY="sk_kernit_live_YOUR_KEY"
node hyperlink-docx.mjs paper.docxComplete Script#
import { readFile, writeFile } from 'node:fs/promises';
import { basename } from 'node:path';
const apiKey = process.env.KERNIT_API_KEY;
if (!apiKey) throw new Error('KERNIT_API_KEY is required');
const inputPath = process.argv[2] || 'paper.docx';
const settings = {
"refs": true,
"figures": "number",
"tables": "number",
"equations": "number",
"sections": "number",
"doi": true,
"crossref": true,
"refTitleLink": false,
"linkColor": "#0077BD",
"xrefLinkColor": "#0077BD"
};
async function postMultipart(endpoint, fields) {
const form = new FormData();
for (const [name, value] of Object.entries(fields)) {
if (value === undefined || value === null) continue;
if (value instanceof Blob) form.set(name, value, basename(inputPath));
else form.set(name, typeof value === 'string' ? value : JSON.stringify(value));
}
const response = await fetch('https://api.kernit.org' + endpoint, {
method: 'POST',
headers: { Authorization: 'Bearer ' + apiKey },
body: form
});
const text = await response.text();
if (!response.ok) throw new Error(endpoint + ' failed: ' + response.status + ' ' + text);
try { return JSON.parse(text); } catch { return text; }
}
const fileBytes = await readFile(inputPath);
const file = new Blob([fileBytes], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
});
const scan = await postMultipart('/hyperlink-scan', { file, settings });
const resolve = await postMultipart('/hyperlink-crossref', {
file,
settings,
scanFingerprint: scan.scanFingerprint
});
const decisions = (scan.matches || []).map((match) => ({
refId: match.refId,
status: match.confidence >= 0.9 ? 'approved' : 'skipped',
targetId: match.targetId
}));
const applied = await postMultipart('/hyperlink-apply', {
file,
settings,
scanFingerprint: scan.scanFingerprint,
scanToken: scan.scanToken,
resolvedWorks: resolve.resolvedWorks || {},
decisions
});
await writeFile('paper.linked.docx', Buffer.from(applied.docx, 'base64'));
console.log('Wrote paper.linked.docx with', applied.linkCount, 'links');Workflow Template Contract#
This template is maintained with the endpoint examples and the current OpenAPI contract. It is the canonical Node workflow until an official KERNIT SDK package is published.
Was this page useful?Send a lightweight docs feedback event.