A JavaScript SDK for communicating with the Kontent.ai Custom App API. It facilitates the communication between the Kontent.ai app and the custom app providing access to the configuration and context data.
npm install @kontent-ai/custom-app-sdkImportant
The SDK attaches event listeners to communicate with the Kontent.ai app. Make sure to include the SDK only in the browser environment.
import { getCustomAppContext, CustomAppContext } from "@kontent-ai/custom-app-sdk";
const response = await getCustomAppContext();
if (response.isError) {
console.error({ errorCode: response.code, description: response.description });
} else {
// TypeScript will narrow the type based on currentPage
if (response.context.currentPage === "itemEditor") {
console.log({
contentItemId: response.context.contentItemId,
languageId: response.context.languageId,
validationErrors: response.context.validationErrors
});
}
}Retrieves the context of the custom app. The function takes no arguments and automatically detects the current page type, returning the appropriate context with all relevant properties for that page.
Returns a promise that resolves to a discriminated union type with two possible states:
| Property | Type | Description |
|---|---|---|
isError |
false |
Indicates the request was successful |
context |
CustomAppContext |
A discriminated union of page-specific context objects |
| Property | Type | Description |
|---|---|---|
isError |
true |
Indicates an error occurred |
code |
ErrorCode enum | The code of the error message |
description |
string | The description of the error message |
CustomAppContext is a discriminated union type based on the currentPage property. Each page type includes only the relevant properties for that specific page:
When currentPage is "itemEditor", the context includes:
| Property | Type | Description |
|---|---|---|
currentPage |
"itemEditor" |
Identifies this as an item editor page |
environmentId |
UUID | The environment's ID |
userId |
string | The current user's ID |
userEmail |
string | The current user's email |
userRoles |
Array of UserRole | An array containing all the roles of the current user in the environment |
path |
string | The current path within the Kontent.ai application |
pageTitle |
string | The title of the current page |
appConfig |
unknown | undefined | JSON object specified in the custom app configuration |
contentItemId |
UUID | The ID of the content item being edited |
languageId |
UUID | The ID of the current language |
validationErrors |
Record<string, string[]> | A record of validation errors for content item fields |
When currentPage is "other", the context includes:
| Property | Type | Description |
|---|---|---|
currentPage |
"other" |
Identifies this as any other page type |
environmentId |
UUID | The environment's ID |
userId |
string | The current user's ID |
userEmail |
string | The current user's email |
userRoles |
Array of UserRole | An array containing all the roles of the current user in the environment |
path |
string | The current path within the Kontent.ai application |
pageTitle |
string | The title of the current page |
appConfig |
unknown | undefined | JSON object specified in the custom app configuration |
| Property | Type | Description |
|---|---|---|
id |
UUID | The role's ID |
codename |
string | The role's codename - applicable only for the Project manager role |
Subscribes to context changes and receives notifications when the context is updated. The function takes a callback that will be invoked whenever the context changes.
| Parameter | Type | Description |
|---|---|---|
callback |
(context: CustomAppContext) => void |
Function to be called when the context changes |
Returns a promise that resolves to a discriminated union type with two possible states:
| Property | Type | Description |
|---|---|---|
isError |
false |
Indicates the request was successful |
context |
CustomAppContext |
The initial context value |
unsubscribe |
() => Promise<void> |
Function to call to stop receiving context updates |
| Property | Type | Description |
|---|---|---|
isError |
true |
Indicates an error occurred |
code |
ErrorCode enum | The code of the error message |
description |
string | The description of the error message |
import { observeCustomAppContext } from "@kontent-ai/custom-app-sdk";
const response = await observeCustomAppContext((context) => {
console.log("Context updated:", context);
});
if (response.isError) {
console.error({ errorCode: response.code, description: response.description });
} else {
console.log("Initial context:", response.context);
// Later, when you want to stop observing
await response.unsubscribe();
}Sets the size of the popup window when the custom app is displayed in a popup.
| Parameter | Type | Description |
|---|---|---|
width |
PopupSizeDimension |
The desired width of the popup |
height |
PopupSizeDimension |
The desired height of the popup |
A discriminated union type for specifying dimensions in either pixels or percentages:
type PopupSizeDimension =
| { unit: "px"; value: number }
| { unit: "%"; value: number };Returns a promise that resolves to a discriminated union type with two possible states:
| Property | Type | Description |
|---|---|---|
isError |
false |
Indicates the request was successful |
| Property | Type | Description |
|---|---|---|
isError |
true |
Indicates an error occurred |
code |
ErrorCode enum | The code of the error message |
description |
string | The description of the error message |
import { setPopupSize } from "@kontent-ai/custom-app-sdk";
const response = await setPopupSize(
{ unit: "px", value: 800 },
{ unit: "%", value: 90 }
);
if (response.isError) {
console.error({ errorCode: response.code, description: response.description });
}For Contributing please see CONTRIBUTING.md for more information.
Distributed under the MIT License. See LICENSE.md for more information.