Skip to main content
Skip table of contents

Example: Create document

This example demonstrates how the Kameleon API can be used to create single document from document template.

Node.js + TypeScript

Following environment variables are used in example

  • TENANT_ID, CLIENT_IDand CLIENT_SECRET Azure AD credentials used for authentication.

  • API_URL https://api.kameleon.app

  • API_SCOPE scope for Kameleon API api://e95a233a-a13e-4c5d-99b9-51832e41e036/.default

  • AUTHOR_NAME name of the document author e.g John Doe

  • COMPANY_ID company identifier retrieved from the Kameleon API.

  • UNIT_ID company unit identifier retrieved from the Kameleon API.

  • DOCUMENT_ID document template identified retrieved from the Kameleon API

Full example

This example uses client secret to acquire access token for Kameleon API call.

TYPESCRIPT
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()
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.