Skip to content
Last updated

Your app schema is defined in the index.js file. The app schema defines all the configurations as well as the plugins that your app creates. The app schema is a defined in JSON format. When you scaffold an app, a default schema is created.

{
  "id": "YOUR_APP_NAME",
  "version": "YOUR_APP_VERSION",
  "platformVersion": "APP_PLATFORM_VERSION",
  "authentication": "THE_AUTHENTICATION_SCHEMA",
  "beforeRequest": "THE_BEFORE_REQUEST_SCHEMA",
  "afterResponse": "THE_AFTER_REQUEST_SCHEMA",
  "actions": "THE_ACTIONS_SCHEMA",
  "messaging": "THE_MESSAGING_SCHEMA",
  "resources": "THE_RESOURCES_SCHEMA"
}

PROPERTIES

PROPERTIESDESCRIPTIONVALUE TYPEDEFAULT VALUE
idThe identifier of your Appstring
versionThe version of your appnumberversion specified in package.json
platformVersionThe version of app-platform package you used to create the appnumberversion specified in package.json of the @chatlayerai/app-platform in node modules.
authenticationThe authentication schema. This schema is used to authenticate the accounts which will be used in this appAuthentication Schema{}
beforeRequestThe schema of things to do before calling sdk.request method. They are the middlewares that you add before making any sdk request.Array of functions[]
afterResponseThe schema of things to do after calling sdk.requestArray of functions[]
actionsThe schema of plugins that you want to create which shows up in Action Dialogstate in chatlayer.Actions Schema{}
messagingThe schema of sending and receiving messages from different channels like whatsapp, facebook messenger etc..Messaging Schema{}
resourcesThe schema of the resources that you want to provide to the app. This can be used laterResources Schema{}

Example

const includeApiKey = (request, sdk, bundle) => {
    if (bundle.authData.api_key) {
        request.headers = {
            ...request.headers,
            "Content-Type": "application/json",
            Authorization: `Bearer ${bundle.authData.api_key}`,
        };
    }

    return request;
};
const authenticationConfig = {
    type: "custom",
    test: (sdk, bundle) => {
        return sdk
            .request({
                url: `https://${bundle.authData.host}/v2/agents`,
            })
            .then((res) => {
                if (res.status === 400) {
                    throw new Error("Invalid region");
                }
                if (res.status === 401) {
                    throw new Error("Invalid API key");
                }
            });
    },
    connectionLabel: () => "FreshChat",
    fields: [
        {
            key: "host",
            label: "Region",
            placeholder: "Choose your datacenter region",
            default: "api.freshchat.com",
            choices: {
                "api.freshchat.com": "United States",
                "api.eu.freshchat.com": "Europe",
                "api.in.freshchat.com": "India",
                "api.au.freshchat.com": "Australia",
            },
        },
        {
            key: "api_key",
            label: "API Key",
            required: true,
            helpText:
                "You can generate an API Key by login to your FreshChat account and navigating to Admin > API Tokens > Generate Token",
        },
    ],
}

module.exports = {
    version: require("./package.json").version,
    platformVersion: require("@chatlayerai/app-platform").version,
    id: "freshdesk-messaging",
    authentication: authenticationConfig,
    beforeRequest: [includeApiKey],
    actions: {},
    messaging,
    resources: {},
};