KERNIT Documentation
Python integration
This Python example runs the full KERNIT workflow with requests: scan the file, resolve DOI evidence, build review decisions, apply links, and write the returned DOCX.
Install and Run#
python -m pip install requests
export KERNIT_API_KEY="sk_kernit_live_YOUR_KEY"
python hyperlink_docx.py paper.docxComplete Script#
import base64
import json
import os
import sys
import requests
api_key = os.environ["KERNIT_API_KEY"]
input_path = sys.argv[1] if len(sys.argv) > 1 else "paper.docx"
settings = json.loads("{\n \"refs\": true,\n \"figures\": \"number\",\n \"tables\": \"number\",\n \"equations\": \"number\",\n \"sections\": \"number\",\n \"doi\": true,\n \"crossref\": true,\n \"refTitleLink\": false,\n \"linkColor\": \"#0077BD\",\n \"xrefLinkColor\": \"#0077BD\"\n}")
def post_multipart(endpoint, data):
with open(input_path, "rb") as docx:
files = {"file": (input_path, docx, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")}
response = requests.post(
"https://api.kernit.org" + endpoint,
headers={"Authorization": f"Bearer {api_key}"},
data={key: value if isinstance(value, str) else json.dumps(value) for key, value in data.items()},
files=files,
timeout=480,
)
if response.status_code >= 400:
raise RuntimeError(f"{endpoint} failed: {response.status_code} {response.text}")
return response.json()
scan = post_multipart("/hyperlink-scan", {"settings": settings})
resolved = post_multipart("/hyperlink-crossref", {
"settings": settings,
"scanFingerprint": scan["scanFingerprint"],
})
decisions = [
{
"refId": match["refId"],
"status": "approved" if match.get("confidence", 0) >= 0.9 else "skipped",
"targetId": match.get("targetId"),
}
for match in scan.get("matches", [])
]
applied = post_multipart("/hyperlink-apply", {
"settings": settings,
"scanFingerprint": scan["scanFingerprint"],
"scanToken": scan["scanToken"],
"resolvedWorks": resolved.get("resolvedWorks", {}),
"decisions": decisions,
})
with open("paper.linked.docx", "wb") as output:
output.write(base64.b64decode(applied["docx"]))
print(f"Wrote paper.linked.docx with {applied.get('linkCount', 0)} links")Workflow Template Contract#
This template is maintained with the endpoint examples and the current OpenAPI contract. It is the canonical Python workflow until an official KERNIT SDK package is published.
Was this page useful?Send a lightweight docs feedback event.