JavaScript Sample — Context API
Full workflow: authenticate → get presigned URL → upload file → submit enrichment → poll results.
Download the JavaScript Script (.js)Setup
const https = require("https");
const fs = require("fs");
const url = require("url");
const CONTEXT_API_URL = "https://knowledge-enrichment.ai.app.hyland.com/latest/api/context-enrichment"; // Production URL; replace with your environment URL
const OAUTH_URL = "https://auth.app.hyland.com/idp";
const CLIENT_ID = "YOUR_CLIENT_ID";
const CLIENT_SECRET = "YOUR_CLIENT_SECRET";
function request(method, targetUrl, headers, body) {
return new Promise((resolve, reject) => {
const parsed = new url.URL(targetUrl);
const options = { method, hostname: parsed.hostname, path: parsed.pathname + parsed.search, headers };
const req = https.request(options, (res) => {
const chunks = [];
res.on("data", (c) => chunks.push(c));
res.on("end", () => resolve(Buffer.concat(chunks).toString()));
});
req.on("error", reject);
if (body) req.write(body);
req.end();
});
}
Workflow
1. Get access token
async function getAccessToken() {
const credentials = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString("base64");
const body = new url.URLSearchParams({
grant_type: "client_credentials",
scope: "environment_authorization"
}).toString();
const response = await request("POST", `${OAUTH_URL}/connect/token`, {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${credentials}`
}, body);
return JSON.parse(response).access_token;
}
const token = await getAccessToken();
2. Get presigned upload URL
async function getPresignedUrl(token, contentType) {
const encoded = encodeURIComponent(contentType);
const response = await request("GET",
`${CONTEXT_API_URL}/files/upload/presigned-url?contentType=${encoded}`,
{ Authorization: `Bearer ${token}` }
);
return JSON.parse(response);
}
const { presignedUrl, objectKey } = await getPresignedUrl(token, "application/pdf");
console.log("objectKey:", objectKey);
3. Upload file
async function uploadFile(presignedUrl, filePath, contentType) {
const data = fs.readFileSync(filePath);
await request("PUT", presignedUrl, { "Content-Type": contentType }, data);
console.log("Upload complete");
}
await uploadFile(presignedUrl, "input/document.pdf", "application/pdf");
4. Submit enrichment job
async function processContent(token, objectKey, actions) {
const body = JSON.stringify({
version: "context.api/v2",
objectKeys: [{ path: objectKey }],
actions
});
const response = await request("POST", `${CONTEXT_API_URL}/content/process`, {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json"
}, body);
return JSON.parse(response).processingId;
}
const processingId = await processContent(token, objectKey, {
textSummarization: { maxWordCount: 150 },
textClassification: { classes: ["Report", "Contract", "Invoice", "Other"] }
});
console.log("Processing ID:", processingId);
5. Poll results
async function pollResults(token, processingId, intervalMs = 3000, timeoutMs = 300000) {
let elapsed = 0;
while (elapsed < timeoutMs) {
const response = await request("GET",
`${CONTEXT_API_URL}/content/process/${processingId}/results`,
{ Authorization: `Bearer ${token}` }
);
const data = JSON.parse(response);
console.log("Status:", data.status);
if (!data.inProgress) return data;
await new Promise((r) => setTimeout(r, intervalMs));
elapsed += intervalMs;
}
throw new Error(`Job ${processingId} timed out`);
}
const results = await pollResults(token, processingId);
console.log(JSON.stringify(results, null, 2));