API
Integrate Kameleon document generation and template metadata into your own applications using the Kameleon REST API.
Authentication setup
To call the Kameleon API, register an application in Microsoft Entra ID and authorize it in Kameleon Hub:
-
Register a new application in Microsoft Entra ID (see Register an application with Microsoft identity platform) and copy its Application (client) ID.
-
In Kameleon Hub, go to Users > Applications and add a new application user with that Application (client) ID.
API reference
Explore interactive endpoints and schemas at https://api.kameleon.app.
Code examples
Both Node.js + TypeScript examples below authenticate with the registered Microsoft Entra ID application using these environment variables:
TENANT_ID,CLIENT_ID, andCLIENT_SECRET— Microsoft Entra ID app credentials (see Add client secret credentials in Microsoft Entra ID).API_URL—https://api.kameleon.appAPI_SCOPE—api://e95a233a-a13e-4c5d-99b9-51832e41e036/.defaultAUTHOR_NAME— Display name of the document author (for example, John Doe).COMPANY_ID— Company ID retrieved from/v1.0/companies.UNIT_ID— Company unit ID retrieved from/v1.0/companies/{id}.DOCUMENT_ID— Document template ID retrieved from the Kameleon API (single-document example only).
Example: Create a document
Acquire an access token using a client secret and post to /v1.0/documents/{DOCUMENT_ID}/create to receive the generated Word, Excel, or PowerPoint binary:
import { ClientSecretCredential } from "@azure/identity";
async function createDocument() {
const { TENANT_ID, CLIENT_ID, CLIENT_SECRET, API_SCOPE } = process.env
const credential = new ClientSecretCredential(TENANT_ID!, CLIENT_ID!, CLIENT_SECRET!)
const accessToken = await credential.getToken(API_SCOPE!)
const { API_URL, DOCUMENT_ID, AUTHOR_NAME, COMPANY_ID, UNIT_ID } = process.env
const url = new URL(`/v1.0/documents/${DOCUMENT_ID}/create`, API_URL)
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `${accessToken.tokenType} ${accessToken.token}`
},
method: 'POST',
body: JSON.stringify({
author: {
name: AUTHOR_NAME,
companyId: COMPANY_ID,
unitId: UNIT_ID
},
properties: {}
})
})
await response.bytes() // Document binary, could be e.g. saved to external system
}
createDocument()
Example: Batch create documents
Traverse document template categories recursively and save each generated document under ./documents while preserving the category folder structure:
import { AccessToken, ClientSecretCredential } from "@azure/identity";
import { mkdir, writeFile } from 'fs/promises'
import { join } from 'path'
interface IEntity {
id: string
name: string
}
interface IDocument extends IEntity {
extension: string
}
interface IDocumentCategory extends IEntity {
contents: IDocument[]
subcategories: IEntity[]
}
const sanitizePathname = (pathname: string) => pathname.replace(/[/\\?%*:|"<>]/g, '-')
async function request(accessToken: AccessToken, path: string, body?: any) {
const url = new URL(path, process.env.API_URL)
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `${accessToken.tokenType} ${accessToken.token}`
},
method: body ? 'POST' : 'GET',
body: JSON.stringify(body)
})
if (!response.ok) {
throw new Error(`Request failed: ${response.status} ${response.statusText}`);
}
return response
}
async function generateDocuments() {
// Get access token
const { TENANT_ID, CLIENT_ID, CLIENT_SECRET, API_SCOPE } = process.env
const credential = new ClientSecretCredential(TENANT_ID!, CLIENT_ID!, CLIENT_SECRET!)
const accessToken = await credential.getToken(API_SCOPE!)
// Get document root categories
const response = await request(accessToken, '/v1.0/document-categories')
const categories: IEntity[] = await response.json()
for (const category of categories) {
await processCategory(accessToken, category, './documents')
}
}
async function processCategory(accessToken: AccessToken, entity: IEntity, path: string): Promise<void> {
// Get full category (contains documents and subcategories)
const response = await request(accessToken, `/v1.0/document-categories/${entity.id}`)
const category: IDocumentCategory = await response.json()
const categoryPath = join(path, sanitizePathname(category.name))
await mkdir(categoryPath, { recursive: true })
// Generate documents
for (const document of category.contents) {
await createDocument(accessToken, document, categoryPath)
}
// Process subcategories (recursive)
for (const subcategory of category.subcategories) {
await processCategory(accessToken, subcategory, categoryPath)
}
}
async function createDocument(accessToken: AccessToken, document: IDocument, path: string): Promise<void> {
const pathname = join(path, `${sanitizePathname(document.name)}.${document.extension}`)
console.log(`Generating '${pathname}'...`)
// Generate document binary
const { AUTHOR_NAME, COMPANY_ID, UNIT_ID } = process.env
const response = await request(accessToken, `/v1.0/documents/${document.id}/create`, {
author: {
name: AUTHOR_NAME,
companyId: COMPANY_ID, // See "/v1.0/companies" endpoint
unitId: UNIT_ID // See "/v1.0/companies/{id}" endpoint
}
})
// Save document locally
await writeFile(pathname, await response.bytes())
}
generateDocuments()